In: Computer Science
1) Write a shell script to display your name to the screen.
echo "$USER"
---------------------------------------
2) Write a shell script to take a directory as an argument and display the contents of that directory
ls -F ("Dir name)
----------------------------------------
3) Write a shell script that takes any command as a parameter and displays help on that command
help (command)
-----------------------------------------
4) Write a shell script that requires two file names as parameters and copies content of one file into another without asking any questions. The output file should contain a note that it is a copy, name of the original file, current date and time after the content
cp file1 file2
-------------------------------------------
5) Write a shell script that requires a file name as a parameter. Prompts the user if the file should be deleted. If the answer is 'Y' or 'y' and the file exists, it should be deleted. (lookup 'rm' command)
rm -i filename ( i is for inquire)
----------------------------------------------
6) Write a shell script that asks the user to enter a number, prints that number and prints whether that number is odd or even.
echo -n "Enter numnber : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi
----------------------------------------------
7) Write a shell script that prompts for a year of birth. The script should output "Minor" if the user is younger than 18. Otherwise, the script should output "Adult" if the user is younger than 65, "Retired" otherwise
echo -n "Enter your DOB [ddmmyyyy] : "
read n
d=`echo $n | cut -c1-2`
m=`echo $n | cut -c3-4`
y=`echo $n | cut -c5-8`
yy=`date "+%Y"`
mm=`date "+%m"`
dd=`date "+%d"`
if [ $y -le $yy ]
then
yyy=`expr $yy - $y`
mmm=`expr $mm - $m`
if [ $m -gt $mm ]
then
yyy=`expr $yyy - 1`
mmm=`expr $mmm + 12`
fi
if [ $d -gt $dd ]
then
ddd=`expr $d - $dd`
ddd=`expr 31 - $ddd`
else
ddd=`expr $dd - $d`
fi
fi
if [$yyy -lt 18]
echo -n "Minor"
fi
if [$yyy -ge 18 && $yyy -lt 65]
echo -n "Adult"
fi
if [$yyy > 65]
echo -n "Retired"
fi
-------------------------------------------------
8) Write a shell script with an input parameter of a valid file extension. Then the script should print the number of files with that extension in the current directory.
ls -lR /path/to/dir/*("extension) | wc -l
here extension is .mp3,.jpg , .doc etc
---------------------------------------------
9) Write a shell script that takes no more than 5 numbers as parameters and prints the largest of them.
read -p "Enter 1st Number:" num1 read -p "Enter 2nd Number:" num2 read -p "Enter 3rd Number:" num3 read -p "Enter 4th Number:" num4 read -p "Enter 5th Number:" num5 if[ [ num1 -gt num2 ] && [ num1 -gt num2 ] && [ num1 -gt num3 ] && [ num1 -gt num4 ] && [ num1 -gt num5 ]] ; then echo "$num1 is a Greatest Number" elif[ [ num2 -gt num3 ] && [ num2 -gt num3 ] && [ num2 -gt num4 ] && [ num2 -gt num5 ]] ; then echo "$num2 is a Greatest Number" elif[ [ num3 -gt num4 ] && [ num3 -gt num5 ] ] ; then echo "$num3 is a Greatest Number" elif[ num4 -gt num5 ] ; then echo "$num4 is a Greatest Number" else echo "$num5 is a Greatest Number" fi