LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script help needed (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-help-needed-911256/)

rng 11-01-2011 08:40 AM

Bash script help needed
 
How can I make a script do the following:

Script should check the number of arguments given on commandline and if they are less than 2, it should echo a message and exit.

I suspect it is easy but I am a newbie in this. Thanks for your help.

MensaWater 11-01-2011 08:49 AM

Code:

#!/bin/bash
ARGS=$#
if [ $ARGS -lt 2 ]
then echo Hey dummy you need at least two arguments.  You only entred $ARGS.
fi

$# is a "Special Parameter" - if you type "man bash" and search for Special Parameters you'll see there are several. This particular one gives a count of arguments.

ARGS is arbitrary - you could call the variable anything you wanted (e.g. ralph) then use it throughout the script (e.g. $ralph instead of $ARGS)

Note it is important you set the ARGS (or ralph or whatever) variable before doing much else in the script as other things within the script might set their own arguments so you'd get a false count.

The [ ] is the same as "test". If you type "man test" you'll see many things you can do with this. Note the use of "-lt" which is "less than".

grail 11-01-2011 09:12 AM

Looks suspiciously like homework.

rng 11-01-2011 10:30 AM

@mensawater: Thanks for your help. Can you suggest some good site for learning bash programming.

@grail (senior member): You can check my other posts- I am beyond the homework age but trying to make linux as my main OS.

grail 11-01-2011 11:27 AM

Fair enough. I will take you at face value then and suggest the following sites:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex

MensaWater 11-01-2011 11:51 AM

For many things if you do a web search with the item and "tutorial" you'll get lots of good hits.

For example doing a search for Bash Scripting Tutorial came up with one of the links above (which says advanced) and the one below which I'd think would be more tailored to newbies:

http://linuxconfig.org/Bash_scripting_Tutorial

rng 11-01-2011 12:48 PM

Thanks for the links.

@grail: I appreciate your efforts in encouraging kids to learn themselves.

David the H. 11-01-2011 12:57 PM

The wooledge BashGuide has the best explanation of general scripting concepts I've yet found. linuxcommand.org comes in just behind it in usefulness, due to its more hands-on, by-example, approach.

http://mywiki.wooledge.org/BashGuide
http://www.linuxcommand.org/index.php
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start

The rest are good references to look at, when you have the time.

rng 11-01-2011 07:20 PM

Thanks for the links.


All times are GMT -5. The time now is 10:57 AM.