Newbie bash scripting help requested


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie bash scripting help requested
# 1  
Old 05-10-2010
Newbie bash scripting help requested

Hi,

I'm very new to bash scripting and Linux in general. I'm running Ubuntu Server 10.04 and trying to write a bash script to launch a program. In doing so, I've come across a couple of things that I obviously don't understand. Here is a short script that exemplifies those things:

Code:
#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

if [ testfunc ]; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

exit 0

The output of this script is:
Code:
testfunc() returned true

My questions:
  1. Why does the 'echo "some text"' within the function not produce any output? (Actually, I can't even tell if the function is being called.)

  2. Why does the function appear to return a true value?

Like I said, there are obviously some things I don't understand. Smilie
# 2  
Old 05-10-2010
Hi.

You never do call the function. Your if statement is testing a literal string "testfunc", which would always be "true" (as a non empty string)

Correct would be:
Code:
#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

if testfunc; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

Or:
Code:
#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

testfunc
if [ $? -eq 0 ]; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

# 3  
Old 05-10-2010
Modify yours test :
Code:
#!/bin/bash

testfunc() {
    echo "Inside function testfunc()"
    return 0
}

if testfunc ; then
    echo "testfunc() returned true"
else
    echo "testfunc() returned false"
fi

exit 0

Output:
Code:
Inside function testfunc()
testfunc() returned true

Jean-Pierre.
# 4  
Old 05-10-2010
Thank you both. However, the results aren't what I'd expect.
Code:
Inside function testfunc()
testfunc() returned true

Shouldn't I be seeing 'false' by returning 0 from the function?
# 5  
Old 05-10-2010
Quote:
Originally Posted by Carson Dyle
Thank you both. However, the results aren't what I'd expect.
Code:
Inside function testfunc()
testfunc() returned true

Shouldn't I be seeing 'false' by returning 0 from the function?
No. A zero exit code is taken to mean "true". See this extract from the man page:
Code:
if list ;then list [ ;elif list ;then list ] ... [ ;else list ] ;fi
              The  list  following  if is executed and, if it returns a zero exit status, the list following the first then is
              executed.

# 6  
Old 05-10-2010
Ah, ok... Is that because the non-empty string is evaluated as 'true', or does it mean that non-zero will be evaluated as 'false'?

I came across something that said that function return values are much like exit codes, so I'm guessing that they're not intended to be used to return the "results" of executing the function. How then would you normally define and use a function to return a boolean value?
# 7  
Old 05-10-2010
A function returns the result (a number) of the last command executed in it (unless you tell it otherwise):

Code:
# cat some_script
function some_function {
 grep x some_file
}

some_function
echo $?

touch some_file
some_function
echo $?

echo x > some_file
some_function
echo $?

# ./some_script
grep: some_file: No such file or directory
2
1
x
0

If you want it to return exactly 0 or 1, then you should code your function to do this, although a non-zero result is generally taken to mean failure.

(to your first statement, change "or does it mean" to "and does it mean", and the answer would be "yes" to both)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newbie needs help with some bash scripting

Please bear with me, I'm a beginner but have had some experience and decent knowledge to understand things as I read them and I'm in the process of trying to learn more. I recently inherited a UNIX server which has a bash script which is part of a crontab schedule that needs to be modified (or... (3 Replies)
Discussion started by: Danylko
3 Replies

2. Shell Programming and Scripting

Bash scripting - Newbie

Hi all, I have drill to do and I'll very appreciate your help: Please create a simple CSV file as follow (3 columns and 4 rows): A,B,C A,”B,C”,D “A,B”,C,D o A,B,”C,D” - Please refer to the comma between the quotation marks as a parameter and not as a separator. - Please provide... (3 Replies)
Discussion started by: elior
3 Replies

3. Shell Programming and Scripting

Shell Scripting Newbie

Hi Guys, I want to create a shell script to run multiple jobs in sequence. Explaination - If I were to run each jobs individually I would have gone to folder - "abin"(where my shellscript is place) as follows cd abin abin > runappeng.sh abc001 Now, I have list of programs which are like... (8 Replies)
Discussion started by: chaits84
8 Replies

4. Shell Programming and Scripting

Scripting needed for newbie

Hi, I am newbie in shell scripting I have a file name like simple.txt which comes from Mainframe systems onto windows dir every 15 minutes daily. File name is same. Every 15 minutes it updates. I need to write shell script to check if the file arrived every 15 min or not. If the new file... (4 Replies)
Discussion started by: chinniforu2003
4 Replies

5. Shell Programming and Scripting

Shell Scripting NEWBIE - Need Help

Could someone please recommend a very good shell scripting book for me. I would be starting a new job that would require a very good understanding of shell scripting. Please help. (3 Replies)
Discussion started by: ayoka
3 Replies

6. Shell Programming and Scripting

scripting newbie... some help please?

hi all, i am just getting in to bash scripting, so don't be too harsh... i've created this little backup script, and it's just awfull... ugly, doesn't work like I want it to, the works. anyways, i was hoping some of you might help me improve it and learn a little in the process. what i... (13 Replies)
Discussion started by: jmd9qs
13 Replies

7. Shell Programming and Scripting

scripting newbie needs help

I have written a script that will email a generic user when a device is "offline". I would like to enhance this by having the script lookup a contact's email and automatically add it to the MAIL_LIST. I am trying to lookup and return data based on a field common in two files File 1 ... (0 Replies)
Discussion started by: irishluck66
0 Replies

8. Shell Programming and Scripting

Scripting Newbie

Seems simple but I am having difficulty with this one: I am trying to write a single command line argument (which will be a path) - the program should print out the owner of the path. I can not get anything I write to run. Please help. (5 Replies)
Discussion started by: Kymmers7
5 Replies

9. UNIX for Dummies Questions & Answers

Shell Scripting Newbie

I'm relatively new at this scripting game, just need to learn some basic stuff for data handling. My current need is to write a script that loops through a textfile of filenames, and for each file removes the first line and re-writes that file to a new name. In fact I could do with knowing... (1 Reply)
Discussion started by: mattyjim2
1 Replies

10. Shell Programming and Scripting

Newbie - Need help in bash scripting

Hi there, I am a student and currently working on a project. I have a file that contains about 50 filenames. (1.txt, 2.txt, 3.txt ...). I would like to know how can I store these filenames into a variable using a loop? I would appreciate if anyone can help me. Thank You. Regards, Bib (4 Replies)
Discussion started by: coolbib
4 Replies
Login or Register to Ask a Question