LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2011, 08:40 AM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Rep: Reputation: 47
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.
 
Old 11-01-2011, 08:49 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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".

Last edited by MensaWater; 11-01-2011 at 08:50 AM.
 
Old 11-01-2011, 09:12 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Looks suspiciously like homework.
 
0 members found this post helpful.
Old 11-01-2011, 10:30 AM   #4
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
@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.

Last edited by rng; 11-01-2011 at 10:32 AM.
 
Old 11-01-2011, 11:27 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 11-01-2011, 11:51 AM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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
 
Old 11-01-2011, 12:48 PM   #7
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Thanks for the links.

@grail: I appreciate your efforts in encouraging kids to learn themselves.
 
Old 11-01-2011, 12:57 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 11-01-2011, 07:20 PM   #9
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Thanks for the links.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script help needed anotherunixnoob Programming 9 11-04-2010 04:41 PM
[SOLVED] Help needed with bash script and zenity. Wizard^^ Programming 4 10-29-2010 10:01 PM
Bash Script - Help needed manya Programming 14 07-22-2010 03:38 AM
Bash script. help needed tazthecat Linux - General 2 09-30-2005 01:54 AM
bash script help needed Henster Linux - General 4 08-21-2005 09:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration