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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 79

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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

624 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