Help understanding a program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help understanding a program
# 1  
Old 03-19-2006
Help understanding a program

I ran the following program. I don't understand some of it.

#!/bin/sh
case $1 in
--test|-t)
echo "you used the --test option"
exit 0
;;
--help|-h)
echo "Usage:"
echo " myprog.sh [--test|--help|--version]"
exit 0
;;
--version|-v)
echo "myprog.sh version 0.0.1"
exit 0
;;
-*)
echo "No such option $1"
echo "Usage:"
echo " myprog.sh [--test|--help|--version]"
exit 1
;;
esac

echo "You typed \"$1\" on the command-line"

-----------------------------------------------------------------------------

Do I need a program called '' myprog.sh '' to run this program?

I got the following message when running it.

You typed "" on the command-line.

The name of the program is '' temp25 '' .


[nissanka@c83-250-111-20 ~]$ chmod 755 temp25
[nissanka@c83-250-111-20 ~]$ ./temp25
You typed "" on the command-line

--------------------------------------------------
I have a file called '' Cisco ''

[nissanka@c83-250-111-20 ~]$ ./temp25 Cisco
You typed "Cisco" on the command-line
[nissanka@c83-250-111-20 ~]$


Could you please tell me how to run this program and how it works?
# 2  
Old 03-20-2006
You are running the program alright. You do not need anything else to run this program.
What this program does is check for what options were passed in to it. It does this by checking for $1 (the first option/arg passed) in the case statement. If you send in any of the options checked for in the case statement, it will execute the commands that are below each of the cases upto the ';;' i.e.
If you run the program as "./temp25 --test", the commands executed will be 'echo "you used the --test option"
exit 0',
and so on.

If you send in as arguments any thing that is not present in the case statement (without a leading '-'), it will just execute the last statement in the script:
Code:
echo "You typed \"$1\" on the command-line"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Shell Programming and Scripting

Need your help in understanding this

Hi, I found this in a script and I would like to know how this works Code is here: # var1=PART1_PART2 # var2=${var1##*_} # echo $var2 PART2 I'm wondering how ##* makes the Shell to understand to pick up the last value from the given. (2 Replies)
Discussion started by: sathyaonnuix
2 Replies

3. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

4. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

5. UNIX for Dummies Questions & Answers

understanding {%/*}/

Hi Gurus: I am trying to understand the following line of code.I did enough of googling to understand but no luck.Please help me understand the follow chunk of code: X=$0 MOD=${X%/*}/env.ksh X is the current script from which I am trying to execute. Say if X=test.ksh $MOD is echoing :... (3 Replies)
Discussion started by: vemana
3 Replies

6. Shell Programming and Scripting

need help understanding mv

I just started shell coding and I'm a bit confused on how 'mv' works can someone explain to me how it works and if i did this correctly. Thanks. echo "Enter Name of the first file:" read file1 #echo $file1 if ; then echo "Sorry, file does not exist." exit 1 ... (16 Replies)
Discussion started by: taiL
16 Replies

7. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

8. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

9. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

10. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies
Login or Register to Ask a Question