Shell Script Doubts

Hello please kindly solve these doubts i have about the following scripts

1
2
3
4
5
6
7
8
9
10
Script 1
//Shell script that accepts arguments and prints them in reverse order//
echo "number of arguments"
len=$#;
while [ $len -ne 0 ]
do
eval echo \$$len
len=`expr $len - 1`
done
Output
$sh test.sh a b
b
a
1.what is \$$len.what is the significance of the escape character '\'


Script 2
1
2
3
4
5
6
7
8
9
10
11
12
//Bundle shell script that accepts file name as arguments and create a shell scipr that has the file as well as the code to recreate the files.Thus if the script generated by your script is executed it would recreate the original file//

echo "#to bundle ,sh this file"
for i in $*
do
echo "echo $i 1>&2"
echo "cat > $i <<'end of $i'"
cat $i
echo " end of $i"
done

Output
$cat f1
hi
$cat f2
hello
$sh test.sh f1 f2 >new.sh
$rm f1 f2
$sh new.sh
f1
f2
1.Even though the 3rd line is a simple print statement if i write something different i get the following error message "new.sh: line 1: to: command not found".Can u tell me why do i get this message
2.In the 6th line what does 1>&2 mean
3.Is "end of" a command and what does "<<" mean.
Last edited on
In script 1, the \ is escaping the first $, so that the eval statement won't use it as a variable.

In script 2:

1. Perhaps you get this error because // is not a valid comment.
2. This redirects standard out to standard error
3. This is known as a "hear" document and it reads from your script until it sees what you specify -- in this case, the string 'end of $i'.

(Thanks ne555!)
Last edited on
1>&2 redirects stdout to stderr
But you are only printing it.
can any one tell me what this line do

len=$#;

sorry i am new to lunix ... scripting .
@kooth,@ne555
Thanks for the reply guys.But i just want to know...
In script 1, can \$$len be written as $($len)

In script 2, what is the use of '&'operator in 1>&2 and why should we redirect to standard error in that script.

@bluecoder
$# is a shell variable which stores the number of arguments entered.See the o/p of script 1 and notice it is 2.
while [ $len -ne 0 ] not working for me .. saying ..command not found .
please advice. .
and thanks @Shynash21 , i got it now .
Please post your complete script.
1
2
3
4
5
6
7
8
#!/bin/bash
echo "Enter the numbers of arguments " 
len=$#
while[ $len -ne 0 ]
do
echo $len
len='expr $len - 1'
done		


this is my complete script .
Please indent your code.
while [ note the space
Also len=`expr $len - 1` backsticks. Or len=$(expr $len - 1) if you prefer

In script 2, what is the use of '&'operator in 1>&2 and why should we redirect to standard error in that script.
Because that's the syntax. Take &2 as an alias to /dev/stderr
If you wrote 1>2 then you will be redirecting to a file named '2'.

¿why are you doing that? It's your program, you should know. Maybe you just want to show error messages
@ne555 Hi thanks for the reply

"¿why are you doing that? It's your program, you should know. Maybe you just want to show error messages "

Its not my program.it's one of the scirpt i read from a book and didn't understand so i wanted to know the logic of it.
Topic archived. No new replies allowed.