Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion

Go Back   Tilted Forum Project - TFP - Sexuality, Philosophy and Political Discussion > Interests > Tilted Technology

Reply
 
LinkBack Thread Tools
Old 02-26-2004, 03:55 AM   #1 (permalink)
paranoid
 
Silvy's Avatar
 
Join Date: Jun 2003
Location: The Netherlands
Shell scripting help (bash)

Hi,

I have the following problem:
I would like a script to decide on a commandline that should be executed.
This descision is based on the arguments passed to the script.
The possibilities for the commandline-to-be-executed are fixed, and declared at the beginning of the script.

script flow:
- declaration of possible command calls (CALL_A and CALL_B , see below)
- case statement on the argument $1 that either sets (CALL=CALL_A or CALL=CALL_B)
- exectute CALL

This works fine in most cases, except when CALL_A or CALL_B use an output re-director.
If I declare CALL_A="ls -al >test.txt"
then ls is executed with "-al" and ">test.txt" as parameters, causing an error because ls can't find the file ">test.txt".

I realize this might sound fuzzy, so I wrote 2 scripts as an example:

Script 1:
(echo's all arguments that were passed to it. this allows you to see how the commandline was interpreted.)
Code:
#!/bin/bash 
#prints the first 4 arguments that are passed to it.  $0 is the name of the script!
#
if [ -n $0 ]
then
   echo "arg 0: $0" 
fi
if [ -n $1 ] then
   echo "arg 1: $1" 
fi 
if [ -n $2 ] then
   echo "arg 2: $2" 
fi 
if [ -n $3 ] then   
  echo "arg 3: $3" 
fi 
if [ -n $4 ] then 
   echo "arg 4: $4" 
fi
Script 2:
(This script is giving me troubles, it runs the above script with normal or redirected output commandlines. The second type should echo into a file and not on screen.)
Code:
## testing script

#command to run:
CALL_A="./arg_check 1st 2nd 3rd 4th"
CALL_B="./arg_check 1st >result.txt"

#select command based on argument, or present helpful message:
if [ -n "$1" ]  #test wether $1 has been initialized
then
  case "$1" in
  "a" ) CALL=$CALL_A ;;
  "b" ) CALL=$CALL_B ;;
  * ) echo "Duh, enter a or b"
      exit 1 ;;
  esac
else
  echo "Enter a or b as an argument please"
  exit 1;
fi

#call the chosen command:
$CALL
If you copy/save these scripts as "arg_check" and "testscript" respectively, you can run "./testscript a" or "./testscript b" to see the results. (remember to chmod +x them both)

You'll notice that the CALL_B commandline if executed directly on the prompt behaves as expected: the output of "arg_check a" is redirected to "result.txt"

I'd REALLY like to know what in the second script I should change to get the expected result: i.e.
./testscript a results in output to screen
./testscript b results in output to file

Note: It's not really an option to remove the declarations and "$CALL" statement, and move the commandlines directly into the case statement (which would work irf you used "backticks": `) as the script this is meant for uses several sources and checks to build and conditionally execute the commands.

2nd note: I'm quite new to shell scripting so if you have any tips, please post them! Also if this code is laughable: any constructive comments are really appreciated!

EDIT: edited because the "submit" button is a lot more attractive than the one for "preview"
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. "
- Murphy MacManus (Boondock Saints)

Last edited by Silvy; 02-26-2004 at 04:00 AM.
Silvy is offline   Reply With Quote
Old 02-26-2004, 08:52 AM   #2 (permalink)
Rookie
 
Join Date: Feb 2004
You might like this site if you haven't come across it already: http://tldp.org/LDP/abs/html/

I'm kind of a newbie too (haven't had *any* time to start learning recently), but it seems like you've forgotten the #!/bin/bash on your "testscript" file. That may fix the problem.
mgcloud is offline   Reply With Quote
Old 02-26-2004, 05:31 PM   #3 (permalink)
paranoid
 
Silvy's Avatar
 
Join Date: Jun 2003
Location: The Netherlands
That was exactly the site that helped me this far, but for finding a specific answer I think it's a little chaotic...
But to be honest, I haven't read it from the beginning, I just glance at the subjects I need.

I couldn't find an example or answer to my problem in that document, but I realise it's not something you'd come across often.

And yes, I did forget to add the "shabang" line in my script, it was the haste. Adding it made no difference though.

Thanks for the help, but the problem still stands. Anyone?
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. "
- Murphy MacManus (Boondock Saints)
Silvy is offline   Reply With Quote
Old 02-26-2004, 09:53 PM   #4 (permalink)
Rookie
 
Join Date: Feb 2004
Hi,

Try replacing your last line with eval $CALL
(instead of $CALL)
mgcloud is offline   Reply With Quote
Old 02-27-2004, 01:16 AM   #5 (permalink)
paranoid
 
Silvy's Avatar
 
Join Date: Jun 2003
Location: The Netherlands
Quote:
Originally posted by mgcloud
Try replacing your last line with eval $CALL
(instead of $CALL)
Thanks a lot! That was exactly it!


Cheers!
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. "
- Murphy MacManus (Boondock Saints)
Silvy is offline   Reply With Quote
Reply

Bookmarks

Tags
bash, scripting, shell

Thread Tools

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
Forum Jump


All times are GMT -7. The time now is 01:03 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
All text (c) 2002-2008 Tilted Forum Project
"Insignia" vBulletin 3.5 - b6gm6n - x7x7x7.com