View Full Version : shell script with input
c0mrade
07-09-2008, 05:27 PM
Hi all,
Here is my shell script
#!/bin/sh
echo "What is your name"
read name
echo "What is your surname"
read surname
echo "Your name is:${name} and your surname is:${surname}"
How can I modify this script so that I input those two variables from console?Lets say my name is "first" and my surname is "last" . So when I run my script ./script.sh and input first and last I get "Your name is:first and your surname is:last". Is it possible to modify the script so that it runs like this ./script.sh first last and that I get same output "Your name is:first and your surname is:last" .. Thank you for your answers
dk_netsvil
07-09-2008, 06:18 PM
Ok, so you have a script called /bin/script.sh - when you follow that with arguments like First and Last there are already variables built in to allow you to use them.
/bin/script.sh First Last is going to store the value of First in $1 and Last in $2 - so if you script contained something like:
#!/bin/sh
echo "Your name is: $1 and your surname is: $2"
you'd be closer to solving this problem.
EDIT:
Actually, this is pretty ugly - there's a fairly good introductory tutorial over here (http://steve-parker.org/sh/sh.shtml).
c0mrade
07-09-2008, 07:16 PM
Thank you for your post
Carpetsmoker
07-09-2008, 07:22 PM
Also see getopt, although I'm not sure how portable this is.
TerryP
07-09-2008, 11:10 PM
/bin/sh provides a builtin getopts command as a replacement for the getopt program. In FreeBSDs manual page for sh the following information is provided on getopts:
getopts optstring var
The POSIX getopts command. The getopts command deprecates the
older getopt(1) command. The first argument should be a series
of letters, each possibly followed by a colon which indicates
that the option takes an argument. The specified variable is set
to the parsed option. The index of the next argument is placed
into the shell variable OPTIND. If an option takes an argument,
it is placed into the shell variable OPTARG. If an invalid
option is encountered, var is set to `?'. It returns a false
value (1) when it encounters the end of the options.
So I assume it is required by one of the POSIX standards defining the behavior of /bin/sh, either way the manual says this implementation was based on SVR4.
OpenBSDs version of the public domain Korn Shell also implements it, the GNU Bourne Again (bash) shell should too.
----
getopts is only useful in your shell scripts if you want to make a script behave like normal unix programs. For example,
wc -l file
prints only the number of lines in 'file', where the wc program usually prints lines, words, and bytes.
The getopt manual page that Carpetsmoker posted explains how to do things like that in a script.
the difference in using the external getopt and the internal getopts command is basically an 's', technical advantages aside.
c0mrade
07-13-2008, 05:33 AM
Thank you for your answers
vBulletin® v3.7.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.