I have created a script that prompts the user to enter three variables that are seperated by a space as the delimiter.
It then performs a command 3 seperate times for each variable entered.
I want the script to llow the user to enter as many variables as they may like and the script to perform the commandwith each variable entered, each time.
Hello, I couldn't help wonder how I would do it in bash. This one lets the user enter all arguments first, one line at a time, then operates on the arguments.
It is more verbose than it should be, only to show usage of arrays... replace "echo "executing command"..." etc with Your actual command.
#!/bin/bash
echo Enter arguments, just press enter when finished.
cnt=0
while [ true ]; do
echo -n "Enter argument $(($cnt+1)): "
read x[$cnt]
[ "${x[$cnt]}" == "" ] && break
cnt=$(($cnt+1))
done
echo Processing $cnt arguments
cnt=0
while (($cnt < ${#x[@]}-1 ))
do
echo "executing command on argument $(($cnt+1)) which is: ${x[cnt++]}"
done
#echo ${x[@]}
OK, I should provide more info. first I am running in ksh.
I have a file with one line of a series of words that are three letters long, using s space as the delimiter.
I want to run a script which will retrieve those words and assign them to a variable, which i can run with my command in a loop until each word has been used, individually each time in the command until each three letter word has been used.
the file looks like this:
AIN OTB ALK SJA ALS HAH INA (etc..)
I want to run the command with all three letter word individually.
The commands i want to perform is already designed. I just need to insert the words in the command in a loop to do it each tmie. note the file may change with the count of words but it will always be three letter words. therefore it has to count the words each time it is run. i was thinking I would cat the file, pipe it to a wc -w and use that as the count for how many time to perform the loop. (I.e. wordct="cat <filename> | wc -w") with this variable I have a defined count of how many words each time.