LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-27-2012, 11:23 PM   #1
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Rep: Reputation: Disabled
Unhappy Need help in bash scripting in Linux


Hi everyone,

I am new to linux and trying to learn scripting in linux.
My problem in scripting is.....I am not able to use if n else...can anyone pls help me....

Below is the one of the scripting which I wrote and getting errors.

To find greatest of 3 numbers.
-------------------------
#!/bin/bash
read n1
read n2
read n3
if [$n1 -gt $n2] && [$n1 -gt $n3]
echo "$n1 is greatest number"
else
if [$n2 -gt $n1] &&[$n2 -gt $n3]
echo "$n2 is greatest number"
else
echo "$n3 is greatest number"
fi
---------------------------------
 
Old 03-27-2012, 11:32 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
The structure is "if+then, [elif+then|else], fi" .. also don't forget to leave space between your brackets and your variables
Code:
#!/bin/bash

read n1
read n2
read n3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
  echo "$n1 is greatest number"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ] 
then
  echo "$n2 is greatest number"
else
  echo "$n3 is greatest number"
fi
 
Old 03-27-2012, 11:35 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
You should really have said what errors you were getting.
Try adding 'set -xv' thus to debug
Code:
#!/bin/bash
set -xv

read n1
read n2
read n3
if [$n1 -gt $n2] && [$n1 -gt $n3]
echo "$n1 is greatest number"
else
if [$n2 -gt $n1] &&[$n2 -gt $n3]
echo "$n2 is greatest number"
else
echo "$n3 is greatest number"
fi
Basically, you need spaces around such thyings as '[', ']' etc.
Each if..then.. must end with 'fi', you also want to consider elif

my version
Code:
#!/bin/bash
read n1
read n2
read n3
if [[ $n1 -gt $n2 ]] && [[ $n1 -gt $n3 ]]
then
    echo "$n1 is greatest number"
elif [[ $n2 -gt $n1 ]] && [[ $n2 -gt $n3 ]]
then
    echo "$n2 is greatest number"
else
    echo "$n3 is greatest number"
fi
bookmark/read
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 03-27-2012, 11:38 PM   #4
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
hi kbp,

ooh ok.
Thanks for helping.......Now I got it.
 
Old 03-27-2012, 11:44 PM   #5
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
Hey,

I am having one more doubt.....regarding hidden files in linux.
If i go with "ls -a" I can see all the hidden files....my doubt is

1.who creates those hidden files
2.what data will be inside those hidden files.
----------------------------------

In my linux Redhat 6.2 server edition....if I go with "ls -a " I can see 2 hidden files with dark blue color....2 files are
1.single dot[.]
2.double dot[..]

So please explain me about the hidden files....

Thanks in advance.....
 
Old 03-27-2012, 11:56 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
These represent the current directory (.) and the parent directory (..), if you try 'cd .' or cd ..' you'll see what I mean.
 
Old 03-27-2012, 11:58 PM   #7
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
yah what you said is rite.

But when ever i create a DIR ...I can see 2 files created automatically......why those are created???
 
Old 03-28-2012, 01:01 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,772

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
. and .. are always created, those are by default "available", the system needs them to be able to walk in the directory tree.
 
Old 03-28-2012, 01:12 AM   #9
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
hi pan,

Can I know what is directory tree???????
 
Old 03-28-2012, 01:49 AM   #10
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
Hi everybody,

Trouble with scripting.......
Below is the script which I wrote but giving the error .i : COMMAND NOT FOUND IN LINE 5
------------------------------------------
i=5
while test $i != 0
do
echo "$i"
i=`expr $i - 1`
done
----------------------------------------
PLEASE HELP ME
 
Old 03-28-2012, 03:00 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,772

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
you have a root directory represented by a /. All of the subdirs inside are named as /dir1, /dir2, /dir_whatever.
All of these subdirs can have subdirs, like /dir1/subdir1 /dir5/subdir9 and so on. This structure can be represented as a tree - beginning from its root - so usually it is called directory tree. All of the available stuff in a linux can be found in a directory, somewhere in this directory tree.

This script is working (for me).
 
Old 03-28-2012, 04:38 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think you need to go back and look at your notes on testing strings and testing numbers in bash. May I also add, (()) is preferred over [[]] when doing arithmetic tests as standard
mathematical symbols may be used, ie. like <, >, etc
 
Old 03-28-2012, 04:42 AM   #13
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
hey I need one more script
That will print the wishes like gud morning,eveng n night.....whenever system gets booted.......?
it should print automatically
 
Old 04-06-2012, 03:25 PM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,604

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by ravi_nandula View Post
hey I need one more script
That will print the wishes like gud morning,eveng n night.....whenever system gets booted.......?
it should print automatically
If you need a script, then WRITE IT YOURSELF. You've been pointed to bash scripting guides before, and can easily find lots more via Google. We'll be glad to HELP you, but you need to show some effort on your part. Post what you've written/done, and where you're stuck.

Otherwise, do your own work.
 
  


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
Linux Bash Scripting Background thunder44 Programming 1 02-04-2008 05:32 AM
Linux > #bash Scripting Help Tommis Linux - General 10 09-16-2007 06:02 PM
Linux Shell Scripting using BASH Help! fooforon Programming 5 02-05-2004 09:16 AM

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

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