confusion in use of exit 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting confusion in use of exit 0
# 1  
Old 10-10-2012
Java confusion in use of exit 0

hi

i am new to shell scripting.

i was going thru the part option and arguments. on this section i fail to understand the use of exit 0 in below example .
Code:
#!/bin/sh
USAGE="Usage: $0 [-c|-t] [file|directory]"
case "$1" in
-t) TARGS="-tvf $2" ;;
-c) TARGS="-cvf $2.tar $2" ;;
*) echo "$USAGE"
exit 0
;;
esac
tar $TARGS

also, i got and when i use option c in the script. however directory created successful.

below command i use to create the dir .
Code:
./pmeter -c a3

and below is the error which i got
Code:
tar: a3: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors.

but i can see dir is created .
Code:
$ ls -lrt a3.tar
-rw-r--r--  1 khare  guest  1024 Oct 10 03:08 a3.tar
$

# 2  
Old 10-10-2012
Hi Scriptor,

The file has to exist before you can run this.

IE the file a3 would have to exist to create a valid a3.tar.

Regards

Dave
# 3  
Old 10-10-2012
sorry but i am not able to understand you mention.
request you to please explain again .

once again sorry for the inconvenience
# 4  
Old 10-10-2012
Hi Scriptor,

If you run the script like this;

Code:
davem]:$ ./pmeter -c "filename"

Then "filename" should exist and will be archived to "filename.tar"

Regards

Dave
# 5  
Old 10-10-2012
one more thing
why i am getting error when trying to create a file.
Code:
$ ./pmeter -c "a5"
tar: a5: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors.

---------- Post updated at 04:26 PM ---------- Previous update was at 03:14 PM ----------

waiting for response
plz
# 6  
Old 10-10-2012
You have to be aware that your script is running some severe risks: create 2 or three files in a new empty directory (run "mkdir x ; cd x ; touch a; touch b; touch c") and then run

Code:
pmeter -c '*'

Do an "ls -l" before and after and notice what happens. I do hope you created a new directory for this - in this case just go one level up and remove it with "rm -rf x" instead of having to deal with "*.tar".

If you want to reduce the risk check your passed arguments before:

Code:
if [ \( ! -f "$2" \) -a \( ! -d "$2" \) ] ; then
     echo "Error: only (regular) files or dirs allowed." > /dev/stderr
     exit 1
fi

The "-f" asks if the contents of "$2" exists and is a regular file, the "-d" if it is a directory, the "-o" is a logical AND, the "!" is a logical NOT.

I hope this helps.

bakunin
# 7  
Old 10-10-2012
Hi Bakunin

thx for your response.
what i was able to understand from your response is that, i was getting below error because there was no such file or dir .

however this time i make a file keto and dir qq and then run the script.

below is the command for file keto
Code:
 $ ./pmeter -c keto

o/p
Code:
a keto

below is the command for dir qq
Code:
$ ./pmeter -c qq

o/p
Code:
a qq

Now my question is why i am getting a letter "a" in the output along with file or dir name .

i thing it should come back to the prompt.

-rw-r--r-- 1 khare guest 100 Oct 10 11:23 keto
-rw-r--r-- 1 khare guest 2048 Oct 10 11:23 keto.tar
drwxr-xr-x 2 khare guest 512 Oct 10 11:26 qq
-rw-r--r-- 1 khare guest 1536 Oct 10 11:26 qq.tar

regards
vk
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need the difference between exit 1 & exit 7

Hi In one of the script I am seeing some thing like exit 7,exit 1,exit 2,exit 3,exit 9,exit6.What is the difference between all of this exit.Can anyone help here please (3 Replies)
Discussion started by: ginrkf
3 Replies

2. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

3. UNIX for Dummies Questions & Answers

crontab confusion

I come across an entry in cron which is in such: 0 * * * * What is the first 0 indicating? 0 minute? meaning a script cron as such will run every minute? :confused: (2 Replies)
Discussion started by: user50210
2 Replies

4. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

5. UNIX for Dummies Questions & Answers

what is meaning of exit(0) and exit(1)

can u tell me what is the meaning of exit(0),exit(1),exit(2) what is diff amonng these. Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

6. Programming

exit(0) versus exit(1)

What is the difference between using exit(0) and exit(1) to exit a program? Which should I use? (9 Replies)
Discussion started by: enuenu
9 Replies

7. UNIX for Dummies Questions & Answers

ftp confusion

I'm an intern at a company that recently bought out another business. In doing so, they inherited a unix system that contains files which they need to retrieve. No one in the company, including myself, really understands or knows unix so please respond with the true assumption that I'm a unix... (1 Reply)
Discussion started by: intern
1 Replies

8. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

9. UNIX for Dummies Questions & Answers

Please help me clear up some confusion

Hello All, I like this forum btw, and have only been lurking for about a day. Recently I purchased some new hardware (AMD Athlon 64 3200+ and a Asus K8V Deluxe Motherboard), and I want to find an OS that can take advantage of the 64 bit processor. Basically, what are the differences... (2 Replies)
Discussion started by: RoY_mUnSoN
2 Replies
Login or Register to Ask a Question