Bash- Command run from script does not pass full parameters with spaces inside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash- Command run from script does not pass full parameters with spaces inside
# 1  
Old 06-13-2012
Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so:

Code:
./RunDiag.js param1:'string one here' param2:'string two here'

I have a shell script where I invoke the above command. I can run it in a script as simple as this

Code:
#!/bin/bash

stuff="./RunDiag.js param1:'string one here' param2:'string two here'"
$stuff

When I run this within a script, the parameters are all truncated, each becoming 'string, instead of the entire parameter. Why? When I run this command from command line, no such thing occurs.

---------- Post updated at 03:31 PM ---------- Previous update was at 02:03 PM ----------

Having done more testing, it seems like that every time there's a space between items in the string, such as "string one", the spaces are converted into commas: string,one - any idea what might be causing this?
# 2  
Old 06-13-2012
The shell is field splitting the unquoted variable reference, using $IFS. Try:
Code:
eval "$stuff"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-13-2012
Quote:
#!/bin/bash

stuff="./RunDiag.js param1:'string one here' param2:'string two here'"
$stuff
Why so complicated?

Try:
Code:
#!/bin/bash
./RunDiag.js 'param1:string one here' 'param2:string two here'


Last edited by methyl; 06-13-2012 at 07:56 PM.. Reason: grammar
# 4  
Old 06-13-2012
That script is really just a reduced and obfuscated equivalent to what my full script is doing- it's building each part of that command piece by piece, including the parameter list, which is parsed elsewhere. When I run it in my shell script like

Code:
$RunDiag ${params[@]}

it fails because if any of the parameters are strings that are like 'string one', the spaces are overwritten sometime in the process into commas.

I've found a solution: change the default IFS temporarily when I run that command, and it works.

Edit: You're right, scrutinizer! It took me some time looking it up but that was the solution, yeah.
# 5  
Old 06-14-2012
Good, but since you are using arrays, did you try:
Code:
$RunDiag "${params[@]}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing sed command inside a bash script

I want to run commands inside a bash script. An example is I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable sed.sh regexp sed.sh "-i \"s/<p>//g\"" then call sed "$regexp" $fl (3 Replies)
Discussion started by: Kangol
3 Replies

2. Shell Programming and Scripting

Listen sharp time to run a command inside a script

Hello, Just wondered if there is any possibility to run a command at sharp time inside a script in linux. My question is not about crontab Example: #!/bin/bash cd /home/database for i in * do command 1 if time is 19:00, day is Monday then run command2 if time is 20:00, day is... (10 Replies)
Discussion started by: baris35
10 Replies

3. Shell Programming and Scripting

Run bash command inside zsh script

Hi, I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh). I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory " eve though I merged the whole... (7 Replies)
Discussion started by: kamcamonty
7 Replies

4. Shell Programming and Scripting

Pass Parameters to awk command

I need to pass values at runtime for the below awk command where l is the length and partial.txt is the file name. awk -v l=285 '{s="%-"l"s\n";printf(s,$0);}' partial.txt > temp1.txt; (5 Replies)
Discussion started by: Amrutha24
5 Replies

5. Shell Programming and Scripting

Preserve extented ascii character when run echo comand inside bash script

Hi everyone, I'm echo some text with extended ascii characters as below: echo -e "Pr\xE9sentation du spectacle" > output or echo -e "Présentation du spectacle" > outputIf I open the file created I see this text Présentation du spectacleThe text is shown correctly in this created file when... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. Shell Programming and Scripting

need to pass parameters to working and tested awk script

I have a working and tested AWK script that removes duplicates from an input file and generates an output file without the duplicates. I had help from my other post to develop it: ... (3 Replies)
Discussion started by: script_op2a
3 Replies

8. Shell Programming and Scripting

How to pass parameters transparently into a sub script

Hi, I am trying to write a script like this: #!/bin/ksh #script name: msgflow #The awk commands for Solaris and Linux are incompatible if ] then msgflow-solaris $* elif ] then msgflow-linux $* fi This script is shared by a file system which is visible to both... (3 Replies)
Discussion started by: danielnpu
3 Replies

9. Shell Programming and Scripting

Run the command inside perl script

I have a command which will run fine in a unix command prompt. Can you tell how to interprete this command inside perl script...... The command is : perl -pe 's/(\|333\}.*)\}$/$1|1.6}/' FIA.txt This will search for the number 333 and appends 1.6 at the end of that line....... (1 Reply)
Discussion started by: vinay123
1 Replies

10. UNIX for Dummies Questions & Answers

How to pass two or more parameters to the main in shell script

Hey Guys from the below script what I understood is we are sending the the first parameter as input to the main (){} file main > $LOGFILE 2>&1 but can we send two or three parameter as input to this main file as main > $LOGFILE 2>&1 2>&2 like this Can any one plz help I need to writ a... (0 Replies)
Discussion started by: pinky
0 Replies
Login or Register to Ask a Question