1. Write a Shell Script to Check Whether a Particular User Has Logged in Or Not
Total Page:16
File Type:pdf, Size:1020Kb
CSE ([email protected] II-Sem) EXP-13 1. Write a shell script to check whether a particular user has logged in or not. If he has logged in, also check whether he has eligibility to receive a message or not. [KRISHNASAI@localhost exp13]$ vi 1check.sh function checkuser { f=0; echo "----------------------------------" for u in `who | cut -f1 -d" " | sort | uniq` do who>userlist if [ $u = $1 ] then echo "$1 is available" echo "$1 is logged in " `grep -c $1 userlist` " terminals " echo "$1 is logged at" grep "$1" userlist|cut -d"(" -f2 f=`expr $f + 1` fi done if [ $f -lt 1 ] then echo "user not available" fi } if [ -z $1 ] then echo "Enter username: " read k checkuser $k echo "----------------------------------" exit 1 fi checkuser $1 exit 1 Output: [KRISHNASAI@exp13]$ sh 1check.sh [KRISHNASAI@exp13]$ sh 1check.sh Enter username: Enter username: KRISHNASAI krishna ---------------------------------- ---------------------------------- KRISHNASAI is available krishna is available KRISHNASAI is logged in 1 terminals krishna is logged in 2 terminals KRISHNASAI is logged at krishna is logged at 192.168.1.191) 192.168.1.166) ---------------------------------- 192.168.1.118) ---------------------------------- bphanikrishna.wordpress.com FOSSLAB Page 1 of 38 CSE ([email protected] II-Sem) EXP-13 2. Write a shell script to accept the name of the file from standard input and perform the following tests on it a) File executable b) File readable c) File writable d) Both readable & writable [krishnasai@exp13]$ vi 2fp.sh Output: echo enter a file name read file [krishnasai@exp13]$sh 2fp.sh if [ -e $file ] enter a file name then h1 echo "$file exists" h1 exists if [ -f $file ] h1 is an ordinary file then h1 does not have read access echo "$file is an ordinary file" h1 has write permission if [ -r $file ] h1 has execute permission then echo "$file has read access" else echo "$file does not have read access" fi if [ -w $file ] then echo "$file has write permission" else echo "$file does not have write permission" fi if [ -x $file ] then echo "$file has execute permission" else echo "$file does not have execute permission" fi if [ -r $file ] && [ -w $file ] then echo "$file has both read and write operations" fi elif [ -d $file ] then echo "$file is a directory" fi else echo "$file does not exist" fi bphanikrishna.wordpress.com FOSSLAB Page 2 of 38 CSE ([email protected] II-Sem) EXP-13 3. Write a shell script which will display the username and terminal name who login recently in to the unix system [KRISHNASAI@Host exp13]$ vi 3.ut.sh today=$(date +"%A") tday=$(date +"%d-%B-%Y") time=$(date +"%r ||%z ") echo "Today is :$today" echo "Today date is :$tday" echo "Time :$time" who>userlist i=0 for u in `who|cut -d" " -f1` do i=`expr $i + 1` done echo "Total number of users are : $i " echo "login user details in `date +"%D %H-%M-%S "`" echo "---------------------------------" echo "USER TERMINAL" echo "=================================" who|cut -d" " -f1>u who|cut -d"(" -f2>t paste u t echo "---------------------------------" Syntax Time only [KRISHNASAI@Host exp13]$ sh 3.ut.sh date +"%FORMAT" Today is :Tuesday Type the following Today date is :10-March-2015 $ date +"%m-%d- command: Time :05:58:18 PM ||+0530 %y" $ date +"%T" Total number of users are : 2 Sample output: login user details in 03/10/15 17-58-18 Outputs: --------------------------------- 02-27-07 USER TERMINAL 19:55:04 ================================= CSESTAFF 192.168.1.219) 149P1A0521 192.168.1.118) --------------------------------- bphanikrishna.wordpress.com FOSSLAB Page 3 of 38 CSE ([email protected] II-Sem) EXP-13 4. Write a shell script to find no. of files in a directory [KRISHNASAI@localhost exp13]$ vi 4.sh f=0 d=0 for i in `ls -l|cut -c 1` do if [ $i = "-" ] then f=`expr $f + 1` elif [ $i = "d" ] then d=`expr $d + 1` fi done echo no of ordinary files are $f echo no of directories are $d [KRISHNASAI@localhost exp13]$ ls 14palendrom.sh 17fact.sh 3data.sh 4.sh kkk [KRISHNASAI@localhost exp13]$ sh 4.sh no of ordinary files are 4 no of directories are 1 bphanikrishna.wordpress.com FOSSLAB Page 4 of 38 CSE ([email protected] II-Sem) EXP-13 5. Write a shell script to check whether a given number is perfect or not [KRISHNASAI@exp13]$ cat [KRISHNASAI@localhost exp13]$ cat 5perfect.sh 5perfecta.sh echo "Enter a number:" n=1 read n while [ $n -lt 500 ] i=1 do sum=0 i=1 while [ $i -lt $n ] sum=0 do while [ $i -lt $n ] if [ `expr $n % $i` -eq 0 ] do then if [ `expr $n % $i` -eq 0 ] sum=`expr $sum + $i` then fi sum=`expr $sum + $i` i=`expr $i + 1` fi done i=`expr $i + 1` if [ $sum -eq $n ] done then if [ $sum -eq $n ] echo "$n is a perfect number" then else echo "$n is a perfect number" echo "$n is not a perfect number" fi fi n=`expr $n + 1 ` done [KRISHNASAI@ exp13]$ sh [KRISHNASAI@exp13]$ sh 5perfecta.sh 5perfect.sh 6 is a perfect number Enter a number: 28 is a perfect number 6 496 is a perfect number 6 is a perfect number [KRISHNASAI@lexp13]$ sh 5perfect.sh Enter a number: 22 22 is not a perfect number Description: A perfect number: a number is perfect when the sum of its divisors (except the number itself) equals the given number. Examples of perfect numbers 6 : The divisors of 6 are 1,2,3 & 6. To show that this is a perfect number we could all the divisors except the number itself 1+2+3 = 6 28: 1 + 2 + 4 + 7 + 14 =28 496 and 8128 are also perfect number. bphanikrishna.wordpress.com FOSSLAB Page 5 of 38 CSE ([email protected] II-Sem) EXP-13 6. Write a menu driven shell script to copy, edit, rename and delete a file [KRISHNASAI@localhost exp13]$ vi 6fop.sh ch=0 while [ $ch -ne 9 ] do clear echo "1.Display current dir" echo "2.Listing the dir" echo "3.Make a dir" echo "4.Copy a file" echo "5.Rename file" echo "6.Delete file" echo "7.Edit file" echo "8.open or display file" echo "9.Exit" echo "Enter your choice" read ch case $ch in 1)echo "Current Dir is : " pwd;; 2)echo "Directories are" ls;; 3)echo "Enter dir name to create" read d mkdir $d echo $d" Dir is created";; 4)echo "Enter filename from copy" read f1 echo "Enter filenm2 to be copied" read f2 cp $f1 $f2 echo $f2" is copied from "$f1;; 5)echo "Enter file name to rename" read f1 echo "Enter new name of file" read f2 mv $f1 $f2 echo $f1" is renamed as "$f2;; 6)echo "Enter any filenm to be delete" read f1 rm $f1 echo $f1" is deleted";; 7)echo "Enter any file to be editing " read f1 vi $f1;; 8) echo "Enter the file name you want to open" read f1 cat $f1;; 9)echo "Have a nice time" exit;; *)echo "Invalid choice entered";; bphanikrishna.wordpress.com FOSSLAB Page 6 of 38 CSE ([email protected] II-Sem) EXP-13 esac echo "are you continue (1 for yes / 0 for No)" read temp if [ $temp -eq 0 ] then ch=9 fi done Output: [KRISHNASAI@localhost exp13]$sh 6fop.sh 1.Display current dir Enter your choice 2.Listing the dir 5 3.Make a dir Enter file name to rename 4.Copy a file kkkk 5.Rename file Enter new name of file 6.Delete file srisai 7.Edit file kkkk is renamed as srisai 8.open or display file 9.Exit Enter your choice Enter your choice 1 6 Current Dir is : Enter any filenm to be delete /home/KRISHNASAI/krishna/foss/exp13 h1 are you continue (1 for yes / 0 for No) h1 is deleted Enter your choice Enter your choice 2 7 Directories are Enter any file to be editing 13delz.sh 15amga.sh Krish Enter your choice Enter your choice 3 8 Enter dir name to create Enter the file name you want to open srisai krish srisai Dir is created alway say i don't have fear are you continue (1 for yes / 0 for No) be a hero bphanikrishna.wordpress.com FOSSLAB Page 7 of 38 CSE ([email protected] II-Sem) EXP-13 7. Write a shell script for concatenation of two strings Program: Output: [KRISHNASAI@exp13]$ cat 7concat.sh [KRISHNASAI@exp13]$ sh 7concat.sh echo Enter first string: Enter first string: read s1 krishna echo Enter second string: Enter second string: read s2 sai s3=$s1$s2 Concatinated string:- krishnasai len=${#s3} krishnasai length:- 10 echo Concatinated string:- $s3 echo $s3 length:- $len 8. Write a shell script which will display Fibonacci series up to a given number of argument. [KRISHNASAI@localhost exp13]$ cat [KRISHNASAI@localhost exp13]$ cat 8fib.sh 8fiba.sh echo enter the number of terms for n in $* read n do f1=0 f1=0 f2=1 f2=1 i=3 i=3 echo "fibonacci seroes up to $n numbers" echo "fibonacci seroes up to $n numbers" echo $f1 echo $f1 echo $f2 echo $f2 while [ $i -le $n ] while [ $i -le $n ] do do f3=`expr $f1 + $f2` f3=`expr $f1 + $f2` f1=$f2 f1=$f2 f2=$f3 f2=$f3 i=`expr $i + 1` i=`expr $i + 1` echo $f3 echo $f3 done done exit 0 done exit 0 [KRISHNASAI@localhost exp13]$ sh [KRISHNASAI@localhost exp13]$ sh 8fib.sh 8fiba.sh 4 enter the number of terms fibonacci seroes up to 4 numbers 9 0 fibonacci seroes up to 9 numbers 1 0 1 1 2 1 2 3 5 8 13 21 awk 'BEGIN {a=0;b=1; while(++x<=10){print a; t=a;a=a+b;b=t}; exit}' bphanikrishna.wordpress.com FOSSLAB Page 8 of 38 CSE ([email protected] II-Sem) EXP-13 9.