LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-12-2012, 03:20 PM   #1
ahmooody
LQ Newbie
 
Registered: Mar 2012
Posts: 9

Rep: Reputation: Disabled
small bash script for learning perpose


hello dears
i need to make small bash script if i pass an argument for it like yes it is out put give me word no
and if i pass argument no it is out put is YES
if i pass no words it is out put " ERROR"
if i pass any other letters from the keyboard it is not out put any thing just blank
this is just script to know and understand if conditioning
i hope if there will be some one can help me
and i need some books or vedios explain bash scripting for linux and how to understand looping , functions, etc..........
 
Old 04-12-2012, 03:34 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Well this looks like a homework requirement. I will give you some advice and point you in the right direction but we aren't going to write it for you.


Arguements passed on the command line are read by bash as the variables $1, $2, $3 and so on for each argument. The $1 is the first argument, $2 is the second and so on.

So you will want to look at doing a test statement to test your argument. Do some searching for "bash if statements" and start looking at their syntax. That should help get you to where you want to be. Check out http://tldp.org/HOWTO/Bash-Prog-Intr...O-6.html#ss6.2 and find yourself some good bash scripting tutorial/reference sites and bookmark them.
 
Old 04-12-2012, 04:56 PM   #3
ahmooody
LQ Newbie
 
Registered: Mar 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
ok i did that

#!/bin/bash
all="yes"
none="no"
echo please enter any argument
read arg
if [ $arg = ""]
then
echo "error"
elif [ "$arg" = "$yes" ]
then
echo "no"
elif [ "$arg" = "$no" ]
then
echo "yes"
else
echo " "
fi

but i got that error
-bash: [: no: unary operator expected

and i need to know how to solve that error
 
Old 04-12-2012, 05:14 PM   #4
kindofabuzz
Member
 
Registered: Mar 2010
Location: There
Distribution: Linux Mint 17.1
Posts: 237

Rep: Reputation: 45
http://linuxcommand.org/wss0100.php
 
Old 04-12-2012, 05:23 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,454

Rep: Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052
Use the -z flag to check for an empty string:
Code:
if [ -z $arg ]
then
echo "error"
Also, you're treating yes and no as if they're variables:
Code:
elif [ "$arg" = "$yes" ]
elif [ "$arg" = "$no" ]
There is no variable "yes" or "no". You can either compare against the strings "yes" and "no" by removing the $, or you can use your "all" and "none" variables that you declared earlier for the test:
Code:
elif [ "$arg" = "yes" ]
elif [ "$arg" = "no" ]
or
Code:
elif [ "$arg" = "$all" ]
elif [ "$arg" = "$none" ]

Last edited by suicidaleggroll; 04-12-2012 at 05:25 PM.
 
Old 04-12-2012, 05:51 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Note that a space is reqd on each side of [ & ] chars; in fact [[ ]] is recommended http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 04-12-2012, 06:19 PM   #7
TommyC7
Member
 
Registered: Mar 2012
Distribution: Slackware, CentOS, OpenBSD, FreeBSD
Posts: 459

Rep: Reputation: Disabled
I'd also like to add this link:
http://mywiki.wooledge.org/BashGuide

That's the official BashGuide the bash IRC channel (#bash) recommends.
 
Old 04-13-2012, 01:27 AM   #8
ahmooody
LQ Newbie
 
Registered: Mar 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
tahnks for all

thanks alot now it is working fine and i got the concept
second thank for all who posted sites for learning the Bash-Scripts
i apreciate you exeretd efforts
but if there is any media ( mean videos it will be better than books )
thanks again for all
 
  


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] small bash script m3phisto Linux - Newbie 7 04-26-2011 04:38 AM
Very small bash script enemorales Programming 8 02-25-2005 02:47 AM
small bash script issue zoomzoom Linux - General 7 06-08-2004 06:33 PM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM


All times are GMT -5. The time now is 05:52 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