LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I can't figure out the error message. (https://www.linuxquestions.org/questions/programming-9/i-cant-figure-out-the-error-message-609939/)

Uncle_Theodore 01-01-2008 11:48 AM

Quote:

Originally Posted by Gins (Post 3007424)
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

Gins 01-01-2008 12:41 PM

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 '.

Gins 01-01-2008 12:50 PM

Thanks Uncle_Theodore.

I tried what you have suggested; it works.

Nylex 01-01-2008 12:50 PM

Quote:

Originally Posted by Gins (Post 3007709)
The usual 'cp' command does not care whether it overwrite the destination file.

It will care if you use the -i option, which you'd have known if you had read the man page.

Uncle_Theodore 01-01-2008 12:52 PM

Quote:

Originally Posted by Gins (Post 3007709)
Thanks gnashley.

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 $?

Gins 01-01-2008 01:38 PM

Thanks for the reply.

--------------------------------
if [ "$#" -ne 2 ] ;

then

echo "Usage: mycp from to"
exit 1
-----------------------------------

I just removed the 'exit 1 ' line and reran the program.
It worked fine.

Of course I gave the name 'mycp8' and ran the
command 'chmod 755 mycp8 ' make it an executable one.


I expected an error message. No error messages.
This means 'exit 1' line is not necessary.

Uncle_Theodore 01-01-2008 01:49 PM

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.

Gins 01-01-2008 01:49 PM

You are right Nylex.

The regular 'cp' command is different.

The program I made has the name 'mycp'.

I must always write ' ./mycp file1 file2 '.

Is it possible to escape write ' ./mycp ... '?

I would like to write ' mycp file1 fil2 ' similar to the ' cp ' command.

Uncle_Theodore 01-01-2008 01:52 PM

Quote:

Originally Posted by Gins (Post 3007760)
I would like to write ' mycp file1 fil2 ' similar to the ' cp ' command.

Add the dot "." to your PATH variable.
I think you'd be much better off just reading this document.
http://tldp.org/LDP/abs/html/
Good Luck.

dive 01-01-2008 02:04 PM

Quote:

Originally Posted by Gins (Post 3007760)
Is it possible to escape write ' ./mycp ... '?

I would like to write ' mycp file1 fil2 ' similar to the ' cp ' command.

If you intend to write more scripts then you can make a scripts folder in your home directory and add it to your path in .bashrc or .bash_profile:

export PATH=$PATH:~/scripts

Put all your scripts in there.

gnashley 01-01-2008 02:53 PM

Or, easiest of all, you can place a copy of the script in your regular path -/usr/local/bin would be the best place.

Gins 01-01-2008 03:21 PM

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:~>


Nylex 01-02-2008 12:30 AM

You need a colon between '.' and '/usr/local/bin'.


All times are GMT -5. The time now is 06:44 PM.