Sponsored Content
Homework and Emergencies Homework & Coursework Questions Shell Scripting with Arguments Post 302686617 by bakunin on Wednesday 15th of August 2012 03:08:34 AM
Old 08-15-2012
You described yourself as "done some C++" in your original thread and i suppose it is the difference between shell scripting and high level languages, which makes this hard to understand for you:

A single line in a HLL is read by a parser, then compiled to object code and finally this object code is executed. All the interpretation of the code is done at compile time and all the execution is done at run time. This is fundamentally different with shell scripting.

The big difference is that the interpretation part and the execution part takes place at the same time and in fact is a tightly interwoven process. The following, for instance, has no counterpart in any high-level language:

Code:
cmd="ls -l"
$cmd

In line one a variable is created and filled with a certain content. In the second line this variable is executed as command! Well, in fact not the variable is executed, but the command is: when the shell has to execute this line the first step is to "expand" all the variables, which means to literally replace them with their values. Only then the resulting line is executed.

This makes shell code very versatile (it was created with the purpose of "being able to quickly whip together something" in mind).

This evaluation process does not stop at variables. You sure have done something like this already:

Code:
ls -l *txt

The asterisk is also interpreted by the shell. It is replaced ("expanded") to the list of files matching the pattern (in this case "anything ending in 'txt'").

Consider the following line:

Code:
cmd="ls -l *"
$cmd

This is what the shell will do:

Code:
cmd="ls -l *"
$cmd                           # first, replace the variable with its content
ls -l *                        # now replace the wildcard with the list of files
ls -l a.file b.file c.file     # finally execute this

(i supposed to have the three files "a.file", "b.file" and "c.file" in this directory)

The only way to prevent the shell from doing this expansion is to quote: use double quotes to prevent it from expanding wildcards (which is why it hasn't expanded the asterisk in line 1 already) and single quotes to prevent expansion of variables:

Code:
var="ls *"

echo $var          # results in "ls a.file b.file c.file"
echo "$var"        # results in "ls *"
echo '$var'        # results in a literal "$var"

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Arguments to a shell program

Hi List, Is it possible to pass one argument to a shell program eg) there is a shell program abc which takes one arguments abc one Due to some reasons I pass abc one two Now one,two must be considered as "one" argument to the shell programs. Any suggestions,hints are welcome. ... (3 Replies)
Discussion started by: csvenkata
3 Replies

2. Shell Programming and Scripting

Shell script with arguments

Hi All, I need some help/ideas in coming up with a shell script. Basically, the script should install 1 or 2 or 3 packages based on the input arguments. For example, if I type in pkgscript.sh a1 a2 a3, it should install all the 3 scripts and pkgscript.sh a1 should install only a1. If a... (3 Replies)
Discussion started by: sankar6254
3 Replies

3. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

4. Shell Programming and Scripting

Bash Shell - # of arguments

Hi!, How can I check the number of arguments passed from the shell in a bash shell ? (1 Reply)
Discussion started by: DNAx86
1 Replies

5. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

6. Shell Programming and Scripting

Using arguments in Shell script

Hello, I have to make a shell script doing that : the program tests if there is an argument, if there is it checks whether this is a directory or not, If it is it opens it. for any .c file in the directory it prints 2 lines in the screen : the dependence line of the .o and compiler commend... (1 Reply)
Discussion started by: dekl
1 Replies

7. Shell Programming and Scripting

sub arguments to shell script

Hi, I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit. What I am trying to do is run the shell script with the argument passed in however it does not seem to work. This is what I did ./test.sh 1 Instead it printed the line... (6 Replies)
Discussion started by: aqua9
6 Replies

8. Shell Programming and Scripting

Shell scripting with passing arguments

Hi All, I am using the script for creating local queue and passing the arguments while running the script as below n=0 while do e=`expr $n + 3` echo 'DEFINE QL('$e') MAXDEPTH('$6') MAXMSGL('$7') DEFPSIST('$8') '$9'' | /apps/mqm_opt/bin/runmqsc $2 n=`expr $n + 1` done Running the... (5 Replies)
Discussion started by: Anusha M
5 Replies

9. Shell Programming and Scripting

Scripting with arguments for ping command

This is the script I already have but I have problems with two arguments the first argument -t , I want to count 200 by the last digit of the IP address for example when I run the script ./ping.sh -t 17, the output would be192.168.0.217 is upThe second arguments --up won't work. Could anybody... (1 Reply)
Discussion started by: Roggy
1 Replies

10. UNIX for Beginners Questions & Answers

Beginner at bash scripting - need help with passing arguments

I at the moment, making a simple bash script, capable of setting up an workspace for me, so i don't have to do it manually.. Problem is though i can't seem to provide the bash script any argument, without running into my error checks, checking for input... Here is the code: #!/bin/bash... (7 Replies)
Discussion started by: kidi
7 Replies
All times are GMT -4. The time now is 06:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy