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:
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:
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.
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.:
If you wanted to display the help info from "help" and * then that would benefit from a function.
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:
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):
Last edited by CRGreathouse; 03-23-2009 at 10:42 PM..
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.
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.)
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.
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)
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)
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)
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)
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)
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)
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)