0% found this document useful (0 votes)
49 views2 pages

Solutions For Assignment 2

The document contains solutions and code snippets for assignments involving shell scripting. It includes scripts to check if a file exists, move a file, grep for a date in a file, loop through months and guess a password. It also lists solutions for part A of another assignment involving date formatting, concatenating files, viewing user accounts, sorting users and listing files by size.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views2 pages

Solutions For Assignment 2

The document contains solutions and code snippets for assignments involving shell scripting. It includes scripts to check if a file exists, move a file, grep for a date in a file, loop through months and guess a password. It also lists solutions for part A of another assignment involving date formatting, concatenating files, viewing user accounts, sorting users and listing files by size.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Solutions for Assignment 2

if test f $1
then
echo file exists
else
echo file does not exist
fi

if test $# -ne 1
echo you must give an argument
else
elif test f $1 o d $1
then
echo file exists
else
echo file does not exist
fi

if test $# -ne 1
echo you must give an argument
elif test f $1
then
echo file exists
mv $1 /usr/$1
echo File moved
else
echo file does not exist
fi

today=`date`
result=`grep $today $1`
if [ "result" ]
then
echo "The file $1 was created today"
else
echo "The file $1 was not created today"
fi

#!/bin/sh
# Start at month 1
month=1
while [ $month -le 12 ]
do
echo "Month no. $month"
month=`expr $month + 1`
done
#!/bin/sh
password="open"
answer=""
while [ "forever" ]
do
echo "Guess the password to quit the program> \c"
read answer

if [ "$answer" = "$password" ]
then
break
fi
done

Solution for Assignment 3

Part A
1. date | cut d f 1,2,3,4,5,6 output-delimiter=$\n or date|tr [:space:] \n
2. paste d -s date.lst
3. tail +11 /etc/passwd
4. sort t : k 3,3 -k4,4 /etc/passwd
5. ls lS | head -5 [s based on file size in blocks
S based on file size in bytes ]
6. tail +5 /etc/passwd |head -5

Part B

You might also like