Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 18 Sep 2014, 01:56
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Issue writing a function... !
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [4 Posts]  
Author Message
vnairaj

Joined: 10 Feb 2014
Posts: 2

PostPosted: Mon 10 Feb 2014, 12:49    Post subject:  Issue writing a function... !
Subject description: Code in a function
 

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

my code is :
Code:
#!/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... !
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Mon 10 Feb 2014, 14:51    Post subject: Re: Issue writing a function... !
Subject description: Code in a function
 

vnairaj wrote:

echo "Files unzipped"

ret1=$?
echo "return1 value is $ret1"
That is just meaning: echo was successfull
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 3972
Location: Kiel,Germany

PostPosted: Mon 10 Feb 2014, 15:27    Post subject:  

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 ?
Back to top
View user's profile Send private message Visit poster's website 
PANZERKOPF

Joined: 16 Dec 2009
Posts: 280
Location: Earth

PostPosted: Tue 11 Feb 2014, 08:21    Post subject: Re: Issue writing a function... !
Subject description: Code in a function
 

[quote="vnairaj"]

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

You should summarize exit codes.

Code:


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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [4 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0542s ][ Queries: 11 (0.0100s) ][ GZIP on ]