LinuxQuestions.org
Help answer threads with 0 replies.
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, 09:40 AM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,014

Rep: Reputation: 25
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, 09:49 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
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 09:50 AM.
 
Old 11-01-2011, 10:12 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

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

Original Poster
Rep: Reputation: 25
@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 11:32 AM.
 
Old 11-01-2011, 12:27 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825
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, 12:51 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
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, 01:48 PM   #7
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,014

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

@grail: I appreciate your efforts in encouraging kids to learn themselves.
 
Old 11-01-2011, 01:57 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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, 08:20 PM   #9
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,014

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:41 PM
[SOLVED] Help needed with bash script and zenity. Wizard^^ Programming 4 10-29-2010 11:01 PM
Bash Script - Help needed manya Programming 14 07-22-2010 04:38 AM
Bash script. help needed tazthecat Linux - General 2 09-30-2005 02:54 AM
bash script help needed Henster Linux - General 4 08-21-2005 10:54 AM


All times are GMT -5. The time now is 12:07 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration