DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 9th July 2008
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
 
Join Date: May 2008
Posts: 41
Thanked 0 Times in 0 Posts
Default shell script with input

Hi all,

Here is my shell script
Code:
#!/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
Code:
./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
Code:
./script.sh first last
and that I get same output "Your name is:first and your surname is:last" .. Thank you for your answers
Reply With Quote
  #2   (View Single Post)  
Old 9th July 2008
dk_netsvil dk_netsvil is offline
Real Name: Devon
Fdisk Soldier
 
Join Date: May 2008
Location: New York
Posts: 75
Thanked 7 Times in 7 Posts
Default

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.

Last edited by dk_netsvil; 9th July 2008 at 05:21 PM.
Reply With Quote
  #3   (View Single Post)  
Old 9th July 2008
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
 
Join Date: May 2008
Posts: 41
Thanked 0 Times in 0 Posts
Default

Thank you for your post
Reply With Quote
  #4   (View Single Post)  
Old 9th July 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,062
Thanked 198 Times in 156 Posts
Default

Also see getopt(1), although I'm not sure how portable this is.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #5   (View Single Post)  
Old 9th July 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

/bin/sh provides a builtin getopts command as a replacement for the getopt program. In FreeBSDs manual page for sh(1) the following information is provided on getopts:

Code:
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.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #6   (View Single Post)  
Old 13th July 2008
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
 
Join Date: May 2008
Posts: 41
Thanked 0 Times in 0 Posts
Default

Thank you for your answers
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script with zenity bsdnewbie999 OpenBSD General 5 24th April 2009 02:37 AM
shell script-start another process bsdnewbie999 Programming 2 23rd April 2009 07:48 PM
shell script compare md5 sum bsdnewbie999 Programming 1 11th April 2009 02:20 PM
incrementing within a shell script? spiderpig Programming 5 29th September 2008 08:12 PM
Shell Script. bsdnewbie999 Programming 21 15th July 2008 07:54 AM


All times are GMT. The time now is 08:51 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick