Arguments in variable vs direct string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arguments in variable vs direct string
# 8  
Old 11-10-2016
Hello!
Thank You all for the answers.
The best solution for now is:
test.sh:
Code:
#!/bin/sh
my_args="1 2 3 '4 5' 6"
echo "Count of arguments when using my_args:"
echo "./arguments_count.sh $my_args" > ./my_args.sh    #put whole command into a file 
sh ./my_args.sh                        #run the file
echo "Count of arguments when using direct string:"
echo "./arguments_count.sh 1 2 3 '4 5' 6" > ./my_string.sh    #put whole command into a file
sh ./my_string.sh                        #run the file

# 9  
Old 11-10-2016
That is double parsing/interpretation. It is equivalent to
Code:
eval /.arguments_count.sh "$my_args"

Which has the same security concerns unless you are in control of the input..




----
Quote:
Originally Posted by bakunin
Code:
$ my_args="a b c \'d e\' f g"
$ arguments_count.sh $my_args

Hi Bakunin, this would offer no different result, since the since the unquoted variable expansion of my_args would still be subjected to field splitting into 7 arguments:

Code:
$ printf "%s\n" $my_args
a
b
c
\'d
e\'
f
g

within the same parsing run, there is no way to recover the intended quoted fields from the string contained in the variable, if it is subjected to field splitting with the standard IFS.

Last edited by Scrutinizer; 11-10-2016 at 08:08 AM..
# 10  
Old 11-10-2016
Quote:
Originally Posted by Scrutinizer
That is double interpretation. It is equivalent to
Code:
eval /.arguments_count.sh "$my_args"

Which has the same security concerns unless you are in control of the input..
Thank You. Eval also solves the problem.

Quote:

----

Hi Bakunin, this would offer no different result, since the since the unquoted variable expansion of my_args would still be subjected to field splitting into 7 arguments:

Code:
$ printf "%s\n" $my_args
a
b
c
\'d
e\'
f
g


Last edited by RudiC; 11-10-2016 at 08:22 AM..
# 11  
Old 11-10-2016
Hi Break_da_funk. Yes that could solve your problem, but if at all possible, such an approach is best avoided, given the security concerns that come with the use of eval or double shell interpretation ..
# 12  
Old 11-10-2016
Quote:
Originally Posted by Scrutinizer
Hi Break_da_funk. Yes that could solve your problem, but if at all possible, such an approach is best avoided, given the security concerns that come with the use of eval or double shell interpretation ..
I will remeber about this Smilie
# 13  
Old 11-10-2016
If you can possibly avoid it, just don't do that - these are all ugly hacks to accommodate something structured inside-out and backwards. But if you absolutely have to, xargs can handle them.

People loathe xargs for processing quotes instead of handling them raw, but that's actually useful here. It will handle quotes but will ignore shell syntax -- which makes it much safer than eval.

Code:
UGLYMESS="a b 'c d' e"
OLDIFS="$IFS"

# Split unquoted strings on newlines and ONLY newlines.
IFS="
"

# xargs printf "%s\n" will print every separate argument or quoted section on its own newline.
# You could set $1=a, $2=b, $3="c d", $4=e
set -- $(echo "$UGLYMESS" | xargs printf "%s\n" )
# ...or put them into an array like this:
ARR=( $(echo "$UGLYMESS" | xargs printf "%s\n" ) )

# Now that we've stored them already split, we can restore IFS.
IFS="$OLDIFS"


Last edited by Corona688; 12-02-2016 at 11:33 AM.. Reason: Typo, missing ) in array
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

ksh parsing arguments in a string rather than from the cmdln

Hi. I have a piece of code that reads and parses command line options. I'd like to alter it slightly to read from a string (that's set elsewhere in the script) rather than directly from the command line (arg). Can somebody show me how to do this? Many thanks. My code is as follows: typeset... (6 Replies)
Discussion started by: user052009
6 Replies

3. Shell Programming and Scripting

Processing arguments in a string

Hi The following code works when reading the arguments from the command line but fails when I try to read from a string. So this works while ; do case $1 in -dbversion) if '`" ]; then { echo "ERROR: missing value for '$1' (seen '$2')"; usage; exit 1; } else { shift;... (6 Replies)
Discussion started by: user052009
6 Replies

4. Shell Programming and Scripting

Manipulating sed Direct Input to Direct Output

Hi guys, been scratching round the forums and my mountain of resources. Maybe I havn't read deep enough My question is not how sed edits a stream and outputs it to a file, rather something like this below: I have a .txt with some text in it :rolleyes: abc:123:xyz 123:abc:987... (7 Replies)
Discussion started by: the0nion
7 Replies

5. Shell Programming and Scripting

Removing command line arguments from string list

I am passing a list of strings $list and want to remove all entries with --shift=number, --sort=number/number/..., --group=number/number/... Also are removed whether upper or lower case letters are used For example the following will all be deleted from the list --shift=12 --shift=2324... (7 Replies)
Discussion started by: kristinu
7 Replies

6. Shell Programming and Scripting

string manipulation in arguments

Hi all, I have a requirement where I am taking the first argument as argument name and storing the second argument in argument name as value. Thanks to ppl here, i learnt to do it.:p while ( $1 != "" ) set arg = $1 shift set val = "$1" echo "set... (2 Replies)
Discussion started by: animesharma
2 Replies

7. Shell Programming and Scripting

Print arguments with the help of variable

Let's say I want to print the arguments $4 till $#, how can I do this? $# contains the number of arguments $@ contain all the arguments as string What i need is something like for i in $4_till_$#; do #do something with $i convert $i ~/$output done The first 3 arguments are used as options... (6 Replies)
Discussion started by: hakermania
6 Replies

8. Shell Programming and Scripting

How to make bash wrapper for java/groovy program with variable length arguments lists?

The following bash script does not work because the java/groovy code always thinks there are four arguments even if there are only 1 or 2. As you can see from my hideous backslashes, I am using cygwin bash on windows. export... (1 Reply)
Discussion started by: siegfried
1 Replies

9. Shell Programming and Scripting

How to call arguments with variable in a script??

Hello, I was wondering if it were possible to call arguments passed to a script using a variable. For example: sh script.sh yes no good bad x=$# while do echo (last argument, then second last etc until first argument) let x=($x-1) done should print out bad good no (4 Replies)
Discussion started by: VanK
4 Replies

10. Shell Programming and Scripting

Joining string arguments

Hi, how can I join given arguments (not starting from the first one) to form one string, each argument separated by a space. For example, out of 5 given arguments, I'll like to start joining from the 3rd to the last. In python there exists something like ' '.join(sys.argv) and it starts joining... (5 Replies)
Discussion started by: plhelpme
5 Replies
Login or Register to Ask a Question