The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File already exists error while using '>' operator jathin12 UNIX for Dummies Questions & Answers 5 02-26-2009 08:05 AM
checking textfile exists or not in all directories G.K.K Shell Programming and Scripting 5 09-29-2008 05:04 AM
Checking if a file exists gugs Shell Programming and Scripting 4 07-23-2008 12:42 PM
Checking the file if it exists ksmbabu Shell Programming and Scripting 3 04-02-2008 06:16 AM
checking a connection still exists? fishman2001 IP Networking 2 12-14-2007 07:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-04-2009
rowlf rowlf is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 6
Checking if file exists using a NOT operator and shell variable

Hi,

I'm new to UNIX, at least shell programming and am having trouble figuring out a problem i'm having. In one section in my nested if statement, i want the program to test if the file does not exist, based on an argument supplied at the command line by the user. What i have is

PHP Code:
elif [ -!$]; then
              
echo "Message: File \"$4\" does not exist. Creating new file."
              
touch $4
              cp 
$$4
              set error
=
and from there it goes down a long list of other conditions, but this is the only one giving me problems. It skips completely over it and doesnt print anything to screen.

Another problem i'm having is with error codes. I want certain error code values to display if the user should decide to ask for the error code level at the command line, using a logical AND in addition to the arguments supplied for the main part of hte program. I tried changing the last line in all of my nested ifs to

PHP Code:
set $?=<error code i wanted
bu that threw an error saying an assignment was attempted on a nonvariable. OK fine, so how would this be done? Is it even possible to set up specified error codes in a shell script that you can call by using 'echo $?' ?

Any help is appreciated, thanks !!


Using BASH shell btw.

Last edited by rowlf; 04-04-2009 at 09:30 PM..
  #2 (permalink)  
Old 04-04-2009
Gee-Money Gee-Money is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 48
I don't think you can set "?"

For one thing, you do assignment without the "$", so even if you tried, it would be
Code:
$ set ?=127
$ echo $?
0
However, it doesn't achieve the desired result, as after every command (even "set"), "?" is set to the exist status of last command.

You could just use an argument to exit (or return if in a function), as in

Code:
exit 127 (or return 127)
And for your file test...try the ! at the left of the test

Code:
if [ ! -e $4 ]; then
  stuff;
  exit 127;
fi

Last edited by Gee-Money; 04-04-2009 at 10:32 PM..
  #3 (permalink)  
Old 04-04-2009
Gunther Gunther is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 37
Concerning your first problem, you need to move the ! in front of &quot;-e&quot;. Otherwise you're just negating the command line argument.
About your second problem: Why don't you call &quot;exit&quot; with the respective error code? OK, I wasn't fast enough
  #4 (permalink)  
Old 04-05-2009
rowlf rowlf is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 6
Both worked - thanks a million guys !

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0