bash, command line substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash, command line substitution
# 8  
Old 09-15-2011
Thanks for the error posted previously. Was rushed.
Here's 2 scripts. I'm certain my array use is correct.
proofTstA.sh
Code:
#!/bin/bash  -e
listOfParms=$(echo ONE TWO \"TH REE\")
echo PARMS: $listOfParms
# next line doesn't work as intended
./proofTstB.sh $listOfParms
# next line does work as intended
eval "./proofTstB.sh $listOfParms"
exit

proofTstB.sh
Code:
#!/bin/bash 
echo "------------------------"
echo
parmsArray=("$@")
elem_cnt=${#parmsArray[@]}
echo element count: $elem_cnt
indx=0
while [ "$indx" -lt "$elem_cnt" ]
do
	echo "parm[$indx]: ${parmsArray[indx]}"
	let indx+=1
done
exit


Last edited by skippyV; 09-15-2011 at 05:12 PM..
# 9  
Old 09-15-2011
listOfParams wasn't an array either; your arguments weren't ever three things.

Code:
#!/bin/bash

listOfParms=( ONE TWO "TH REE" )

# echo really is getting ONE TWO "TH REE" as three args here -- it's just
# impossible to tell from the output.
echo PARMS: "${listOfParms[@]}"

# This illustrates what happens to the array better.
# IFS controls what characters variables split on when evaluated,
# and what character arrays are joined on when flattened with[*].
OLDIFS="$IFS" ; IFS=","
echo PARMS: "${listOfParms[*]}"
IFS="$OLDIFS"

./prooftstb.sh "${listOfParms[@]}"

Your prooftstb.sh works, though you can simplify it:

Code:
#!/bin/bash

echo "$# args"

for ((N=1; N<=$#; N++))
do
        echo "arg ${N}:        ${!N}"
done

Code:
$ ./prooftsta.sh
PARMS: ONE TWO TH REE
PARMS: ONE,TWO,TH REE
3 arguments
arg 1:        ONE
arg 2:        TWO
arg 3:        TH REE

# 10  
Old 09-15-2011
Quote:
listOfParams wasn't an array either; your arguments weren't ever three things.
I never said the input starts out as an array or an array of 3 things. I said it is a list which I could place into a variable. And the list contains quoted strings containing white space.
I have no control over the generation of the input - but of course I can massage it within scriptA before sending it on to scriptB. I merely tried to emulate the input with an echo of that variable. Your IFS block of code is useful. It demonstrated that my "emulated" input was incorrect in regards to what I was receiving during my earlier observations.
With your IFS block I saw the following printout:
Quote:
ONE,TWO,"TH,REE"
Obviously by placing that input into an array - it will contain four elements. (I tried it anyways.) But to get the desired interpretation of this list as 3 elements, I still believe "eval" is necessary.
So thank you for your input. It was very helpful.
# 11  
Old 09-15-2011
Yeah, to process that kind of string you need eval. Just watch what you feed into it very carefully.

STR=$(echo stuff) won't help; the number of arguments is only determined by the splitting of the string later.

You can just do

Code:
STR='this has "quotes and stuff" in it'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question on bash command line

OS : RHEL / Oracle Linux 6.8 In bash shell, how can I replace a character under the cursor with another character ? In the below example , after I typed the following line, I realized that I meant 7013 and not 2013. So I move the cursor to the left and keep it on top of 2 (of 2013) and I want... (7 Replies)
Discussion started by: kraljic
7 Replies

2. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Shell Programming and Scripting

Cp command works on command line but not in bash

The below command moves all the .vcf files into the directory. cp /home/cmccabe/Desktop/test/vcf/overall/stats/*.vcf /home/cmccabe/Desktop/NGS/annovar When I use a bash wrapper the target.txt gets created but the text files do not get copied. All the paths are the same, but not sure why... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

How to Force command substitution evaluation in bash?

OK, I'm striving to abide by all the rules this time. Here is a fragment of my windows10/cygwin64/bash script: export BUPLOG=$(BackupRecords --log "$src") robocopy $(BackupRecords -mrbd "$src" --path "$src") $(BackupRecords --appSwitches "$src") "$src" "$dst" $(BackupRecords --fileSwitches... (15 Replies)
Discussion started by: siegfried
15 Replies

5. Shell Programming and Scripting

How to Force command substitution evaluation in bash?

OK, I'm striving to abide by all the rules this time. Here is a fragment of my windows10/cygwin64/bash script: export BUPLOG=$(BackupRecords --log "$src") robocopy $(BackupRecords -mrbd "$src" --path "$src") $(BackupRecords --appSwitches "$src") "$src" "$dst" $(BackupRecords --fileSwitches... (0 Replies)
Discussion started by: siegfried
0 Replies

6. Shell Programming and Scripting

Bash - Loading a command's output line by line into an array

I have been trying this a lot of different ways and haven't found too much online. Here's what I've got so far: j=0 declare -a first zero=(`cat $tmpfile`) for i in "${zero}" do command $i >> "${first}" ... (4 Replies)
Discussion started by: Azrael
4 Replies

7. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. AIX

Typing "bash" at the command line spawns two bash processes

Server: IBM p770 OS: AIX 6.1 TL5 SP1 When one of our develoeprs types "bash" on the command line to switch shells, it hangs. For some reason, two bash processes are created....the first bash process spawns a second bash process in the same console, causing a hang. Anyone have any idea what... (2 Replies)
Discussion started by: wjssj
2 Replies

9. Shell Programming and Scripting

[bash] command line substitution with environmental variables

Hi, I'm using an array that contains compiler FLAGS that need to be executed either before ./configure or after the main 'make' command. example of array containing compiler flags. ------------------------------------------------- FLAGS="CFLAGS=\"-arch x86_64 -g -Os -pipe... (7 Replies)
Discussion started by: ASGR
7 Replies
Login or Register to Ask a Question