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 01-01-2008, 11:48 AM   #31
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71

Quote:
Originally Posted by Gins View Post
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
 
Old 01-01-2008, 12:41 PM   #32
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 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 '.
 
Old 01-01-2008, 12:50 PM   #33
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 Uncle_Theodore.

I tried what you have suggested; it works.
 
Old 01-01-2008, 12:50 PM   #34
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by Gins View Post
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.
 
Old 01-01-2008, 12:52 PM   #35
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Quote:
Originally Posted by Gins View Post
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 $?
 
Old 01-01-2008, 01:38 PM   #36
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 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.
 
Old 01-01-2008, 01:49 PM   #37
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
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.
 
Old 01-01-2008, 01:49 PM   #38
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
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.
 
Old 01-01-2008, 01:52 PM   #39
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Quote:
Originally Posted by Gins View Post
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.
 
Old 01-01-2008, 02:04 PM   #40
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Quote:
Originally Posted by Gins View Post
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.

Last edited by dive; 01-01-2008 at 02:06 PM.
 
Old 01-01-2008, 02:53 PM   #41
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Or, easiest of all, you can place a copy of the script in your regular path -/usr/local/bin would be the best place.
 
Old 01-01-2008, 03:21 PM   #42
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
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:~>

Last edited by Gins; 01-01-2008 at 03:24 PM.
 
Old 01-02-2008, 12:30 AM   #43
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You need a colon between '.' and '/usr/local/bin'.
 
  


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
i couldn't figure out error message hibudd Linux - Software 1 03-26-2007 10:26 AM
Can't figure out this error MrSako Linux - Software 2 01-06-2007 10:13 AM
Strange Repeating Error message in /var/log/message lucktsm Linux - Security 2 10-27-2006 08:29 AM
New to Ubuntu - can't figure out dpkg "kernel image" message for RT2500 install phaggood Ubuntu 1 02-09-2006 11:41 AM
Configuration Error can't figure out!! amrogers3 Linux - Newbie 16 01-29-2004 12:36 PM

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

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