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.
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'.