bash: executing commands and reading exit vals


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash: executing commands and reading exit vals
# 1  
Old 07-17-2006
bash: executing commands and reading exit vals

I have a function that returns a bunch of exit codes, say func1, and in my code I'm trying to execute that function in an if statement. This is the closest I could get.

f1call=`func1 $arg1 $arg2`
if [[ "$f1call" = "0" ]]; then
...
fi

When I run the script the function never gets called. What's the right way to do this?

Here are some other basic questions that I need answered too:

-When do we use double-brackets vs. single-brackets for the if-statement expression?

-Is the exit code that's being returned an integer or string? I ask this because I'm wondering if it should be "[[ $f1call = 0 ]]" as opposed to what I have up there.
# 2  
Old 07-17-2006
Your usage would indicate that
Code:
func1()
{
    .....do stuff
    echo "$EXIT_CODE"
}

Anyway that's what func1 has to do for
Code:
 f1call=`func1 parm1 parm2 parm3`
 echo "$f1call"

the above code to work.
# 3  
Old 07-17-2006
So does that mean I don't need to put "exit $exit_val", but instead just "echo $exit_val"? Or do I still need that exit statement in my function?
# 4  
Old 07-18-2006
Quote:
Originally Posted by eur0dad
So does that mean I don't need to put "exit $exit_val", but instead just "echo $exit_val"? Or do I still need that exit statement in my function?
having an exit $exit_val in the function will not have any effect with respect to the function

it is the value that is echoed is caught in the variable.

just the echo statement would do
# 5  
Old 07-18-2006
Code:
func1()
{
    .....do stuff
    #echo "$EXIT_CODE"
    return $EXIT_CODE
}

if func1 $arg1 $arg2
then
   # func1 : succes (exit status 0)
else
   # func1 : error
fi

func1 $arg1 $arg2
if [ $? -eq 0 ]
then
   # func1 : succes (exit status 0)
else
   # func1 : error
fi

Jean-Pierre.

Last edited by aigles; 07-18-2006 at 11:54 AM..
# 6  
Old 07-18-2006
Quote:
Originally Posted by matrixmadhan
having an exit $exit_val in the function will not have any effect with respect to the function

it is the value that is echoed is caught in the variable.

just the echo statement would do
An exit inside a function will make the script to exit.

See this.

Code:
[/tmp]$ cat try.sh
#! /bin/sh
func1 ()
{
        return 1
}
func2 ()
{
        return 0
}
func3 ()
{
        exit 1
}
func1
status=$?
echo "Expecting 1 -> Got $status"
func2
status=$?
echo "Expecting 0 -> Got $status"
func3
status=$?
echo "Unreachable echo" 
[/tmp]$ ./try.sh
Expecting 1 -> Got 1
Expecting 0 -> Got 0
[/tmp]$

Also instead of echo'ing the exit status, the OP can use return. Unless ofcourse he runs the script as
Code:
. script

# 7  
Old 07-18-2006
vinog,

then what abt this...

i think i am messing up things really today..


Code:
>cat fun.ksh

Code:
#! /usr/bin/ksh

function abc {
echo "inside the function "
ty=1
exit $ty
}

echo "before the function abc"
val=`abc`
echo "val is $val"
echo "after the function abc"

exit 0

Code:
>output

Code:
before the function abc
val is inside the function
after the function abc

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing bash file with sudo for the second time, leads to permission denied, for some commands

I have a script that checks if the script has been ran with sudo. If the script is not ran as sudo, the current script is being executed with exec sudo bash. You are asked for a password, you type in the password, success. Everything is perfect - the commands inside the script are ran as sudo.... (1 Reply)
Discussion started by: boqsc
1 Replies

2. AIX

C Exit Not Executing

Greetings all, I am using an enterprise utility for document archiving. This program calls a custom C exit to do data manipulation before loading data into the archive. When the program is called via an automation script (Ran as ROOT), program successfully executes the C exit, and gives me an... (3 Replies)
Discussion started by: jeffs42885
3 Replies

3. Programming

Fork and Execvp Not Executing Bash Commands From C correctly.

I had been looking at page 75 of this online book: http://richard.esplins.org/static/downloads/linux_book.pdf I've used the system function in C to call bash commands before, but wanted to learn this way too. The solution in the book worked perfectly. However, I tried changing the simple "ls -l... (3 Replies)
Discussion started by: Azrael
3 Replies

4. Shell Programming and Scripting

Executing 'exit' command from shell script

Hi, I am writing shell script to automate few use cases for CLI interface. We have CLI interface which has bunch of commands. I am trying to execute one of the commands 'exit' as part of automation to exit from CLI object (not from shell script) in my shell script. My intension is to execute... (4 Replies)
Discussion started by: Mahesh Desai
4 Replies

5. UNIX for Dummies Questions & Answers

Execute shell script and if string found while executing then exit

Hi All, I have one shell script start.sh which executes another shell script test.sh something like below :test.sh -param1 -param2 In the test.sh there is one command for removing file:rm file1.bak I want whenever I execute start.sh, it will execute test.sh and if it finds string rm... (7 Replies)
Discussion started by: ORAI
7 Replies

6. Shell Programming and Scripting

OSX, bash, cat with <<MARKER executing commands

I have a script that writes another script with cat >/usr/local/bin/myscript.sh <<EOF #!/bin/sh VAR=`run a command here` EOF Problem is, after this is run, I get: $ cat /usr/local/bin/myscript.sh #!/bin/sh VAR=result of command How do I stop that from happening with Macs... (2 Replies)
Discussion started by: jnojr
2 Replies

7. Shell Programming and Scripting

Executing multiple processes without waiting for their exit status.

Hello Friends, Hope you are doing well. I just need a help in executing multiple processes. I've written a shell script which calls another scritps. But the problem is there are too many processes to run, and each process takes about a min to finish its execution. So, I want to just... (3 Replies)
Discussion started by: singh.chandan18
3 Replies

8. UNIX for Dummies Questions & Answers

nohup - sub job in script not executing until I exit

My job is launched using this command: I'm at home and having VPN drops so I used nohup and background. nohup perf_mon -c rating_4_multi,cfg & The main script is PID 26119, and the sub task under it is 26118 which is not running - just sits there. 26119 runs forever but nothing else runs. I... (2 Replies)
Discussion started by: ido1957
2 Replies

9. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies
Login or Register to Ask a Question