bash script

Posted on 2013-12-30
Last Modified: 2013-12-31

I have a question, like to require  your assistance, want a bash script which work same like as below mentioned perl script:


my %OPTS = @ARGV;
my $user = $OPTS{'user'};

$task = "mv /backup/cpbackup/daily/$user.tar.gz /backup-delete/$user.tar.gz";
$contents = `$task`;


Question by:smksa
LVL 23

Expert Comment

ID: 39747573
Since the first declarations are never used I imagine this would have the same effect:


task="mv /backup/cpbackup/daily/$user.tar.gz /backup-delete/$user.tar.gz"
LVL 77

Expert Comment

ID: 39747640
Presumably the argumets being passed to your perl script are user username

the bash script that savone provided has to be modified to;

task="mv /backup/cpbackup/daily/$2.tar.gz /backup-delete/$2.tar.gz"

Open in new window

If you perl script is called with various arguments for historical reasons and user username are not the only arguments on the line and they are not the first two.  Further checks within the bash script have to be done to evaluate each argument to see whether it is the string 'user' and then use the next argument.
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 39747705
The most obvious bash script to do that might be
perl -e 'my %OPTS = @ARGV; my $user = $OPTS{user}; $task = "mv /backup/cpbackup/daily/$user.tar.gz /backup-delete/$user.tar.gz"; $contents = `$task`; exit;' $*

Open in new window

But maybe you're interested in a more "bash"ful method.

%OPTS = @ARGV;  seems a fragile way to set option values, but instead of arguing with it, I'll just try to do something similar in bash
if (( $# % 2 != 0 )) ; then echo "Odd number of elements in hash assignment" >&2 ; fi
while [[ $1 = 'user' ]] && user=$2 ; shift 2 ; do continue ; done
contents=`mv /backup/cpbackup/daily/$user.tar.gz /backup-delete/$user.tar.gz`

Open in new window


Author Closing Comment

ID: 39747784
superb  ozo, yes it's working according to desire results :)


Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to rebuild XFS volume from LV 19 90
Penetration Testing home based work 3 91
SonarQube on Linux vs Windows 3 53
How to use question mark (?) in filename with html 25 73
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question