Solved

bash script

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
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…
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…

776 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