nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

call function in bash

This is a discussion on call function in bash within the Shell scripting forums, part of the Development/Scripting category; hi all, i have a problem, i did search around but end up disappointed. lets say i have 3 scripts ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-15-2006, 03:26 PM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Rep Power: 0
mala_un
Default call function in bash

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?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-15-2006, 04:44 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Replace if [ -e filename ] with if [ -e filename ] ; then i.e.
Code:
#!/bin/bash
clear
if [ -e filename ]; then
./script_a
else
./script_b
fi
exit
if command syntax is as follows
if [ ]; then
...
else
...
fi
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 08-15-2006, 04:57 PM
Junior Member
 
Join Date: Aug 2006
Posts: 2
Rep Power: 0
reynold
Default

u r syntax is not corretc, here is what help if command gives me back:

Code:
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
Reply With Quote
  #4 (permalink)  
Old 08-16-2006, 10:26 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Rep Power: 0
mala_un
Default

actually what i want to do is

/bin/bash /home/mylnx/script_a

this is the call function in bash since we cant use
call script_a

thanks anyway guys...
Reply With Quote
  #5 (permalink)  
Old 08-16-2006, 10:51 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Rep Power: 0
mala_un
Default

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
Reply With Quote
  #6 (permalink)  
Old 08-16-2006, 02:35 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Quote:
#!/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
Hmm.. did ya copy and pasted code? copy paste may create a problem for you. Your code seems to perfect to me, i hope dir - /home/script/ does exists

Quote:
/bin/bash /home/mylnx/script_a
this is the call function in bash since we cant use
call script_a
To call bash function from other script you need to write as follows:
script_a code with foo() function
Code:
#!/bin/bash
function foo(){
   echo "I am foo()"
}
Now call this foo from mainscript file as follws:
Code:
#!/bin/bash
source script_a
foo
Command source includes the script_a
Reply With Quote
  #7 (permalink)  
Old 08-16-2006, 04:57 PM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Rep Power: 0
mala_un
Default

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 monk

about calling a function
u really give me an idea, i'm gonna test that..
Reply With Quote
  #8 (permalink)  
Old 08-16-2006, 06:14 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Glad to know you are learning fast ...
Reply With Quote
  #9 (permalink)  
Old 08-17-2006, 08:29 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Rep Power: 0
mala_un
Default

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..
Reply With Quote
  #10 (permalink)  
Old 08-18-2006, 02:36 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

./var => script name
$1 $2 $3 $4 $5 $6 $7 $8 $9 => All are command line args passed to this script
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Bash not working! Mwalimugini Getting started tutorials 4 03-24-2008 10:52 PM
Adding storage function to a mail server satimis Databases servers 0 01-04-2008 02:15 PM
bash history...suse 10.1 grogf Linux software 4 10-30-2007 05:42 PM
-bash: /bin/ps: Cannot allocate memory kasimani Linux software 1 10-12-2007 10:00 PM
Call Logging and Asset Mgmnt SW ricc Linux software 1 06-29-2007 02:45 AM


All times are GMT +5.5. The time now is 08:14 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36