Operating System - HP-UX
1752618 Members
4411 Online
108788 Solutions
New Discussion юеВ

about read from command line

 
kamal_15
Regular Advisor

about read from command line

Dear all
iam new in the shell script.

I write a sample script,with read command to read the input from command line.

**********
read a
export a
echo $a
**********

when i run this script ,the script wating to pass the value to it.
when i type test the script output is test.
but if i want to pass a variable?
example:
export x=/home/test
when i run a script.i want to type in the input $x.
but the read take this value($x) as a string.
is there any way to pass a variable($x) to the (read a )command in my script .? and ouptput from script be /home/test NOT $x.??
please help.
thankx.
8 REPLIES 8
Thierry Poels_1
Honored Contributor

Re: about read from command line

hi,

#! /bin/ksh
read atmp
export a=$(eval echo "$atmp")
echo $a

This should work, but why not making thing simpler:

#!/ bin/ksh
echo $1

and supply the variable on the command line:
ksh yourscript $x


regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Chris Vail
Honored Contributor

Re: about read from command line

I'm a little confused about what you're asking. The read command is there for operator input but there are other uses too.

In your example, whatever your operater replied to the "read a" command is stored in the variable a. You can use any word (real or imagined) instead of "a". I have an standard I hold myself to, in that all variables are upper case. So instead of "read a", my script would show "read A". However, my variables are better defined. Instead of "A", I'd use "DIRECTORY" if the script was written expecting a directory, or "FILENAME" if it was expecting a file name.
Note that, in order to make your script relatively idiot-proof, you'll have to do some sort of testing on the variable. For example:
read DIRECTORY
if test -d "$DIRECTORY"
then
do something
else
echo "$DIRECTORY doesn't exist"
fi
Testing for incorrect operator input can take up to 90% of your script. I once wrote a 200 line script, only 5 of which actually did something. The other 195 lines were either comments or testd to ensure that the data entered was valid.

Chris
kamal_15
Regular Advisor

Re: about read from command line

Dear Chris Vail
thankx for answer.
in your sample script (read DIRECTORY).

if you pass (/home/test)as a value of this directory when u run your script.is same when you pass $x ? where x=/home/test.?

that is my question?
Bharat Katkar
Honored Contributor

Re: about read from command line

Hi Kamal,
I hope i understood your question.
See suppose your script is like this

script01.ksh
--------------------------
#!/usr/bin/ksh
echo "Enter the value for Variable VAR1: \n"
read VAR1
VAR2=$1

echo "Variable VAR1 is $VAR1 \n"
echo "Variable VAR2 is $VAR2 \n"
---------------------------

Now when you execute this scirpt as:

# ./script01 myvalue2
Enter the value for Variable VAR1:


Variable VAR1 is myvalue1
Variable VAR2 is myvalue2

#

I hope this is what you were asking.
Parameter passed to the script is assigned to VAR2 using $1 and directly to VAR1 using read statement.

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
kamal_15
Regular Advisor

Re: about read from command line

thankx all

i fell no body can understand me.
maybe i can't explain good.

simply

read statment : take values passed to it from command line as a string.
Ex:
**********
read VAR1
echo $VAR1
**********

when i type --> /home/kamal/test
the response is /home/kamal/test

when i type --> $k (where k=/home/kamal/test)
the response is $k

i meaning
read statment
can't interpret $k to /home/kamal/test.

my question
is there any way to solve that?
thank u


Paul Eadington_1
Valued Contributor

Re: about read from command line

Thierry pointed it out earlier.

Do the following in your script.

read VAR1
export a=$(eval echo "$VAR1")
echo $a

Save that.

Export k to /home/test then run your script and give it $k.

I've just tried this and it worked fine here.
I had hair .. then I got into Unix
kamal_15
Regular Advisor

Re: about read from command line

thankx for all

I tryed it. and it work now
thankx again

kamal
Bharat Katkar
Honored Contributor

Re: about read from command line

Yes as paul said it works. Only thing i can point out is variable k must export first.
This is how it worked:


# export k=/home/username
# echo $k
/home/username
#
# more scr1

#!/usr/bin/ksh
read VAR1
export a=$(eval echo "$VAR1")
echo $a

#
# ./scr1
$k
/home/username
#
#
#

Regards,
You need to know a lot to actually know how little you know