How to use a function? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

How to use a function?

UNIX for Dummies Questions & Answers


Tags
arguments, functions

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-14-2012
Vitaly Vitaly is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 14 November 2012, 11:43 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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
fi


Last edited by Scrutinizer; 11-15-2012 at 12:57 AM.. Reason: code tags
Sponsored Links
    #2  
Old 11-14-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 22 August 2014, 7:46 AM EDT
Location: India
Posts: 1,822
Thanks: 24
Thanked 523 Times in 502 Posts
What error are you seeing? Please post the error message.
Sponsored Links
    #3  
Old 11-14-2012
Vitaly Vitaly is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 14 November 2012, 11:43 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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
fi


Last edited by Scrutinizer; 11-15-2012 at 12:58 AM.. Reason: code tags
    #4  
Old 11-15-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 August 2014, 1:30 PM EDT
Location: Aachen, Germany
Posts: 4,139
Thanks: 67
Thanked 986 Times in 934 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 02:06 PM.