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.
Gnashley
Yes, the command 'man test' gave me the following:
-e FILE [ This means FILE exists]
-f FILE [This means FILE exists and is a regular file]
-x FILE [ This means FILE exists and execute (or search) permission is
granted]
I have file called 'w3' on my system. It is a text file.
I tested the command 'test'. I didn't write a shell script program.
Ni@linux-3vxw:~> test [ -e w3 ]
bash: test: too many arguments
Ni@linux-3vxw:~> test [ w3 ]
bash: test: w3: binary operator expected
Ni@linux-3vxw:~>
Why did I get a strange output?
----------------------------------------------------------
I can't understand what Tinkster has written.
You don't use "test" and "[]" together, because it's the same thing. Try this if test -e w3; then echo "It's a file"; else echo "No such file"; fi
if [ -e w3 ]; then echo "It's a file"; else echo "No such file"; fi
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
Thanks gnashley. I wish I knew these things like you.
If you know everything, you know how to play with programming as well as shell scripting.
The problem is that I don't work with these things at my work.
I try to learn these things when I find time. Nowadays I have some free time until 7th of January. I will try to learn shell scripting, sed, awk , grep, egrep, etc.
I will do some modifications to the program I made with your help and ask your help when necessary. The usual 'cp' command does not care whether it overwrite the destination file. The program I made with your help is clever. It asks your permission to overwrite.
I just came home from the gym. I train 3 times a week at the gym. I will try to modify it later on.
By the way, what is the difference
between 'exit 1' and ' exit 0 '?
Sometimes you write 'exit 1 ' and sometimes it is ' exit 0 '.
By the way, what is the difference
between 'exit 1' and ' exit 0 '?
Sometimes you write 'exit 1 ' and sometimes it is ' exit 0 '.
It's the return code. When the program exits, you might wanna know if it's done everything as you intended or whether something went wrong. So, if your program aborts execution due to an error, you make it return 1 (exit returning 1), and if everything went fine, you make it exit returning 0. You can always check the return code of the last program executed in your bash shell by running echo $?
The $# variable holds the number of arguments given to the script. The original intent of this piece of code
Code:
if [ "$#" -ne 2 ] ;
then
echo "Usage: mycp from to"
exit 1
was to exit the script with the return code of 1 if the number of arguments is not 2. There's no error message to be expected, just if you look at the output of
echo $?
right after the script exited in the abovementioned case, it will return 1, not 0.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
I thank Uncle_Theodore, Dive, Gnashley and the others for the comments.
Dive asked whether I am going to write more shell scripts. Everything hinges on time. If I have time, I will do mo work.
Code:
The following is my path:
Ni@linux-3vxw:~> echo $PATH
/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib64/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
Ni@linux-3vxw:~>
I will change it to the following:
Ni@linux-3vxw:~> echo $PATH
./usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib64/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
Ni@linux-3vxw:~>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.