LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-31-2007, 03:07 AM   #16
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71

In the original script "if" was commented out, but the corresponding "then" was not. That's why bash said "unexpected token 'then'"... Why did you spend so much time correcting a commented out line?
 
Old 12-31-2007, 03:08 AM   #17
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Quote:
Originally Posted by gnashley View Post
Code:
if [-e "$to" ] ; then
Might wanna edit that and add a space
 
Old 12-31-2007, 05:28 AM   #18
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Thanks dive, I should have schecked that $%&"!)? code before posting -makes me look as lazy as the OP...
 
Old 12-31-2007, 02:28 PM   #19
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
Now it works.
Thanks for all the help.

if [ "$#" -ne 2 ]

Here ne means 'not equals'.
What is the reason for giving the value 2 here?
Can't we write 3, 4, 15 some other higher value?


I replaced 2 with 32. The program didn't work. I mean copying of one file to another file did not take place. However, there was not any error message; why is that?

Last edited by Gins; 12-31-2007 at 02:34 PM.
 
Old 12-31-2007, 02:34 PM   #20
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
$# presumably gives the number of arguments to the script. So, it makes sense to test that $# is not equal to two, because your script requires two arguments: the source and destination files. If you've supplied fewer or more arguments, that doesn't make sense for copying.
 
Old 12-31-2007, 02:46 PM   #21
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
Thank Nylex

if [ -e "$to" ] ;

I guess '' e '' means the existence. It just checks whether a file named 'to' is on my system.
Am I correct?


What would be the position if the destination file is write protected?

Last edited by Gins; 12-31-2007 at 02:49 PM.
 
Old 12-31-2007, 02:49 PM   #22
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Yes, it checks whether the file, whose name stored in the variable "to", exists.
 
Old 12-31-2007, 03:08 PM   #23
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

If the file named 'to' is write protected, will it overwrite?
 
Old 12-31-2007, 04:29 PM   #24
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Gins View Post
Thank Nylex

if [ -e "$to" ] ;

I guess '' e '' means the existence. It just checks whether a file named 'to' is on my system.
Am I correct?


What would be the position if the destination file is write protected?

man bash
man cp




Cheers,
Tink
 
Old 12-31-2007, 05:44 PM   #25
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
No, I didn't find any relevant information in the man pages of 'bash' and 'cp'.
 
Old 12-31-2007, 05:49 PM   #26
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 Uncle_Theodore

If the file named 'to' is write protected, will it overwrite?
If the user, under whose name your script runs, does not have write permissions on the destination file, it will not be overwritten.
 
Old 12-31-2007, 06:52 PM   #27
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Gins View Post
No, I didn't find any relevant information in the man pages of 'bash' and 'cp'.
How long did you try for? 1 second, 3 seconds?

man bash
/-e
n
(n x 5 on my machine, took me 4 seconds to get there)
gives you EXACTLY the answer to the -e question of yours.

I think you're just a lazy person who expects others to
regurgitate knowledge and spoon-feed it to you.

I have a splendid idea: why don't you re-write all the
man-pages you don't like reading in a nice way that fits
your taste, and start giving back to the community that
has been pampering you for years in that way?



Cheers,
Tink
 
Old 01-01-2008, 02:31 AM   #28
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
YOu can get a quicker idea of wht the -e and other such oprions mean by reading the ma page for 'test'. These:
Code:
[ -e file ]
[ -x file ]
[ -r file ]
and other such constructs are constructs of the test program. The '[' means 'test'. However, these are are handled internally by the bash builtins so there are a few differences(bash can handle double brackets, etc). But, still, for the basic usage and tests you may find it much easier to read the 'test' man page rather than the bash man page, which is very long.
 
Old 01-01-2008, 05:55 AM   #29
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
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.

Last edited by Gins; 01-01-2008 at 06:06 AM.
 
Old 01-01-2008, 09:10 AM   #30
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Here's an example of usage with test instead of brackets:
Code:
#!/bin/sh
# copy a file
if test $# -ne 2 ; then
  echo "Usage: mycp from to"
  exit1
fi
You could also write the same thing like this:
Code:
test $# -ne 2 && echo "Usage: mycp from to" && exit 1
You asked about checking to see if a file is non-writeable:
if ! [ -w file ] ; then
 
  


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 12:22 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