简体   繁体   中英

Converting Perl script to Bash

I have a Perl script below and I want to convert it to a Bash script but sad to say I have difficulties in Bash. Can anyone help me this.

#!/usr/bin/perl

use strict;
use warnings;


my $output = `df -h`;
my $bundle = "`awk -F \'=\' \'{print \$2}\' config.txt`";
my $bundlename = `echo $bundle | awk -F \'/\' \'{print \$11}\'`;
print $output;

system ("wget $bundle");
print "$bundlename\n";
tar();


sub tar {
    my $cd = system ("tar -xzvf $bundlename") ;
    system ("rm -vf $bundlename");
}

Please can anyone convert it to Bash script?

#!/bin/sh
bundle=$(awk '$0=$2' FS== config.txt)
bundlename=$(awk '$0=$11' FS=/ <<< "$bundle")
dh -h
wget $bundle
echo $bundlename
tar -xzvf $bundlename
rm -vf $bundlename

This is a very straight-forward direct transliteration of the Perl:

#!/bin/bash

output=$(df -h)
bundle=$(awk -F = '{print $2}' config.txt)
bundlename=$(echo $bundle | awk -F / '{print $11}')  # Possible echo "$bundle" instead?
echo "$output"

wget $bundle
echo "$bundlename"
tar -xzvf "$bundlename"
rm -vf "$bundlename"

Given what you do with the $output variable, you might as well simply write:

bundle=$(awk -F = '{print $2}' config.txt)
bundlename=$(echo $bundle | awk -F / '{print $11}')  # Possible echo "$bundle" instead?
df -h

wget $bundle
echo "$bundlename"
tar -xzvf "$bundlename"
rm -vf "$bundlename"

It isn't clear that the df -h is providing any benefit to the script; you might simply delete the line instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM