Solved

bash script

Posted on 2013-12-30
4
450 Views
Last Modified: 2013-12-31
Hi,

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

==================================================
#!/usr/bin/perl

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

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

==================================================

BR
Javaid
0
Comment
Question by:smksa
[X]
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
4 Comments
 
LVL 23

Expert Comment

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

#!/bin/bash

task="mv /backup/cpbackup/daily/$user.tar.gz /backup-delete/$user.tar.gz"
contents=`$task`
exit
0
 
LVL 78

Expert Comment

by:arnold
ID: 39747640
Presumably the argumets being passed to your perl script are
perlscript.pl user username

the bash script that savone provided has to be modified to;
#!/bin/bash

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

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.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39747705
The most obvious bash script to do that might be
#!/bin/bash
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
#!/bin/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

0
 
LVL 2

Author Closing Comment

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

BR
Javaid
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

710 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