Академический Документы
Профессиональный Документы
Культура Документы
#File:fold_m
#Q2)Write a shell program that fold line of text beyond 30 characters.
sed 's/\t/
/g' $* |
#convert tabs to 8 spaces
awk '
BEGIN {
N = 30
# folds at column 30
for ( i =1 ; i <= N; i++) # make a string of blanks
blanks = blanks " "
}
{
if ( (n = length ( $0 ) ) <= N )
print
else {
for ( i = 1; n > N ; n -= N ) {
printf "%s\\\n", substr($0,i,N)
i += N;
}
printf "%s%s\n",substr(blanks,i,N-n), substr( $0,i)
}
}'
#File:cal_m
#Q3)Write a shell program to enhance the inbuild cal program.
case $# in
0) set `date`; m=$2: y=$6 ;; # no args :use today
1) m=$1; set `date`; y=$6 ;; # 1 args : use this year
*) m=$1; y=$2 ::
esac
case $m in
jan* | Jan* )
m=1
feb* | Feb* )
m=2
mar* | Mar* )
m=3
apr* | Apr* )
m=4
may* | Mar* )
m=5
jun* | Jun* )
m=6
jul* | Jul* )
m=7
aug* | Aug* )
m=8
sep* | Sep* )
m=9
oct* | Oct* )
m=10
nov* | Nov* )
m=11
dec* | Dec* )
m=12
[1-9] | 10 | 11 | 12 ) ;;
*)
y=$m;
esac
/usr/bin/cal $m $y
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
m="" ;;
#numeric month
#plain year
#File:existance_test_m
#Q4)Write a shell program to test wheather a particular file exist and
#a directory or not.
ch=y
while [ $ch == y ]
do
printf "\nEnter a file/directory name to search in current directory:"
read name
if [ -e $name ]
then
if [ -d $name ]
then
printf "$name exists and is a directory.\n"
else
printf "$name exists and is not a directory.\n"
fi
else
printf "$name does not exist\n"
fi
echo -n "more(y/n):"
read ch
done
#File:user_search_m
#File:overwrite
or not.
#File:permission_m
done
}'
else
echo -e
fi
fi
#File:dis_arg_sep_m
#Q8)WAP thet read the user input and display each argument separately.
for word in $*
#to read a argument at a time
do
echo $word
#to display it.
done
#File:gross_salary_m
#File:grade_m
#File:linear_search_m
#File:insertion_sort_m
#File:prime_test_m
#Q13)To check whether a no is prime or not
if [ $# -lt 1 ]
then
echo No command line argument is passed. #if no argument is passed
else
for num in $* #for each argument
do
flag=0
i=2
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` == 0 ]
then
#checking a no is divigable or not
flag=1
fi
i=`expr $i + 1`
done
if [ $flag == 0 ]
then
echo $num is a prime.
else
echo $num is not a prime.
fi
done
fi
#File:sorted_user_m
#File:table_m
#File:file_com_m
#File:sum_m
#File:sorted_print_m
#File:cal
#Q22)To modify "cal" command
for m in $@
do
case $m in
jan* | Jan* )
m=1
feb* | Feb* )
m=2
mar* | Mar* )
m=3
apr* | Apr* )
m=4
may* | Mar* )
m=5
jun* | Jun* )
m=6
jul* | Jul* )
m=7
aug* | Aug* )
m=8
sep* | Sep* )
m=9
oct* | Oct* )
m=10
nov* | Nov* )
m=11
dec* | Dec* )
m=12
esac
set `date` ; y=$6
/usr/bin/cal $m $y
done
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
;;
#File:user_search_m
#Q24)To test a name is user or not.
echo -n Enter user not to search:
read user
cat /etc/passwd | grep -w "$user" >/dev/null
if [ $? = 0 ]
then
echo "$user is a valid name."
else
echo "$user is not a validname."
fi
#File:date_m
#Q25)To display date in the mm/dd/yyyy format.
echo -n "Today date is `date +%m`/`date +%d`" #to get mm/dd/
set `date`;echo $6 #to get yyyy