LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   return and exit (plus more questions about shell scripting) (https://www.linuxquestions.org/questions/linux-newbie-8/return-and-exit-plus-more-questions-about-shell-scripting-937730/)

ofer4 04-02-2012 02:13 PM

return and exit (plus more questions about shell scripting)
 
hi

i am using unix bash..

please someone can explain me what is the different between return and exit?

and i write "exit 1"

is it exit from the program and also return 1?

thanks alot

David the H. 04-02-2012 02:37 PM

exit is for forcing the termination of and specifying the exit status of scripts. return is used for defining the exit status of functions. Otherwise they pretty much work the same way. The number specifies the exit value passed back to the parent process.

Check out their entries in the BUILTINS section of the bash man page.

ofer4 04-02-2012 03:00 PM

hi

thanks alot,

i have one more question

when i try to define a new variable like:

var="hello"

its write to me a error message:

"var=hello: Command not found."

i also tried to write :

var=hello (without " ")

however it has the same problem...

what is the right correct formula to define a new variable in bash?


question 2:

if i want to execute valgrind on my program

valgirnd ./ prog 4

how can i do a script that check the output of the valgrind and diplay "V" when there is no error else display "0"?

thanks alot i really appreciate it

David the H. 04-02-2012 03:34 PM

variable=value is the correct syntax. There can be no spaces around the equal sign.

A variable name can only have letters, numbers, and underscores (a-z,A-Z,0-9,_), and cannot start with a number. If the value is a simple word then it doesn't need quoting. But if it contains spaces or shell-reserved characters, then it needs to be quoted so that the command treats it as a single literal string.

Code:

var1=foobar
var2="foo bar"
var3='foo|bar'

If you still get errors, please post the exact lines you're using, along with the output, so we can see what you are doing wrong.


I recommend you read through this guide. It will cover all the basic things you need to know for bash scripting:

http://mywiki.wooledge.org/BashGuide

ofer4 04-02-2012 03:37 PM

hi david

that is exacly what i did:

"~/ws2>var=hello
var=hello: Command not found.
"

i attached for you the exact lines in the program...i have no any spaces..

also, can you answer please my second question:

question 2:

if i want to execute valgrind on my program

valgirnd ./ prog 4

how can i do a script that check the output of the valgrind and diplay "V" when there is no error else display "0"?

thanks alot i really appreciate it

ofer4 04-02-2012 04:10 PM

hi

i also attached pic..

please help me..i really dont know what is the problem

http://img853.imageshack.us/img853/1707/35233026.jpg

Uploaded with ImageShack.us

ofer4 04-02-2012 05:08 PM

HI

i understand where am i wrong...

please, someone can help me with this 2 questions:

1.


if i want to execute valgrind on my program

valgirnd ./ prog 4

how can i do a script that check the output of the valgrind and diplay "V" when there is no error else display "0"?


2.

if i have a word

"some description for the test@program_name@inout/file.in@inout/file.out "

and i want to seperate the word beteen the @...

how can i do it?

thanks alot

chrism01 04-02-2012 05:59 PM

1. actually, the default convention is 0 = no error, so
Code:

valgrind ./prog 4
if [[ $? -eq 0 ]]
then
    3 no err occurred
    echo "V"
else
    echo "0"
fi

2.
Code:

echo "some description for the test@program_name@inout/file.in@inout/file.out "|awk -F'@' '{print $1, $2, $3, $4}'
Some handy links:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://www.grymoire.com/Unix/Awk.html

ofer4 04-03-2012 01:09 AM

hi

thanks alot

i want that the valgrind will have a input and the output will execute into $1 and the input is $1, so what i wrote is:

valgrind ./"$2" 4 < $3 > $1

is this syntax right?

thanks

David the H. 04-03-2012 01:49 AM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Quote:

Originally Posted by ofer4
i understand where am i wrong...

Does this mean you found out why the variables wouldn't set? If so, could you please tell us about it? It might help someone else later having the same problem.

Quote:

if i have a word

"some description for the test@program_name@inout/file.in@inout/file.out "

and i want to seperate the word beteen the @...

how can i do it?

There are several ways this can be accomplished. The "best" one to use depends on exactly what you need it for, but in general I recommend splitting the string into an array. The read command is one easy way to do that:

Code:

$ IFS='@' read -a arrayname <<<'some description for the test@program_name@inout/file.in@inout/file.out'

$ printf '%s\n' "${arrayname[@]}"        #print all array indexes on separate lines
some description for the test
program_name
inout/file.in
inout/file.out

Here's a good breakdown of bash's built-in string manipulation features:

http://mywiki.wooledge.org/BashFAQ/100


Quote:

i want that the valgrind will have a input and the output will execute into $1 and the input is $1, so what i wrote is:

valgrind ./"$2" 4 < $3 > $1

I don't know anything about valgrind, but $2 is a filename option to the command, standard input is coming from $3 and standard output is going to $1. Most programs aren't designed to accept input both from a direct file option and stdin at the same time. Are you sure you have the usage right here?


You generally cannot use the same file for both input and output redirection at the same time. You'll usually end up with an empty file!

The Input and Output section of the BashGuide I gave above explains the general usage of redirections (and also the here string I used with the read command above).


Note also that you should double-quote all of your variable expansions e.g. "$1".

ofer4 04-03-2012 04:22 AM

tnx you all

i have one more problem:

i wrote a script "testAll"

Code:

#!/bin/bash

## script name: testAll

if [[ -f "logs" ]]; then
        echo "such directory exist"
        exit 1
else
        mkdir logs
fi
count=0
while read line; do
       
        `echo $count: $line | cut -f1 -d@`
        testCompile "$cut -f2 -d@" logs/"$count".compile.log
        count=count+1

done

and testcompile is a program with his first field is getting a program name and the second field is the log name file which the script testcompile need to create...

Code:

#!/bin/bash

## script name: testCompile

gcc "$1.c" -Wall -g -o $1

testCompile > $1 2>&1

fail=$?

if [[ fail -eq 0 ]]; then echo "Compile V"

else echo "Compile X" fi

however its not run for me and wrote

Code:

~/tmpdir>testAll tests.lst
mkdir: cannot create directory `.logs': File exists

testAll: line 15: 0:: command not found
gcc: cut -f2 -d@.c: No such file or directory
gcc: no input files
./testCompile: line 7: $1: ambiguous redirect
./testCompile: line 18: syntax error: unexpected end of file

please help me to find the problem...thanks alot

ofer4 04-03-2012 02:54 PM

hi

i resolved all the problem in my scripts..

however, now i have another problem..

i wrote a script which perform valgrind check on a program and display "Memory X" when there is a memory leakage or another problem and display "Memory V" when there is no any problem

the script run without any error however it allways display "memory V" even when i did a program which has a memory leakage...

can someone help me please?

Code:

#!/bin/bash

## script name: testMem

valgrind --tool=memcheck ./"$2" < "$3.in" > "$1.out"
if [[ $? -eq 0 ]]; then
        echo "Memory V"
        exit 0
else
        echo "Memory X"
fi

in the summary of the valgring its even wrote to me that happened a memory leakage....

chrism01 04-03-2012 11:52 PM

That's because you are checking the success/failure of the valgrind prog.
If valgrind (itself) doesn't crash or otherwise fail (not your code; valgrind itself), then it will return zero, that's the *nix way.
You probably need to parse the output instead to get what you want...

ofer4 04-04-2012 09:20 AM

ok thanks alot i solved the problem...thank you

now i encouter another problem...i wrote a script:

Code:

#!/bin/bash

## script name: testAll

if [[ -d "logs4" ]]; then
        echo "such directory exist"
        exit 1
else
        mkdir logs4
fi

count=1

count2=0

cat $1

while read line; do
       
        description=`echo $line | cut -f1 -d@`
       
        prog=`echo $line | cut -f2 -d@`
       
        in=`echo $line | cut -f3 -d@`
       
        out=`echo $line | cut -f4 -d@`
       
        echo "$count": "$description"
        echo "$prog"
       
        testCompile "logs/"$count".compile.log" "$prog"
       
                if [[ $? -eq 0 ]]; then
                       
                    (( count2++ ))
                fi
       
        testInOut "logs/"$count".inout.log" "$prog" "$in" "$out"
       
                if [[ $? -eq 0 ]]; then
                       
                    (( count2++ ))
                fi
       
       
        testMem "logs/"$count".memory.log" "$prog" "$in"
       
                if [[ $? -eq 0 ]]; then
                       
                    (( count2++ ))
                fi
       
        (( count++ ))
done

and the output is this:

Code:

~/tmpdir>testAll tests.lst
hello world@helloworld@/dev/zero@inout/helloworld.out
hello world with warnings@warnings@/dev/zero@inout/helloworld.out
echo test@myecho@inout/echo.in@inout/echo.out
echo failtest@myecho@inout/echo.in@inout/helloworld.out
memleaks pass@memleaks@inout/memleaks.6.in@inout/memleaks.6.out
memleaks fail@memleaks@inout/memleaks.11.in@inout/memleaks.11.out


1:

gcc: .c: No such file or directory
gcc: no input files
Compile X
./testInOut: line 5: .in: No such file or directory
diff: logs/1.inout.log.out: No such file or directory
diff: .out: No such file or directory
Inout X
./testMem: line 5: .in: No such file or directory
==16376== Memcheck, a memory error detector.
Memory X

as you can see the script doesnot cut the fields "gcc: .c: No such file or directory"...

for example the script need to cut the first filed in the file $1 like "helloworld" and then to execute some scripts on this program..however i dont know what is it not work for me?

i will be really happy for advices

thanks alot

chrism01 04-04-2012 08:11 PM

You need to read+digest those links in my post #8.
In particular (for this latest qn) concentrate on the syntax for a 'while' loop.
Basically, you're not feeding anything input into it, so $line is undefined.
You could amend the top of your script to
Code:

#!/bin/bash
set -xv

The set cmd will show you clearly what is happening.


All times are GMT -5. The time now is 05:38 AM.