Solved

bash script

Posted on 2013-12-30
4
445 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 77

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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 clone solaris 10 machine 33 110
Linux hostname change 2 51
AD LDAP LDS 3 47
Help on running oracle SQL script in linux 3 25
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 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.

948 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

23 Experts available now in Live!

Get 1:1 Help Now