Learning scripting -- functions capturing output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Learning scripting -- functions capturing output
# 1  
Old 03-23-2009
Learning scripting -- functions capturing output

Hi, everyone! First post here. I just assembled my first Linux computer and I'm trying to teach myself shell scripting. (Xubuntu and bash, respectively, but I'm not tied to either.)

I'm writing a program to deal with different kinds of files (to be executed from a text editor -- LaTeX'ing a .tex, compiling a .c or .cpp, etc.). This is partially because I want to have those functions, but also as a learning exercise.

To avoid redundancy I'd like to collect the handling for each type of file together, regardless of how I determined its type (by extension, header, or some other method). In a Windows batch file I'd use GOTO:
Code:
IF "%1"=="help" GOTO Help
. . .
:Help
. . .
GOTO end
. . .
:end

but it looks like there's no goto in bash.* At first it seemed simple: shell scripts support functions, so just make a function for each:
Code:
function Help {
. . .
}
. . .
if [ "$1" = "help" ]; then
  Help
. . .

but functions capture whatever I echo.

1. If I wanted to implement this with functions, how can I get around the capturing? I'd guess that one of the redirection commands would work -- but I've had no luck so far. (It's only my first day, be gentle with me here!)
2. Subjectively, what's the 'right' or 'best' way to do what I want? Functions seem somewhat heavyweight when in many cases I'll just want to do a single line.

* Yes, I'm familiar with Dijkstra's diatribe.
# 2  
Old 03-23-2009
A function that includes echoes should still echo to the terminal.
Yes for one liners functions are no normally used, for your example a case statement might be more elegant, e.g.:
Code:
case "$1" in
  "help")
     echo "This is how you use this script, valid options are help and doit."
  ;;
  "doit")
      echo "Do your stuff here"
  ;;
  *)
      echo "You did not supply a valid option, try: $0 help."
      exit 99
  ;;
esac

If you wanted to display the help info from "help" and * then that would benefit from a function.

HTH
# 3  
Old 03-23-2009
Quote:
Originally Posted by TonyFullerMalv
A function that includes echoes should still echo to the terminal.
I experimented around a bit. They do echo to stdout if they don't have an exit value:
Code:
function foo {
    echo -n hi 

    # Uncomment this line to change the output from "hi again hi " to "again ".
    #exit 0
}
foo
echo again 
foo

but then I can't use them to pass along success/failure information. Right now the relevant part of the script would be something like this (short version, in function form):
Code:
recognized=1;

exec_c () {
  gcc -O3 "$file" -o"$tfile"
}
exec_png () {
  gpicview "$file"
}
cleanup () {
  echo All done!
}

case "$full_ext" in
  "gp.c")
    gp2c-run "$file"
  ;; *)
    recognized=0
esac
exit_status=$?
if [ recognized -eq 1 ]; then
  cleanup
  exit $exit_status
fi

recognized=1
case "$ext" in
  "c" | "cpp")
    exec_c
  ;; "png")
    exec_png
  ;; *)
    recognized=0
  ;;
esac
 exit_status=$?

if [ recognized -eq 1 ]; then
  cleanup
  exit $exit_status
else
     head4=`head -c4 "$file"`
  png=`echo -e '\0211PNG'`

  if [ "$head4" = "$png" ]; then
    exec_png
  fi
fi


Last edited by CRGreathouse; 03-23-2009 at 10:42 PM..
# 4  
Old 03-23-2009
Functions will echo as long as there is some command inside them which writes to stdout. If you want to suppress that, you can redirect output to /dev/null. It has nothing to do with exit code.

You can use "return <exit_code>" to pass around success/failure information. Using "exit <exit_code>" will quit the script altogether. Once you return, you can check $? to check for success/failure.
# 5  
Old 03-23-2009
Quote:
Originally Posted by rikxik
Functions will echo as long as there is some command inside them which writes to stdout. If you want to suppress that, you can redirect output to /dev/null. It has nothing to do with exit code.
I don't want to suppress output, I want to show it.

