confusion in use of exit 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting confusion in use of exit 0
# 8  
Old 10-10-2012
If idea is to backup some subdir to the tar file or look tar file, then maybe something like:

Code:
#!/bin/sh
PRG="$0"

# backup dir to tar file

usage()
{
       echo "usage: $0  [-c|-t] dir" >&2
       exit 1
}

[ $# -lt 2 ] && usage

opt="$1"
dir="$2"

case "$opt" in
  -t) TARGS="-tvf $dir.tar" ;;
  -c) [ ! -d  "$dir" ] && echo "$dir is not dir" >&2 && exit 2
       TARGS="-cvf $dir.tar $dir" 
       ;;
  *)  usage ;;
esac
tar $TARGS

# 9  
Old 10-10-2012
Quote:
Originally Posted by scriptor
Now my question is why i am getting a letter "a" in the output along with file or dir name .
This is the output from the tar program itself: as it creates the tar archive ("tar" is short for "tape archive", that was the original purpose of the program) it "archives" file after file. Every file processed in such manner is reported as output, along with the "a" for "added" or "archived".

I hope this helps.

bakunin
# 10  
Old 10-12-2012
waiting for reply
plz respond

---------- Post updated at 12:27 PM ---------- Previous update was at 12:22 PM ----------

so this means that getting 'a' is obvious and the output is correct .
and
sry plz ignore my previous post.

---------- Post updated at 04:53 PM ---------- Previous update was at 12:27 PM ----------

so this means that getting 'a' is obvious and the output is correct .

---------- Post updated 10-12-12 at 04:00 PM ---------- Previous update was 10-11-12 at 04:53 PM ----------

one more confusion.

my below script is not giving expected output.

Code:
$ cat -n pmeter2
     1  #!/bin/sh
     2  USAGE="Usage: `basename $0` [-c|-t] [files|directories]"
     3  if [ $# -lt 2 ] ; then
     4  echo "$USAGE" ;
     5  exit 1 ;
     6  fi
     7  case "$1" in
     8  -t) shift ; TARGS="-tvf" ;
     9  for i in "$@" ; do
    10  if [ -f "$i" ] ; then
    11  FILES=`tar $TARGS "$i" 2>/dev/null`
    12  if [ $? -eq 0 ] ; then
    13  echo ; echo "$i" ; echo "$FILES"
    14  else
    15  echo "ERROR: $i not a tar file."
    16  fi
    17  else
    18  echo "ERROR: $i not a file."
    19  fi
    20  done
    21  ;;
    22  -c) shift ; TARGS="-cvf" ;
    23  tar $TARGS archive.tar "$@"
    24  ;;
    25  *) echo "$USAGE"
    26  exit 0
    27  ;;
    28  esac
    29  exit $?

when i ran the script like (where f1 is a file name).
Code:
$ ./pmeter2 -t f1

i am getting below o/p
Code:
f1

but the expected output should be as "f1 is not a tar file".

can you please pin point my error.

---------- Post updated at 04:15 PM ---------- Previous update was at 04:00 PM ----------

plz respond to my query

Moderator's Comments:
Mod Comment edit by bakunin: please keep in mind that we are no helpdesk and bumping up threads is forbidden by our rules. If you want reliable responsible consultancy within a certain amount of time please consider hiring an expert instead of pestering us for not answering within a certain timeframe.

All this will get you is stony silence and some infractions. Thank you for your consideration.


---------- Post updated at 08:42 PM ---------- Previous update was at 04:15 PM ----------

hi bakunin
ok . i accept my mistake. and sorry for that.
this is purely a mistake and not intentially.

i will wait for my answer.

thx
vk

Last edited by bakunin; 10-12-2012 at 08:49 AM..
# 11  
Old 10-12-2012
I suggest removing the 2>/dev/null to see what tar thinks it's doing.
# 12  
Old 10-13-2012
Hi Corona,

thx for your response.

i remove the line which you suggested but output is still the same .

Code:
$ cat -n pmeter2
     1  #!/bin/sh
     2  USAGE="Usage: `basename $0` [-c|-t] [files|directories]"
     3  if [ $# -lt 2 ] ; then
     4  echo "$USAGE" ;
     5  exit 1 ;
     6  fi
     7  case "$1" in
     8  -t) shift ; TARGS="-tvf" ;
     9  for i in "$@" ; do
    10  if [ -f "$i" ] ; then
    11  FILES=`tar $TARGS "$i"` 
    12  if [ $? -eq 0 ] ; then
    13  echo ; echo "$i" ; echo "$FILES"
    14  else
    15  echo "ERROR: $i not a tar file."
    16  fi
    17  else
    18  echo "ERROR: $i not a file."
    19  fi
    20  done
    21  ;;
    22  -c) shift ; TARGS="-cvf" ;
    23  tar $TARGS $1.tar "$@"
    24  ;;
    25  *) echo "$USAGE"
    26  exit 0
    27  ;;
    28  esac
    29  exit $?
$


the output is below

Code:
$ ./pmeter2 -t f2

f2

# 13  
Old 10-15-2012
Hi Corona,

i think i found the error . in the below code i remove the space between double quotes and 2
Code:
11  FILES=`tar $TARGS "$i" 2>/dev/null`

after this my script run correctly. o/p below
Code:
$ ./pmeter2 -t f2
tar: Error opening archive: Failed to open 'f22'
ERROR: f2 not a tar file.
$

however i am unable to understand
1) why a single space causing a problem.
2) why script not working even removing 2>/dev/null (as per you suggestion )
3) why in the above attached o/p is is show f22 . when the file name is f2

Last edited by scriptor; 10-15-2012 at 03:46 AM.. Reason: forgot to use wrap tag
# 14  
Old 10-16-2012
Hi

one more observation
when i am keeping the code as it is (as mention earlier)
Code:
FILES=`tar $TARGS "$i" 2>/dev/null`

the o/p comes as expected in case when file size is not zero. if we consider the file whose size is zero in that case is show below o/p
Code:
$ ./pmeter2 -t f2  f2

in this case , then i remove the space between double quotes and 2.

Code:
11  FILES=`tar $TARGS "$i" 2>/dev/null`

now request you to please tell me why this is happening .

Last edited by scriptor; 10-16-2012 at 04:04 AM.. Reason: missed to mention o/p
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