Solved

bash script

Posted on 2013-12-30
4
444 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
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 76

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now