Quote:
Originally Posted by rikxik
You can use "return <exit_code>" to pass around success/failure information. Using "exit <exit_code>" will quit the script altogether. Once you return, you can check $? to check for success/failure.
I'll try that, thanks. But as you can see (try my example), exit does not exit the script, just the function. (I'm learning by doing; I never would have guessed that it would work that way.)
# 6  
Old 03-23-2009
Quote:
Originally Posted by CRGreathouse
But as you can see (try my example), exit does not exit the script, just the function. (I'm learning by doing; I never would have guessed that it would work that way.)
There are no exit commands in the three functions in your example. You are running exit from main body.
# 7  
Old 03-23-2009
Quote:
Originally Posted by rikxik
There are no exit commands in the three functions in your example. You are running exit from main body.
First example, second comment. You're referring to my code snippet, I'm talking about my short testcase.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

After Learning Shell Scripting

Will it be good to learn 1) Perl or 2) Ruby first? (3 Replies)
Discussion started by: alvinoo
3 Replies

2. Shell Programming and Scripting

I am learning shell scripting and i would like to understand how sort -k3,3 -k 4,4 short* works

Please help me understand how sort -k3,3 -k 4,4 short* works , is it sorting 3 and 4 th field ? what is the use of specifying 3,3 and 4,4 in sort and what is actually does and i see sort -k3 short* and sort -k3,3 -k 4,4 short* giving the same output. Sort output attached Please help (2 Replies)
Discussion started by: Antony Ankrose
2 Replies

3. Shell Programming and Scripting

Capturing Output?

Hello All, I'm writing a Bash Script and in it I execute a piped command within a Function I wrote and I can't seem to redirect the stderr from the 1st pipe to stdout..? I'm setting the output to an Array "COMMAND_OUTPUT" and splitting on newlines using this --> "( $(...) )". By putting... (6 Replies)
Discussion started by: mrm5102
6 Replies

4. Shell Programming and Scripting

Learning Scripting

Hi All, I am facing an issue. I need your advise. I want to take my unix skills to the next level. I want to verse in scripting now. I got some understanding of programming. I did a little bit of C++, Assembly in College. I got some basics in perl. I am wondering if It would be best to... (3 Replies)
Discussion started by: Pouchie1
3 Replies

5. Shell Programming and Scripting

Shell scripting book learning.

Hi Which is the most useful shell scripting book. Planning to learn shell scripting.Thanks in advance. (1 Reply)
Discussion started by: arunvellanki
1 Replies

6. UNIX for Dummies Questions & Answers

Book for Learning unix shell scripting reall good

Hi guys I want o learn and understand shell scripting real good, Ive got already some background Due to the fact that there is ton of books with these theme, can you recommend me some good book with with examples explanations, pls no theoretical books :D Thxs (3 Replies)
Discussion started by: kl1ngac1k
3 Replies

7. Shell Programming and Scripting

Capturing the output of dbv

Hello, We have an oracle database running on a Linux host (RHEL5)...I'm trying to run Oracle dbv (database verify utility) and capture its output to a file using the following syntax but the standart output does NOT get redirected to the file... dbv blocksize=32768 ... (2 Replies)
Discussion started by: luft
2 Replies

8. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

9. Shell Programming and Scripting

Suggested Books for Learning Shell scripting

Hi, I had completed RHCE and i am interested to learn shell scripting. Request you to please let me know which book is best for learning shell scripting or any online website. Thanks & Regards arun (2 Replies)
Discussion started by: Arun.Kakarla
2 Replies

10. Shell Programming and Scripting

Learning Shell Scripting

I'm new to UNIX and I want to learn Shell Scripting. I want a web site that has Shell Scripting tutorials. Any suggesstions?.? (3 Replies)
Discussion started by: liveapple2000
3 Replies
Login or Register to Ask a Question