ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
#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
[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
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
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
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.
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".
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)
Cd to the specs directory: cd /usr/src/redhat/SPECS
Apply the patches. Some patches may fix errors in the the documents: rpmbuild -bp grep.spec
Cd to the BUILD directory: cd ../BUILD
Cd to the grep project directory: cd grep-2.5.1a/
If a Makefile doesn't exist, run the configure script: sudo ./configure
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/.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.