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 .

#!/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 .

./pmeter -c a3

and below is the error which i got

tar: a3: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors.

but i can see dir is created .

$ ls -lrt a3.tar
-rw-r--r--  1 khare  guest  1024 Oct 10 03:08 a3.tar
$ 

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

sorry but i am not able to understand you mention.
request you to please explain again .

once again sorry for the inconvenience

Hi Scriptor,

If you run the script like this;

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

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

Regards

Dave

one more thing
why i am getting error when trying to create a file.

$ ./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

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

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:

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

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

 $ ./pmeter -c keto

o/p

a keto

below is the command for dir qq

$ ./pmeter -c qq

o/p

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

If idea is to backup some subdir to the tar file or look tar file, then maybe something like:

#!/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

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

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.

$ 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).

$ ./pmeter2 -t f1

i am getting below o/p

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

---------- 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

I suggest removing the 2>/dev/null to see what tar thinks it's doing.

Hi Corona,

thx for your response.

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

$ 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

$ ./pmeter2 -t f2

f2

Hi Corona,

i think i found the error . in the below code i remove the space between double quotes and 2

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

after this my script run correctly. o/p below

$ ./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

Hi

one more observation
when i am keeping the code as it is (as mention earlier)

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

$ ./pmeter2 -t f2  f2

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

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

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