trying to create a script with multiple variables...


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers trying to create a script with multiple variables...
# 1  
Old 03-20-2008
trying to create a script with multiple variables...

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.

the scripts promt starts as
example of script:

read AP1 AP2 AP3

{command + $AP1}
{Command + $AP2}
{Command + $AP3}



they are later used as $AP1 $AP2 $AP3

How can I change this with one read command that will use a loop to repeat the command to each variable entered?

Thank you for you help in advance.

italy87
# 2  
Old 03-20-2008
use an array - note the "command" line will not work, it's an example:
Code:
#!/bin/ksh
echo "enter stuff \c"
read dummy
set -A array $dummy
let i=0

while [[ $i -lt ${#array[*]} ]]
do
     <command> ${arr[i]}
     let i=$i+1
done

# 3  
Old 03-20-2008
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.

Code:
#!/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[@]}

/Lakris
# 4  
Old 03-21-2008
Computer

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.

I hope this helps.
# 5  
Old 03-21-2008
reading them as one variable and the segregating will help

Last edited by raman1605; 03-21-2008 at 03:02 PM.. Reason: Took wrong meaning of the query
# 6  
Old 03-21-2008
any suggestions on how to do that?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

TCL script to capture range of lines and create two independent variables

Hi I am having a code as stated below module abcd( a , b , c ,da , fa, na , ta , ma , ra , ta, la , pa ); input a , b, da ,fa , na , ta , ma; output c , ra ,ta , la ,pa ; wire a , b , da , fa ,na , ta , ma; // MBIST Structures... (1 Reply)
Discussion started by: kshitij
1 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

4. Shell Programming and Scripting

Assigning multiple variables in ksh script

Is it possible to have a user input multiple words in one line and have the script assign each word a variable? I'm stuck please assist. Example using "BILL JOHN SARA JILL" as what the user could type: printf "Enter account names: " BILL JOHN SARA JILL read input (9 Replies)
Discussion started by: seekryts15
9 Replies

5. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

6. Shell Programming and Scripting

Need help to create multiple file using shell script

HI, i created the below script to create the multiple files, iam not getting the required output, Please advice. #!/bin/sh v_date=$1 # argument will come as daymonthyear eg : 151112 v_day=`echo $v_date | cut -c 1-2` v_mon=`echo $v_date | cut -c 3-4` v_year=`echo $v_date | cut -c 5-6`... (4 Replies)
Discussion started by: jagguvarma
4 Replies

7. Shell Programming and Scripting

calling script with multiple variables in second script

Hi All, Just give me an idea on how to do the below logic. 1. I have one master script masterload.sh, the usage of this script is a. masterload.sh FULL BFLF_LOAD.txt b. masterload.sh DELTA TDLD_LOAD.txt c.masterload.sh USER MAS_LOAD.txt FULL , DELTA ,USER are the varaibles based... (1 Reply)
Discussion started by: mora
1 Replies

8. Shell Programming and Scripting

Multiple Variables for BASH script

Hello, I am new to the whole "scripting" thing. Below is the script that I have so far and where i need the Variables to go (VAR#) #!/bin/bash #Sample Script VAR1= echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." read VAR1 case $VAR1 in 1) echo "Pick... (4 Replies)
Discussion started by: eclerget
4 Replies

9. Shell Programming and Scripting

how to create and access a directory in the same script using variables

I have a working script that defines the paths using variables which is used to move a rename files that are being archived. Is there a way to create a directory in the path with the date as the name and then reference it when moving the file to it? Here is what I have tried with no luck.... ... (1 Reply)
Discussion started by: Snickyt0310
1 Replies

10. Shell Programming and Scripting

Pass multiple variables to SQL script

I am trying to close of multiple users in an Oracle database. Each users has records in multiple tables what I need to do is use a script that call each SQL seperately passing either CLI arguments or gathered arguments from the users during run time. ## Accept variable(s) from the command line... (1 Reply)
Discussion started by: jagannatha
1 Replies
Login or Register to Ask a Question