Accepting multiple values in a variable at run time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accepting multiple values in a variable at run time
# 1  
Old 09-17-2012
Accepting multiple values in a variable at run time

Hi,

Below is starting entry of my script

Code:
#!/bin/ksh
Usage()
{
	print "Usage: $0 ID OPTION SERVER"
	print "<br>Where :"
	print "<br>Enter your ID into PARAM1, OPTION in the PARAM2 and SERVER in the PARAM3  field"
	print "<br>ID should be a valid ID"
	print "<br>OPTION should be either BOUNCE or STATUS"
	print "<br>SERVER should be a valid server name<br>"
}

ID=`print $1 | sed 's/"//g'`
OPTION=`print $2 | sed 's/"//g'`
SERVER=()
for (( i = 1; i <= 4; i++ ))
do
	SERVER[i]=`print $3 | sed 's/"//g'`
done

typeset -u ID_UPPER
typeset -u OPTION_UPPER
typeset -u SERVER_UPPER
ID_UPPER=${ID}
OPTION_UPPER=${OPTION}
SERVER_UPPER=${SERVER}
TOTALSERVER=`print ${SERVER_UPPER} | wc -w`

The things i am looking for are:
1. Those 3 variables are accepting values at run time. ID and OPTION will have one value at a time but SERVER may have one or more than one values
2. I need to figure out how to hold the values passed by the user in $3 (third variable). The values can be one or more than one. Max no. of servers will be 4.
3. For TOTALSERVER, i need to figure out how many SERVER values user has passed (or no. of values variable SERVER is holding).

Let me know if you need more details
Thanks in advance.

Last edited by Scott; 09-17-2012 at 08:55 PM.. Reason: Code tags
# 2  
Old 09-17-2012
You're doing a bit more work than is needed by using sed and wc; Kshell can do that easily and more efficiently than invoking external processes. Here's an example which does pretty much what you are looking for:

Code:
#!/usr/bin/env ksh

id="${1//\"/}"              # no need to pipe it through sed to strip quotes
option="${2//\"/}"
shift 2
typeset -a server=( $* )   # capture remaining parms into the server list
total_server=${#server[*]}  # no need to pipe it through wc to count; this generates size of the array 
for (( i = 0; i < $total_server; i++ ))
do
    typeset -u server_upper[$i]=${server[$i]}
done

typeset -u option_upper="$option"
typeset -u id_upper="$id"
echo "$option  $option_upper"
echo "$id $id_upper"
echo "$total_server"
echo "${server[0]}"
echo "${server_upper[0]}"
exit

If you are stripping the quotes because someone might enter the command like this:
Code:
command "parm1" "parm2" "server1 server2 server3"

Then you don't need to worry about the quotes -- Kshell will do the right thing and the quotes will be removed by the shell. The example above handles the server list as shown or as
Code:
command p1 p2 server1 server2 server3

(as single tokens, or one quoted token list). It should also handle n servers.

Last edited by agama; 09-17-2012 at 09:02 PM.. Reason: code tag boo-boo
# 3  
Old 09-18-2012
Code:
#!/usr/bin/ksh

id=`print $1 | sed 's/"//g'` 
option=`print $2 | sed 's/"//g'`
shift 2
typeset -a server=( $* )   # capture remaining parms into the server list
total_server=${#server[*]}  # no need to pipe it through wc to count; this generates size of the array
for (( i = 0; i < $total_server; i++ ))
do
    typeset -u server_upper[$i]=${server[$i]}
done

typeset -u option_upper="$option"
typeset -u id_upper="$id"
echo "$option  $option_upper"
echo "$id $id_upper"
echo "$total_server"
echo "${server[0]}"
echo "${server_upper[0]}"
exit

i ran the script as scriptname id bounce serv1 serv2 serv3
but getting syntax error at line 6 i.e. typeset -a server=( $* )
also i tried your strip quotes stuff but didn't work.
Thanks for you help.

Last edited by Franklin52; 09-18-2012 at 05:43 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read multiple values into one variable

Hello everybody, I am trying to assign multiple values from text into a single variable in bash. Source TXT: 1 THIS IS THE ENTITY1 NAME1 2 THIS IS THE ENTITY2 NAME2 3 THIS IS THE ENTITY3 NAME3 while read id entity name do printf "$id" "$entity" "$name" done <... (2 Replies)
Discussion started by: mrcrowley
2 Replies

2. Shell Programming and Scripting

Interpreting multiple values from a variable

Hi, I am writing a shell script which will check the status of a resource in a cluster and then display nicely to a user running the script at command line. Basically the script runs a status command and then pulls certain keywords from the return and then should display a concise status. ... (5 Replies)
Discussion started by: chris01010
5 Replies

3. Shell Programming and Scripting

Variable with multiple values

Hello I need to alter a script to check for a flag file but there are now more than one type of flag file in my directory. Basically if any of these flg files exist then the MASK value should be set? Flag files to be included in assignment to variable e2e_scheduled*.flg COLL_STOP*.flg... (1 Reply)
Discussion started by: andymay
1 Replies

4. Shell Programming and Scripting

Passing multiple run time arguments

the scenario is - If I pass 3 three arguments( run time) , it should list all .txt files from a path to temp file if I pass 2 arguments ( run time) , it should list all .csv files from the same path to another temp file the above scenario should be handled in single code and also I dont know ... (2 Replies)
Discussion started by: Prashanth B
2 Replies

5. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

6. Shell Programming and Scripting

Using same variable in 2 different scripts that run at the same time

Hi, I have two scripts that use the same variable. Say suppose I export that variable in script 1 and try to use it in script 2 where I have declared it locally, what will be the effective value of the variable? Will it take the exported value or the locally declared? example: Script1... (4 Replies)
Discussion started by: PraveenSikamani
4 Replies

7. Ubuntu

run multiple command at the same time in one window terminal using multiplexer

Hi, I would like to ask if someone knows or accomplished this task in the terminal multiplexer in a single window with multiple splitted pane: In the script run multiple command at the same time in diff splitted pane or simulatneously. As an example: I would like to run iptraf, iotop, htop,... (2 Replies)
Discussion started by: jao_madn
2 Replies

8. UNIX for Dummies Questions & Answers

Storing Multiple Values in a Variable

Hi, My code is as below: integer i=7 while ((i <= 5 )); do # test_out is a variable which contains data separated by "^". a= `echo $test_out | cut -d"^" -f$i` echo "$a" (( i = i + 1)); done From the above code, i kept $i after f so that i can capture all the data which is... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

9. Shell Programming and Scripting

how to run multiple process at the same time

Hello guys, Look what im doing: I need to run a process from a SERVER1 to SERVER2, SERVER3 and SERVER4. The shell of the process is in each SERVER (2 to 4) So from SERVER1 i do: for i in SERVER2 SERVER3 SERVER4 do rsh $i ' ./process.sh ' done The problem is: each process.sh... (2 Replies)
Discussion started by: lestat_ecuador
2 Replies

10. UNIX for Advanced & Expert Users

how to set the environment variable at run time

hi, I have one environment variable like path in my system.But in my program i need to change that path .suppose it has a value "config" now i need to chage it as "config1" or something else.i need to use that variable for complete project.It means at first it will use the old path but after... (4 Replies)
Discussion started by: sada@123
4 Replies
Login or Register to Ask a Question