doing own custom parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting doing own custom parameters
# 1  
Old 01-17-2011
doing own custom parameters

I have an rsync command that I want to create a variable where user can change to customize the parameters.

complete rsync command to run:
Code:
$RSYNC -e 'ssh -ax -o ClearAllForwardings=yes' --log-file=$LOG_FILE --delete -avzcr -u  --update $SRC_DIR $USER@$TRG_SRV:$TRG_DIR >> $LOG_FILE

What I want to do is separate our the parameters into a variable:
Code:
rsync_param="-e 'ssh -ax -o ClearAllForwardings=yes' --log-file=$LOG_FILE --delete -avzcr -u  --update"

Then I modify the rsync to run as below:
Code:
$RSYNC $rsync_param $SRC_DIR $USER@$TRG_SRV:$TRG_DIR >> $LOG_FILE

But it doesn't work. I get syntax error. But when I echo out the command, it is same as the original command.

Any idea how to solve this?

Last edited by Scott; 01-17-2011 at 07:00 AM.. Reason: Please use code tags
# 2  
Old 01-17-2011
When you assign the string to the variable $rsync_param it will take the [bold]current[/bold] value of the variable $LOG_FILE, which may or may not be set (you haven't shown any context) so that when rsync comes to run it sees --log-file=
which would be a syntax error.
# 3  
Old 01-17-2011
I've run into this issue when I wrote an rsync script, in which I had to eval the command to get things to work properly, as in:
Code:
eval $RSYNC $rsync_param $SRC_DIR $USER@$TRG_SRV:$TRG_DIR >> $LOG_FILE

To demonstrate what is happening, I created a quick script to display the number of parameters, and the parameters themselves:
Code:
#! /bin/sh

echo '#:' ${#}
while [[ 0 -lt ${#} ]]; do echo "${1}"; shift; done | cat -n

And here are the results:
Code:
trogdor $ ./countem a b c d
#: 4
     1    a
     2    b
     3    c
     4    d

trogdor $ ./countem a 'b c' d
#: 3
     1    a
     2    b c
     3    d

Which is as expected. But if we:
Code:
trogdor $ ARGS="a 'b c' d"

trogdor $ ./countem ${ARGS}
#: 4
     1    a
     2    'b
     3    c'
     4    d

${ARGS} is expanded into four tokens. Double-quoting doesn't help either:
Code:
trogdor $ ./countem "${ARGS}"
 #: 1
     1    a 'b c' d

What you need to do is have the shell re-parse your command line, so that it is recognizes the quoting within your variables:
Code:
trogdor $ eval ./countem "${ARGS}"
  #: 3
     1    a
     2    b c
     3    d

Or without the quotes:
Code:
trogdor $ eval ./countem ${ARGS}
#: 3
     1    a
     2    b c
     3    d

Be warned, the script will evaluate the command line twice. Be careful with characters that will cause command line expansion.
This User Gave Thanks to m.d.ludwig For This Post:
# 4  
Old 01-18-2011
thanks for the explanation. Now I understand what's the problem and usage of "eval".

I manage to solve the problem by dumping the whole command into another variable and then use eval on that variable.

Code:
run_rsync="$RSYNC $rsync_param $SRC_DIR $USER@$TRG_SRV:$TRG_DIR >> $LOG_FILE"
eval $run_rsync

Tested and it seems to be running.

Thanks again for the great help. Smilie
# 5  
Old 01-18-2011
Most awsome post mr ludwig. I have been having trouble with understanding what exactly eval was good for. Now I know too.

I would be interested in an example of a situation where this doesn't work as you were warning about.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Custom Report

Hi All, Am getting the raw report from the source and need to prepare the custom report as per the requirement. Requirement keep getting change according to the need. Raw data is as below /* ----------------- test_job_hu ----------------- */ insert_job: test_job_hu job_type: CMD... (4 Replies)
Discussion started by: pradeep84in
4 Replies

2. Programming

Remote login UNIX box from java passing parameters to the custom script called in the profile

Hello Good Day / Guten Tag.... I have to login the server and the user profile contains some scripts which need the inputs to be taken from the keyboard. So I use the method to conn.authenticateWithKeyboardInteractive(username, new InteractiveCallback() { public String... (1 Reply)
Discussion started by: Sanalkumaran
1 Replies

3. Red Hat

Custom nagios configuration

Hi, I want to configure nagios core with my own scripts.Default configuration has been done like ping,http,Root partition,..so on... But according to my requirement i have a script called cpumon.sh which will monitor the current cpu usage. I gone through below link but could not find out... (1 Reply)
Discussion started by: mastansaheb
1 Replies

4. Shell Programming and Scripting

Custom Shell

I have a jump off server, which grants SSH access to a few other servers. I would like to create a custom shell which can be assigned to specific user accounts which runs a menu script upon login, where they can select which server they want to jump too, however should they hit ctrl-c or any... (1 Reply)
Discussion started by: JayC89
1 Replies

5. Shell Programming and Scripting

custom command

hi I am trying to make my own commands in my linux.I thought a command for changing directories will be easy. I made a simple file amd made the entries #!/bin/bash cd /opt/mydir I then made the file executable and then moved it to /usr/bin. But when i type the script name nothing... (2 Replies)
Discussion started by: born
2 Replies

6. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

7. UNIX for Dummies Questions & Answers

Changing ip in a custom way

Hi. I hope someone can help me. I have e very special question. I have a Lunix server and I have installed Webmin on it. This way, I can create a login for an other user and give him restricted access to some custom commands I set up. One of the commands i would like to setup, is for him to... (9 Replies)
Discussion started by: Wonderke
9 Replies

8. Shell Programming and Scripting

Custom PS command

(0 Replies)
Discussion started by: goldfish
0 Replies

9. Solaris

Custom pam module

Does anyone know how to create a custom pam module for modifying the login authentication procedure? (1 Reply)
Discussion started by: mhm4
1 Replies

10. UNIX for Dummies Questions & Answers

How to custom application name in `ps -ef`?

A program named /usr/bin/aa.sh, two parameters: 11, 22. after start it, the row in `ps -ef` is almost like the following: root 12198 10278 0.0 Nov 25 pts/3 0:00.23 /usr/bin/aa.sh 11 22 but I want to change "/usr/bin/aa.sh 11 22" to one rule string, such as: "AA_11_22", how to... (1 Reply)
Discussion started by: linkjack
1 Replies
Login or Register to Ask a Question