passing asterisk to a script as variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing asterisk to a script as variable
# 1  
Old 04-01-2008
passing asterisk to a script as variable

I'm writing a script that will ssh to a number of hosts and run commands. I'm a bit stumped at the moment as some of the commands that I need to run contain wildcards (i.e. *), and so far I have not figured out how to escape the * character so the script doesn't expand it. More specifically, here's an excerpt of the script.

Quote:
#!/bin/sh

COMMAND="$@"

for HOST in `cat $INFILE | grep -v ^#`
do
echo "============== $HOST ==============="
echo " "
ssh $HOST $COMMAND
echo " "
echo " "
done
Let's say I'm trying to pass a command some_command * options to the script. No matter how I tried escaping the *, $@ variable always converts it to the list of files. Any ideas on how to fix this?
# 2  
Old 04-01-2008
Code:
some_command '*' options

# 3  
Old 04-01-2008
No go... already tried that. * is still expanded into the list of files in current directory. Backslash doesn't work, either.
# 4  
Old 04-01-2008
Quote:
Originally Posted by GKnight
I'm writing a script that will ssh to a number of hosts and run commands. I'm a bit stumped at the moment as some of the commands that I need to run contain wildcards (i.e. *), and so far I have not figured out how to escape the * character so the script doesn't expand it. More specifically, here's an excerpt of the script.



Let's say I'm trying to pass a command some_command * options to the script. No matter how I tried escaping the *, $@ variable always converts it to the list of files. Any ideas on how to fix this?

Quote the variables. It is not $@ that expands the asterisk, but the shell that called the script.

You will also have to escape the asterisk:
[CODE]
COMMAND="whatever \* whatever"
ssh "$HOST" "$COMMAND"
{/CODE]
# 5  
Old 04-01-2008
Nope, still doesn't work... I tried that one as well. It doesn't expand the file list anymore, but then I have the backslash as part of my variable (see below for an example).

I'm not sure it's the shell that's expanding the variable:

Code:
$ echo "some_command \* options"
some_command \* options
$ echo "some_command * options"
some_command * options

meanwhile, by adding "echo $COMMAND" to my script, I get:

Code:
./test-script "some_command * options"
some_command ...list of files... options

./test-script "some_command \* options"
some_command \* options

I tried running the script from bash and ksh with the same results.
# 6  
Old 04-01-2008
Quote:
Originally Posted by GKnight
Nope, still doesn't work... I tried that one as well. It doesn't expand the file list anymore, but then I have the backslash as part of my variable (see below for an example).

I'm not sure it's the shell that's expanding the variable:

The shell is the only thing that does expand wildcards, unless you have written something else to do it.
Quote:
Code:
$ echo "some_command \* options"
some_command \* options
$ echo "some_command * options"
some_command * options


The wildcard will not be expanded if it is quoted.
Quote:
meanwhile, by adding "echo $COMMAND" to my script, I get:

Code:
./test-script "some_command * options"
some_command ...list of files... options

./test-script "some_command \* options"
some_command \* options

I tried running the script from bash and ksh with the same results.

Always quote your variables (unless you have a good reason not to):
Code:
ssh "$HOST" "$COMMAND"

# 7  
Old 04-01-2008
As previously stated in an ssh script, the commands ( and eventually all the special characters in them, for example: *, $, ...) will be evaluated first by the current shell, before ssh-in.

So you need to properly escape all of them. For more check these threads:


https://www.unix.com/shell-programmin...s-via-ssh.html

https://www.unix.com/shell-programmin...sh-script.html

-
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable from called script to the caller script

Hi all, Warm regards! I am in a difficult situation here. I have been trying to create a shell script which calls another shell script inside. Here is a simplified version of the same. Calling Script. #!/bin/ksh # want to run as a different process... (6 Replies)
Discussion started by: LoneRanger
6 Replies

2. Shell Programming and Scripting

Need to use asterisk in variable

Hi All, I am having a challange to pass the asterisk in the variable. Basically, I am writing a shell script to check if a marker file exists but when I am assigning the varialbe it cannot use the wildcard asterisk as expected, therefore, my program is always outputs "Marker file is not... (4 Replies)
Discussion started by: express14
4 Replies

3. Shell Programming and Scripting

Passing variable as an argument to another script

Hi, I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem. varaiable "a" value should be -b "FPT MAIN". When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting... (9 Replies)
Discussion started by: Manasa Pradeep
9 Replies

4. Shell Programming and Scripting

help passing variable from script

Hello, How can I pass a variable into a 2nd file? I'm running a script: ls -la $1 >pem99 cat pem99 | awk '{ print $3}' >us99 cat us99 | read us98 This then tells me the owner of a file. my second file is: echo OWNER GROUP OTHERS echo echo --data-- $us98 ... (2 Replies)
Discussion started by: Grueben
2 Replies

5. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

6. Shell Programming and Scripting

How to assign * asterisk to variable?

How can I assign asterisk to variable I have try a="\* " but I was not succesful :( any idea ? thanks (5 Replies)
Discussion started by: kvok
5 Replies

7. Shell Programming and Scripting

Passing asterisk As A Parameter

I have written a Shell Script Program which accepts 3 parameters as shown below: ./calc 20 + 2 in the above line ./calc is the Shell Script itself with 3 parameters, namely: 20 + and 2. Well, now let's look inside the Script: result=$1$2$3 echo $result The output will be as... (8 Replies)
Discussion started by: indiansoil
8 Replies

8. Shell Programming and Scripting

How to ignore * (asterisk) in a variable

I am using a shell script to read SQL statements stored in a DB2 table and write them out to a file. The problem I have is that some SQL statements have an "*" in them which gets resolved as the list of files in the current directory when I run the script. How can I prevent the "*" from being... (7 Replies)
Discussion started by: bradtri2
7 Replies

9. UNIX for Dummies Questions & Answers

passing variable to my script

Hello everybody: Im trying to run the following script on my sol9 machine: line='' ((lineCount= 0)) export lineCount more /tmp/MSISDNs | wc -l > /tmp/tmp cat /tmp/tmp | read lineCount export lineCount; while (( lineCount > 0 )) do line= tail -$lineCount... (5 Replies)
Discussion started by: aladdin
5 Replies

10. UNIX for Advanced & Expert Users

Passing a variable into an awk script

Hello all, I'm trying to run a script of this format - for i in $(cat <file>); do grep $i <file1>|awk '{print $i, $1, $2}' It's not working - does anyone know how this can be done? Khoom (5 Replies)
Discussion started by: Khoomfire
5 Replies
Login or Register to Ask a Question