Problem in checking function return value
Hello All,
I am newbie for linux. I have problem for checking if the function executes correctly or not. #!/bin/bash function test() { echo "Function executed" return 0 } if [ test $? == "0" ] then echo "Success"; #Ends i want to check the function execution with in the if condition. Thanks in Advance. |
Be careful using 'test' as a function as there is bash command called 'test' (the bash command is actually the same thing as square brackets '[ ]'.
You need to run the function first, then perform the if statement on its return code($?) When comparing numbers with the 'test' command (aka '[ ]'), you use special tags: [ 0 -eq 0 ] means 0 == 0 [ 0 -lt 0 ] means 0 < 0 [ 0 -gt 0 ] means 0 > 0 [ 0 -le 0 ] means 0 <= 0 etc also, 'if' statements need to be completed with 'fi' |
First of all: Using 'test' as a name for a function, variable, or script-file is not a good idea. This is because there is nearly always a program called 'test' installed in UN*X / Linux system as a part of the system.
So, in the scripts below I changed the name of your function to 'mytest'. Two possible scripts for what you are trying to do. (but you mixed the two ways, which doesn't work) : Code:
#!/bin/bash Code:
#!/bin/bash |
Thanks to all
More thanks to all.
Now i realize, this is a silly question. I will do more home works. |
All times are GMT -5. The time now is 03:22 PM. |