- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- about read from command line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 02:29 AM
тАО12-28-2004 02:29 AM
about read from command line
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 02:41 AM
тАО12-28-2004 02:41 AM
Re: about read from command line
#! /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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 02:44 AM
тАО12-28-2004 02:44 AM
Re: about read from command line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 02:59 AM
тАО12-28-2004 02:59 AM
Re: about read from command line
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 03:35 AM
тАО12-28-2004 03:35 AM
Re: about read from command line
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 03:56 AM
тАО12-28-2004 03:56 AM
Re: about read from command line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 04:04 AM
тАО12-28-2004 04:04 AM
Re: about read from command line
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 04:18 AM
тАО12-28-2004 04:18 AM
Re: about read from command line
I tryed it. and it work now
thankx again
kamal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2004 04:23 AM
тАО12-28-2004 04:23 AM
Re: about read from command line
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,