|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to use a function?
I am trying to use a function in my script. I'm not quite sure how to complete it. This is my assignment: The first version of hw12.sh should display the usage message "usage: $0 argument", "Enter one or more arguments." when no arguments are on the command line. $ hw12.sh usage: hw12.sh argument Enter one or more arguments. - The second version of hw12.sh should display the message "There were $# arguments on the command line" if there were 1 or more arguments on the command line. Here is an example. $ hw12.sh abc 123 There were 2 arguments on the command line. - The final version of hw12.sh has the following change. Display the usage message from a function. Here is an example. $ hw12.sh hello hw12.sh There were 2 arguments on the command line. $ hw12.sh usage: hw12.sh argument Enter one or more arguments. This is my code that wrote but it says i have a syntax error any help would be appreciated. Thank you Code:
if [ $# -eq 0 ]
then echo "usage: $0 argument"
echo "Enter 1 or more arguments"
else "There were $# arguments on the command line"
exit 1
fiLast edited by Scrutinizer; 11-15-2012 at 12:57 AM.. Reason: code tags |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
What error are you seeing? Please post the error message.
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
I changed my code
As you can see i must use "Usage:" as my function so i changed my code to fullfill the assignment but it doesn't seem its working correctly. I'm new to unix to the whole using function is confusing. This is the new code im trying in my script: Code:
#Vitaly Kuzyanov
usage()
{
[ $# -eq 0 ]
echo "usage: $0 argument"
}
if usage
then echo "Enter 1 or more arguments"
else "There were $# arguments on the command line"
exit 1
fiLast edited by Scrutinizer; 11-15-2012 at 12:58 AM.. Reason: code tags |
|
#4
|
|||
|
|||
|
You are missing the
echo in the else branch.
On top, a function will always have a complete set of parameters of its own, so, in your case, usage will always have zero parameters, regardless of how many parameters your script has gotten. Do input error testing in the body of the script, then call respective error handlers/functions. |
| Sponsored Links | ||
|
![]() |
| Tags |
| arguments, functions |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to step in one function after the function be executed in gdb? | 915086731 | Programming | 4 | 10-31-2011 04:22 AM |
| SHELL SCRIPT Function Calling Another Function Please Help... | omkar.sonawane | Shell Programming and Scripting | 2 | 04-13-2010 10:20 AM |
| Function pointer to inline function ? | emitrax | Programming | 11 | 10-01-2009 03:59 AM |
| Return a value from called function to the calling function | mvictorvijayan | Shell Programming and Scripting | 1 | 09-14-2009 04:19 AM |
| Passing global variable to a function which is called by another function | sars | Shell Programming and Scripting | 4 | 06-30-2008 11:39 AM |
|
|