nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 15-08-2006, 02:26 PM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 15-08-2006, 03:44 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 244 Times in 183 Posts
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 Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 15-08-2006, 03:57 PM
Junior Member
User
 
Join Date: Aug 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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 16-08-2006, 09:26 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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 16-08-2006, 09:51 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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 16-08-2006, 01:35 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
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 16-08-2006, 03:57 PM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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 16-08-2006, 05:14 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
Default

Glad to know you are learning fast ...
Reply With Quote
  #9 (permalink)  
Old 17-08-2006, 07:29 AM
Junior Member
User
 
Join Date: Jul 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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 18-08-2006, 01:36 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 244 Times in 183 Posts
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 Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 07:41 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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