View Full Version : Simple BASH question
partloer
February 8th, 2012, 10:05 PM
Just trying to create a simple BASH script that the main.sh file calls a function in the def.sh file.
main.sh
#!/bin/bash
./def.sh
test
exit
def.sh
test(){
echo "test"
}
I run "sh main.sh" and get no output. I tried Googling this but couldn't find anything as to why the file is included but the function call doesn't work.
MadCow108
February 8th, 2012, 10:38 PM
written like this it does not include the other file but executes the file in another process. it then has no influence on the current file
to include you have to use
. def.sh
(note the space after the dot) or
source def.sh
partloer
February 9th, 2012, 11:19 AM
I have tried this before.
source def.shHowever I get an error.
main.sh: 3: source: not found
The file is in the same folder and I have tried absolute paths too.
partloer
February 9th, 2012, 08:05 PM
BAM!...it works, thanks Major_Bloodnok
Khayyam
February 9th, 2012, 08:19 PM
partloer ...
Set 'bash' rather than 'sh' as the interpreter, 'sh' doesn't have the 'builtin' source. Also 'test' is a unix command, so choose something unique as the function identifier.
Main.sh
#!/bin/bash
source $(pwd)/def.sh
testit
exitdef.sh
function testit()
{
echo "test"
}run the test ..
% chmod u+x main.sh
% ./main.sh
test
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.