LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2006, 05:19 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Exit


#bin/bash

#This program finds whether a given file exists on your system.

if [$# -ne 1]

then echo "Usage -$0 file-name"

exit 1

fi

if [ -f $1 ]

then
echo "$1 file exist"

else

echo " Sorry, $1 file does not exist"

fi

-----------------------------------------------------------

The above works fine. The first if statement has 'exit 1'. It is not in the second if statement. Why is that? Your comments are welcome.

I know the exit existence of 'exit 0' and 'exit 1'.
The 'exit 0' indicates success whereas 'exit 1' is a failure.
 
Old 07-18-2006, 06:13 AM   #2
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
The first if statement is checking to make sure that the number of arguments passed to the script is exactly 1. If it is not, that code block will be executed. If exactly one argument was not passed to the script, then it reports the proper usage and exits with a fail.

The second if statement checks for the existence of the file. Whether the file exists or not, the script has successfully performed its job, so here "exit 1" would not be appropriate.

Make sense?
 
Old 07-18-2006, 07:26 AM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47

Thanks zaichik for taking time to reply me. You are clever at these things whereas I am groping.
 
Old 07-18-2006, 07:46 AM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Well, it's probably just that I have read something like this:

http://www.tldp.org/LDP/abs/html/index.html

and you haven't yet, that's all. This a great tutorial on bash scripting. I highly recommend it. I still refer to it frequently, since I don't write scripts every day.
 
Old 07-18-2006, 08:12 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could make a change to your script:
Code:
#bin/bash

#This program finds whether a given file exists on your system.
if [$# -ne 1]; then echo "Usage -$0 file-name"
exit 1
fi

if [ -f $1 ]; then
   echo "$1 file exist"
   exit 0
else
   echo " Sorry, $1 file does not exist"
   exit 1
fi
The difference is that I changed the exit code if the file isn't found. This is similar to grep.

Code:
jschiwal@hpamd64:~/temp> cat text
This is the first line.
This is the second line.
Toasters can be fun.
And the last line.
jschiwal@hpamd64:~/temp> grep fun text
Toasters can be fun.
jschiwal@hpamd64:~/temp> echo $?
0
jschiwal@hpamd64:~/temp> grep fund text
jschiwal@hpamd64:~/temp> echo $?
1
jschiwal@hpamd64:~/temp> if grep 'fun' text; then
>   echo "Found it."
> fi
Toasters can be fun.
Found it.
jschiwal@hpamd64:~/temp> if grep 'fund' text; then
> echo found
> else
> echo not found
> fi
not found

Last edited by jschiwal; 07-18-2006 at 08:15 AM.
 
Old 07-18-2006, 03:45 PM   #6
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47

Jschiwal
Thanks for taking time to reply me again.

I don't understand your explanation with grep commands.

Your altered programme worked fine.


I want to know about the word 'Usage'. I am sure it is a reseved word. It is not in the man pages. What is it?


[nissanka@c83-250-110-112 ~]$ man Usage
No manual entry for Usage
[nissanka@c83-250-110-112 ~]$


-------------------------------------------------------

There is a small problem of your program.

I named it 'find2'.


I have a text file called 'poem1.txt'

[nissanka@c83-250-110-112 ~]$ cat poem1.txt
A wise old owl lived in an oak;
The more he saw the less he spoke;
The less he spoke the more he heard:
Why can't we all be like that bird?

EDWARD HERSEY RICHARDS
--------------------------------------

There is a negligible error message. Please read the following:

[nissanka@c83-250-110-112 ~]$ ./find2 poem1.txt
./find2: line 9: [1: command not found
poem1.txt file exist

[What is the problem with the line?]


--------------------------------------------------------------------
[nissanka@c83-250-110-112 ~]$ cat find2
#bin/bash
#This program finds whether a given file exists on your system.
#This is similar to the 'find1' program. There is a small alteration.

if [$# -ne 1]; then
echo "Usage -$0 file-name"
exit 1
fi

if [ -f $1 ]; then
echo "$1 file exist"
exit 0
else
echo " Sorry, $1 file does not exist"
exit 1
fi


-----------------------------------------------------
[nissanka@c83-250-110-112 ~]$

Last edited by Gins; 07-18-2006 at 03:56 PM.
 
Old 07-18-2006, 07:07 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
echo "Usage -$0 file-name"

Usage isn't a command. It is the first word in your message.
The $0 part is replaced by the name of the script.

On this line: if [$# -ne 1]; then
You need to add spaces inside the square brackets: if [ $# -ne 1 ]; then
This is because "[" is an actual command ( see /usr/bin/[ ).

I had simply copied and pasted that line from the original.

Your command checks if the file given in the argument exists. Grep checks if the text given in the argument exists in a file, or the standard input. They perform a similar function. Because grep returns an error (1) if the text pattern wasn't found in the file, it can be used in conditional statements such as "if" or as part of a sequence using "&& or ||".

In your "found2" program, you could use a variable to hold the status and then exit on the last line with "exit $status". Then the "fi" statements will be reached.
Code:
if [ -f $1 ]; then
   echo "$1 file exist"
   status=0
else
   echo " Sorry, $1 file does not exist"
   status=1
fi
exit $status

Last edited by jschiwal; 07-19-2006 at 03:30 AM.
 
Old 07-19-2006, 02:51 PM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47

Thanks jschiwal for the reply. I am very proud of your knowledge of scripting. You are good at trouble shooting. I could never ever figure out the reason for the error message.


You said this is a command. ---> "["

How do I find it out? Not in the man pages.

--------------------------------------------
[nissanka@c83-250-110-112 ~]$ man "["
No manual entry for [
[nissanka@c83-250-110-112 ~]$
--------------------------------------------

Is the word 'script' a reserved word? Not in the man pages.
--------------------------------------------
[nissanka@c83-250-110-112 ~]$ man status
No manual entry for status
[nissanka@c83-250-110-112 ~]$
--------------------------------------------

Is 'status=0 ' equals to 'exit 0'?
When writing 'exit 0' , there is no equal (=) sign.
 
Old 07-20-2006, 06:53 AM   #9
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Neither "status" nor "script" are reserved words.
Code:
status=0
assigns the value 0 to the variable $status. The line
Code:
exit 0
ends the script execution with a return value of 0.

You can read about the command [ here: http://www.tldp.org/LDP/abs/html/testconstructs.html

as well as many other things. Again, I highly recommend going through this tutorial.
 
Old 07-20-2006, 07:28 AM   #10
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47

Thanks zaichik for taking time to reply me.

How about this "[" ?
 
Old 07-20-2006, 07:44 AM   #11
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
You can read about the command [ here: http://www.tldp.org/LDP/abs/html/testconstructs.html
 
Old 07-20-2006, 08:56 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The "[" command is the same as the test command. For some commands, such as "[", you can type "help [" in the shell to get some information about them. The shell built in commands such as "if" and "while" may not have man pages, but you can use the "help" command to find out information about them. To find out if something is a command you can run: which <command>. You seem to be confusing variable names for commands. status=0 assigns 0 to the variable status. $status dereferences the status variable. A program that is run by an interpreter, like bash, or python is a script. For example, on your system the "mkinitrd" command is actually a script. SuSE tends to write Bash scripts, whereas most Mandrake scripts are written in Perl.

Another thing is to run "info bash" and use the search command "/<command>" to find it. You can use "?<command>" to search backwards.

In the konqueror browser, many of the info files have an html manual also. Simply enter "info:command" in the location bar. Please try it with "info:coreutils".

This also works with some man pages. Try "man:ls". Another way to view manpages is to generate a postscript version: man grep -t | kghostview -
or: "man -t grep | lp" to print it out.

There are some info manuals that are very important and informative, that I have printed out the formal print versions. These include the info manuals for: coreutils, sed, awk, find and tar. Doing this is a bit more involved however, because you need access to the document source files. I'll run through an example for "grep".
  1. Download the source. The http://rpm.pbone.net is a great place to download the source rpm for your particular RPM based distro (e.g. Manrake)
  2. Cd to the specs directory: cd /usr/src/redhat/SPECS
  3. Apply the patches. Some patches may fix errors in the the documents: rpmbuild -bp grep.spec
  4. Cd to the BUILD directory: cd ../BUILD
  5. Cd to the grep project directory: cd grep-2.5.1a/
  6. If a Makefile doesn't exist, run the configure script: sudo ./configure
  7. generate the manual: sudo make pdf

Note: I tried this out as I was typing. I got a strange configuration error. So, I did it a bit differently. I copied the grep-2.1a directory to my home folder, chown'ed so I was the user, and from there "make grep.pdf" worked. Somehow as root it didn't. You would be doing this on Mandrake instead of SuSE, and have a different grep-version.src.rpm to work with. Hopefully it will be successful.

Note: For SuSE Linux, the rpm build base directory is /usr/src/packages/.

Last edited by jschiwal; 07-20-2006 at 09:08 AM.
 
  


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
Exit malru AIX 6 06-27-2006 07:35 AM
difference between exit 1 and exit kushalkoolwal Programming 6 10-20-2005 03:56 PM
Exit noobtesting Mandriva 11 04-29-2004 03:07 PM
Can't exit X Moebius01 Mandriva 8 11-24-2003 07:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:23 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
Open Source Consulting | Domain Registration