LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-02-2012, 03:13 PM   #1
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Rep: Reputation: Disabled
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
 
Old 04-02-2012, 03:37 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.

Last edited by David the H.; 04-02-2012 at 03:42 PM. Reason: clarification
 
Old 04-02-2012, 04:00 PM   #3
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
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

Last edited by ofer4; 04-02-2012 at 04:21 PM.
 
Old 04-02-2012, 04:34 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
Old 04-02-2012, 04:37 PM   #5
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Wink

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
 
Old 04-02-2012, 05:10 PM   #6
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-02-2012, 06:08 PM   #7
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-02-2012, 06:59 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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
 
Old 04-03-2012, 02:09 AM   #9
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-03-2012, 02:49 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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".

Last edited by David the H.; 04-03-2012 at 03:21 AM. Reason: destroyed a rogue line and patched some errors
 
Old 04-03-2012, 05:22 AM   #11
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-03-2012, 03:54 PM   #12
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
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....

Last edited by ofer4; 04-03-2012 at 03:56 PM.
 
Old 04-04-2012, 12:52 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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...
 
Old 04-04-2012, 10:20 AM   #14
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-04-2012, 09:11 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie questions with shell scripting, tronky Programming 4 03-21-2011 10:54 AM
shell scripting questions tlarkin Programming 8 05-11-2007 04:49 PM
Shell Scripting Questions blaoulis Programming 5 07-12-2006 08:21 PM
Shell Scripting Exit(s) liguorir Linux - Software 3 04-21-2004 11:59 AM
Two Linux shell scripting questions thepryme Programming 5 10-23-2003 08:01 AM


All times are GMT -5. The time now is 04:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration