Q.1 Non-recursive shell script that accepts any number of arguments and prints them in the reverse order.
Filename:-> 1a.sh
Source Code:->
echo "number of arguments are: $#"
len=$#
while [ $len -ne 0 ]
do
eval echo \$$len
len=`expr $len - 1`
done
Output:->
$chmod 777 1a.sh
$./1a.sh a b c
Number of arguments are: 3
c
b
a
-----------------------------------------------------------------------------------------------------------------
Q.2 Shell script that accepts two file names as arguments, checks if the permissions for these files are identical and if the permissions are identical, outputs the common permissions, otherwise outputs each file name followed by its permissions.
Filename:-> 2a.sh
Source Code:->
ls -l $1 | cut -d " " -f1 > file1
ls -l $2 | cut -d " " -f1 > file2
if cmp file1 file2
then
echo "Both the files have same permission"
cat file1
else
echo "Both the files have different permission"
echo "The permission of the first file $1 is "
cat file1
echo "The permission of the second file $2 is "
cat file2
fi
Output:->
$chmod 777 2a.sh
$cat > file1
This is the first file
$cat > file2
This is the second file
$./2a.sh file1 file2
Both the files have same permission
-rw-r--r--
$chmod 777 file2
$./2a.sh file1 file2
Both the files have different permission
The permission of the first file file1 is
-rw-r--r--
The permission of the second file file2 is
-rwxrwxrwx
0 comments:
Post a Comment