The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Newbie Question: passing a variable into java from script? Cailet Shell Programming and Scripting 1 10-18-2007 10:31 PM
passing variable to my script aladdin UNIX for Dummies Questions & Answers 5 06-18-2007 10:34 AM
Passing a variable into an awk script Khoomfire UNIX for Advanced & Expert Users 5 09-05-2006 09:44 AM
passing ip address as a variable to script for ftp Gerry405 UNIX for Dummies Questions & Answers 2 09-01-2005 06:26 AM
passing awk variable to the shell script bcheaib Shell Programming and Scripting 3 07-21-2004 11:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 04-01-2008
GKnight GKnight is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 35
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 (permalink)  
Old 04-01-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
some_command '*' options

  #3 (permalink)  
Old 04-01-2008
GKnight GKnight is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 35
No go... already tried that. * is still expanded into the list of files in current directory. Backslash doesn't work, either.
  #4 (permalink)  
Old 04-01-2008
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Quote:
Originally Posted by GKnight View Post
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 (permalink)  
Old 04-01-2008
GKnight GKnight is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 35
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 (permalink)  
Old 04-01-2008
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Quote:
Originally Posted by GKnight View Post
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 (permalink)  
Old 04-01-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
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:


How to execute multiple commands via ssh

IF condition failing in a SSH script

-
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0