Home / Software / BSD, Linux, and Unix / call function in bash
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: 1 2 [All] - (Bottom) Print
Author Topic: call function in bash  (Read 2812 times)
mala_un
Guest
« on: August 15, 2006, 02:58:12 AM »

hi all,

i have a problem, i did search around but end up disappointed.

lets say i have 3 scripts script_a, script_b and script_c.
all this script is saved in the same directory
what if i want to call script_a and script_b inside a script_c

i've test this way;

#!/bin/bash
clear
if [ -e filename ]
./script_a
else
./script_b
fi
exit

but then, i cant call the ./script_a and ./script_b.
the error message show that there is unexpected before else.

how am i going to do this?
IP logged
Rob Pomeroy
Prodigy



Thanked: 119
Posts: 6,454

Experience: Expert
OS: Other


Web/Networking/Linux

Me
« Reply #1 on: August 15, 2006, 11:06:36 AM »

Try this:
#!/bin/bash
clear
if [ -e filename ]
  /bin/bash /fullpathname/script_a
  exit
else
  /bin/bash /fullpathname/script_b
  exit
fi
exit
IP logged

Author of the fantasy thriller, Insensate - available for all ebook readers and iDevices. Find out more >here<. Only 99p/99¢!
mala_un
Guest
« Reply #2 on: August 15, 2006, 06:14:44 PM »

thanks rob..

i will try
IP logged
mala_un
Guest
« Reply #3 on: August 15, 2006, 09:50:45 PM »

its working man..
u r great  ;D
IP logged
mala_un
Guest
« Reply #4 on: August 15, 2006, 10:20:33 PM »

another problem...

i want user to enter a file name..
then i want to check whether the file is exist or not in home directory..
if exist.. copy to new file..

i wrote this way, but its not working...

#!/bin/bash
echo "Enter filename:"
read filename

cd /home/
if [ -e $filename ]
then
cp $filename /home/script/filename2
else
echo "The file not exist"
fi

the result: unexpected .... before else
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #5 on: August 16, 2006, 02:50:28 AM »

hmm.strange, your code works on my machine, which i only change the path to my own path.
the thing i can suggest is to make sure your paths are correct...etc...
also, for debugging, you could try to put set -x in your script, eg

#/bin/bash
set -x
...
...
<your code>
...

This will show some debug statements in your output...
IP logged

mala_un
Guest
« Reply #6 on: August 16, 2006, 04:24:31 AM »

hehe
what a shame... it was my fault..

i should write this way

#!/bin/bash
echo "Enter filename:"
read filename
 
if [ -e $filename ]
then
cp /home/$filename /home/script/filename2
else
echo "The file not exist"
fi

thanks ghostdog74
IP logged
mala_un
Guest
« Reply #7 on: August 16, 2006, 07:49:42 PM »

i'm a bit confuse...

if i have something like this,
./var $1 $2 $3 $4 $5 $6 $7 $8 $9

what would it used for actually..
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #8 on: August 16, 2006, 08:15:00 PM »

assume your script name is "test.sh" and has something like this:

#!/bin/bash
...
./var $1 $2 $3 $4 $5 $6 $7 $8 $9
....

this means, you are passing arguments from 1 to 9 passed to "test.sh" to the script called "var" at the current directory.

So assuming test.sh is a script  to store 9 user information, and user has these information
1) name = mala
2) tel = 12345678
3) house no = 100
4) favorite color = black
5) favorite number = 4
6) birthday = 12082006
7) car = mercedes benz
8) sex = male
9) city = somewhere

so when invoking the "test.sh" script, it look like this maybe:

unix#>  ./test.sh mala 12345678 100 black 4 12082006  "mercedes benz" male somewhere

and then all these arguments will be passed to "var" script in your "test.sh" script...
man, my english is not very good, hope you understand. :)

IP logged

mala_un
Guest
« Reply #9 on: August 16, 2006, 11:37:32 PM »

you r doing well man,

let me try, correct me if i'm wrong

so the ./var is another script to store all the data into
1) ..
to
8)
9)
is it?

what about this.. am i correct?

./mala $1 $2 $3 $4 $5 $6 $7 $8 $9

if [ $? = 0 ]; then

echo "success"

fi

does this mean that, if ./mala run successfully without error, it will return 0.
if there is an error, it will come maybe from $5.. or else..

am i right?

i just want to know if there is an error.
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #10 on: August 16, 2006, 11:56:35 PM »

Quote

so the ./var is another script to store all the data into
1) ..
to
8)
9)
is it?

what about this.. am i correct?


yes, basically, var is used to process those arguments passed from the first script.


Quote
./mala $1 $2 $3 $4 $5 $6 $7 $8 $9

if [ $? = 0 ]; then

echo "success"

fi

does this mean that, if ./mala run successfully without error, it will return 0.
if there is an error, it will come maybe from $5.. or else..

am i right?

That will depend on how "mala" script is written.An example for "mala" script

Code: [Select]
#!/bin/sh
name=$1
tel=$2
....
....

if [ $tel="" ];then
   exit 1
fi

...
...

Then from the first script,  you check the return code

Code: [Select]
if [ $? -eq 1 ];then
    echo "telephone number is invalid"
fi

Similarly for other errors , you can assign an exit code...

This is just an example..



IP logged

mala_un
Guest
« Reply #11 on: August 17, 2006, 12:24:04 AM »

thanks man..

it very nice of u to explain to me clearly..

 ;D
IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #12 on: August 17, 2006, 01:01:39 AM »

no problem  :)
IP logged

mala_un
Guest
« Reply #13 on: August 17, 2006, 02:22:24 AM »

oh man...
my head want to explode...

/home/script_a : [ : blog.txt : binary operator expected

from this error what can u see...

it just cannot open the blog.txt..

IP logged
ghostdog74
Mentor



Thanked: 26
Posts: 1,511


« Reply #14 on: August 17, 2006, 03:00:46 AM »

not really, is script_a a shell script ? or a compiled program?
please post the script content if possible.

IP logged

mala_un
Guest
« Reply #15 on: August 17, 2006, 04:48:07 AM »

you know what...
i change the directory by copy it to another dir of the blog.txt...
then its ok...
IP logged
Pages: 1 2 [All] - (Top) Print 
Home / Software / BSD, Linux, and Unix / call function in bash « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.104 seconds with 19 queries.