Issue writing a function... !

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
vnairaj
Posts: 2
Joined: Mon 10 Feb 2014, 14:29

Issue writing a function... !

#1 Post by vnairaj »

Hello all..
I have return a script which is needed to return a value if it is successfully completed.

my code is :

Code: Select all

#!/bin/bash

echo "enter the source path for file1 with file extension"
source1=/root/Desktop/source/b14313.pdf.gz

echo "enter file1 name with extension"
file1=b14313.pdf.gz


echo "enter the source path for file2 with file extension"
source2=/root/Desktop/source/Oracle_10g_Installation.pdf.gz

echo "enter file2 name with extension"
file2=Oracle_10g_Installation.pdf.gz


echo "Target location : "
target=/root/Desktop/target/asd/


abcd()
{

echo "$file1 started copying...."

cp $source1 $target
val=`expr $val + 1`

echo "$file1 is copied to target location successfully"

echo "$file2 started copying...."
cp $source2 $target

val=`expr $val + 1`

cd $target
echo "$file1 started uncompressing.."
gunzip $file1

val=`expr $val + 1`

echo "$file1 uncompressed successfully"

echo "$file2 started uncompressing.."
gunzip $file2

val=`expr $val + 1`

echo "Files unzipped"

ret1=$?
echo "return1 value is $ret1" 

}
abcd

ret=$? 
echo "return value is $ret"

It need to return a value 0 ..
If everything is executed properly.. All the things are executing properly the value is returning zero but the problem is that when am trying to give wrong input also it is returning zero .. please help...

can you please put it into a function ..
please help... !

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: Issue writing a function... !

#2 Post by L18L »

vnairaj wrote: echo "Files unzipped"

ret1=$?
echo "return1 value is $ret1"
That is just meaning: echo was successfull

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#3 Post by Karl Godt »

L18L is right !

In theory you would need to evaluate the return code for every single line
and add every single return value to one final return value

For cp return values I see many many error possibilities when i look at your code the other thread ( ie read wrong filenames ) .

BTW : Is that homework ?

PANZERKOPF
Posts: 282
Joined: Wed 16 Dec 2009, 21:38
Location: Earth

Re: Issue writing a function... !

#4 Post by PANZERKOPF »

[quote="vnairaj"]

echo "$file1 started copying...."
cp $source1 $target
val=`expr $val + 1`

You should summarize exit codes.

Code: Select all


abcd() {
local retval=0

 echo "$file1 started copying...."
 cp $source1 $target
 retval=$((retval+$?))

 another_command
 retval=$((retval+$?))

 yet_another_command
 retval=$((retval+$?))

return $retval 
}

abcd
if [ $? -eq 0 ]; then
 echo "OK"
else
 echo "Fail"
fi



SUUM CUIQUE.

Post Reply