Click to See Complete Forum and Search --> : Help with some short bash scripts (was: Explanation needed)


princem3
10-14-2003, 09:14 PM
I've got a couple of short scripts to explain and these should be easy enough for anyone who knows Unix / linux so please help.


Explain the following:


#!/bin/sh
file=$1
shift
vi `grep -l "$file" "$@"`


This one is the real doozy.. but here goes.

Given the following shell scripts


outer
--------------------------------------------------------------------------------

#!/bin/sh
echo $#
inner $*
inner "$@"
inner "$*"

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

inner
--------------------------------------------------------------------------------

#!/bin/sh
echo $#



and the following command lines
--------------------------------------------------------------------------------

prompt> outer
prompt> outer 1 2 3
prompt> outer "1 2 3"
prompt> outer "A B" "C D"
prompt> outer "Hello Class, this is not too hard"
prompt> outer " "

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

Fill in the following table


$# Call to inner with $* | Call to inner with "$@" | Call to inner with "$*"
| |
| |
| |
| |

sploo22
10-14-2003, 09:26 PM
Hints for the first one: ;)

$1, $2, ... are the arguments to the shell script. E.g. if it's run as "script abc foo" then $1 = "abc", $2 = "foo". Also $0 = "script".

$@, I think, refers to all the so-called positional parameters, except $0.

"shift" takes the value of $1 and puts it in $0, $2 -> $1, $3 -> $2, etc.

grep -l xyz file1 file2 ... prints out the names of whichever files (out of file1 file2 ...) contain the string "xyz".

And vi `command` runs the command and edits whichever filenames it outputs.

bryan.6
10-15-2003, 11:01 PM
has your teacher gone over what $#, $@, and $* mean? or is it in your book? those would be good primary resources to check out. also, to fill in that table, are you allowed to run the programs? cause that might help ya, you know?

bsh152s
10-16-2003, 02:14 PM
I'll give you a small hint. $#, $*, $1, ... are all built in shell variables. A good reference for me is O'Reilly's Unix/Linux in a Nutshell. It has sections for bash/ksh.