views:

35

answers:

2

Hello,

I have a shell script where a user passes different sizes as command line arguments. For each of those sizes, I want to perform some task, then save the output of that task into a .txt file with the size in the name.

  1. How can I take the command line passed and make that part of a stringname for the output file? I save the output file in a directory specified by another command line argument. Perhaps an example will clear it up.

  2. In the foor lop, the i value represents the command line argument I need to use, but $$i doesnt work.

./runMe arg1 arg2 outputDir [size1 size2 size3...]

for ((i=4; i<$#; i++ ))
do
    ping -s $$i google.com >> $outputDir/$$iresults.txt
done

I need to know how to build the $outputDir/$$iresults.txt string. Also, the ping -s $$i doesnt work. Its like I need two levels of replacement. I need to replace the $[$i] inner $i with the value in the loop, like 4 for ex, making it $4. Then replace $4 with the 4th command line argument!

Any help would be greatly appreciated.

Thanks!

A: 

Indirection uses the ! substitution prefix:

echo "${!i}"

But you should be using a bare for after shifting the earlier ones out:

shift
shift
for f
do
  echo "$f"
done
Ignacio Vazquez-Abrams
Ok I understand the shiffting, but how should I use it to build the string?
NewShellScripter
The shifting does not build the string. The for loop builds the string.
Ignacio Vazquez-Abrams
A: 
for ARG in $@; do
  $COMMAND  > ${ARG}.txt
done
wilhelmtell
I dont really understand the $COMMAND part?
NewShellScripter
And how would I concatenate a string with the ${ARG}.txt like: {ARG}results.txt
NewShellScripter
`$COMMAND` is your command. Substitute it with anything at all: for example, `ping -s ...`. As for concatenation: yes, `${ARG}results.txt` will do. Given argument 23 it produces 23results.txt.
wilhelmtell
You might want to manipulate the arguments so they doesn't have invalid or unintuitive characters. For example: `ARG=$(echo -n '$ARG' |sed 's/[ \t]/_/g')`
wilhelmtell