Home >> Sem 3 >> 630007 - Programming Skills-V (OS) [3/3]
21. Write a script to check whether a given number is...
21 Write a script to check whether a given number is palindrome or not. ****************************************************************** clear echo "ENTER NUMBER :=" read number rem=0 i=10 revnumber=0 for((;number>0;)) do let revnumber=" revnumber * 10 " let rem=" number % 10 " let number=" number / 10 " let revnumber=" revnumber + rem " done echo -e "\nREVERSE NUMBER :-\c" & echo $revnumber ~ ~ ~ ~ ~ ENTER NUMBER :=\c 435621 REVERSE NUMBER :-126534 [divya@linux ~]$ ******************************************************************
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
22. Write a script to display all words of a file in ascending order.
22.Write a script to display all words of a file in ascending order. ****************************************************************** echo " Enter File Name" read fname sort $fname or echo -e "\n enter File name " read fn for i in `cat $fn` do echo $i >> f2.txt done sort f2.txt rm f2.txt ~ ~ [divya@linux ~]$ cat > f1 z x y w n h t r u a b c [11]+ Stopped cat > f1 [divya@linux ~]$ sh 22.sh enter File name f1 a b c h n r t u w x y z
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
23. Write a script to display all lines of a file in ascending order.
23. Write a script to display all lines of a file in ascending order. echo " Enter File Name" read fname sort $fname /* output [mca0914@ctilinux-1 mca0914]$ sh 23.sh Enter File Name second.txt 1 10 2 4 40 5 8 */
24. Write a script to display the last modified file.
#!/usr/bin/env bash # GTU24: Write a script to display the last modified file. # Code written By: Rushyang Darji # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. # Final Build: 19.10.2010 while true; do # -e enables readline, which means you can use tab-completion. & -p prints whatever's written in "" before taking the path # The ''|| exit'' makes the script exit if read returns false, which it # will if the user hits Ctrl+C amongst other. read -e -p "Enter Directory: " path || exit # if path contains an existing directory, break out of this infinite loop. [[ -d $path ]] && break echo "Invalid Path, Try Again!" done cd $path # cd $path is inevitable because of * in use of ls. ls * -dpltr | grep -v '/$' | tail -n1 # Here, observe '*' after ls. You must specify a wildcard pattern for indicating all files first. # This is because the -d option specifies that only directory names should listed. # Moreover, -p puts an indicator at the end of "directories", which will be stripped by grep inverse. # Once we have neglected directories, we can list(-l) "ONLY FILES" from current working directory sorted by it's modified time (-t) in reverse order (-r). The most last one will be fetched by tail. cd $OLDPWD # OLDPWD is the env var, which always remembers our "PREVIOUS WORKING DIRECTORY". Enter `env | grep OLDPWD` to see it. # 'cd -' will also lead us into last working directory. But then also we don't need to print it on Terminal while executing it.
25. Write a shell script to add the statement #include ...
#!/usr/bin/env bash # Code written By: Rushyang Darji # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. # Final Build: 02.10.2010 while true; do read -e -p "Enter Directory: " path || exit [[ -d $path ]] && break echo "Invalid path! Try Again!" done path=${path%/} myargs=`grep -l -e "printf" -e "fprintf" $path/*.c | xargs` if [ $? -gt 1 ]; then # grep exits with status 1 when no matches were found. echo -n "No Matches were found. " && exit fi temp=$(mktemp tmp.XXXXXXXXX) for i in $myargs # Here, grep has the exit status 0. do echo "Do you want to add '#include ' to $i?" read S case $S in Y|y|YES|Yes|yes|yeah) sed '1i\ #include' "$i" > "$temp" # i for insertion, 1 for 1st line. $i is the file to insert. and all output will be redirected to $temp mv "$temp" "$i" # renaming $temp by over writing to $i ;; n|N|NO|no|No|nope) echo "Alright! Next.." shift ;; *) echo "Invalid input." ;; esac done if [ -z $myargs ]; then echo "No Matches were found. Try another Directory" else clear head -n5 $path/*.c | less fi rm $temp ######################## OR ######################## #!/usr/bin/env bash # GTU25: Write a shell script to add the statement #include at the beginning of every C source file in current directory containing printf and fprintf. # Code written By: Rushyang Darji # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. # Final Build: 19.08.2010 # If you want to rename and include all ".C" files too, Refer GTU26.sh myargs=`grep -l -e "printf" -e "fprintf" *.c | xargs` if [ $? -gt 1 ]; then # grep has exit status 1 when no matches were found. echo -n "No Appropriate Matches were found. " fi for i in $myargs # Here, grep has the exit status 0. do echo "Do you want to add '#include ' to $i?" read S case $S in Y|y|YES|Yes|yes|yeah) sed '1i\ #include' "$i" > $$ # i for insertion, 1 for 1st line. $i is the file to insert. and all output will be redirected to $$ (or you can say temp... Actually $$ returns terminal id. but when output is directed to it, a file is made) mv $$ $i # renaming $$ by over writing to $i ;; n|N|NO|no|No|nope) echo "Alright!" shift ;; *) ;; esac done if [ -z $myargs ]; then echo "No Matches were found. Try another Directory" else clear head -n5 *.c | less fi
26. Write a script that behaves both in interactive and...
# !/usr/bin/env bash Rushyang Darji (SVIT -Vasad ) # 26. Interactive - non-interactive shell script to prompt and delete c files within the given or predefined current directory. # # Code Developed By: Rushyang Darji # Init. Build: 06.08.2010 # Last Build: 19.10.2010 N=$# ext=c if test "$N" -eq "0"; then while true; do # Same inifinite loop as we used in GTU24 read -e -p "Enter Path: " path || exit [[ -d $path ]] && break echo "Invalid Path, Try Again!" done path=${path%/} # Removes last / from the end of the path. Though, it's not compulsion to do so because /foo/bar and /foo//////bar is considered exactly the same! for i in $path/*.C do if [ "$i" != $path/'*.C' ]; then # If there is no match, Value of i will be ''$path/*.C''. & That's why there is no need to rename. mv "$i" "${i/.C/}".c # Renames every .C files to .c, so that we can use it afterwards in same loop. clear fi done for i in $path/*."$ext" do if [ "$i" != "$path"/'*.c' ]; then # If there is no ".C FILE" exist in that directory, it will switch to else. clear echo "File is $i" head -n10 "$i" | nl # head for displaying First 10 lines, nl for numbering them on terminal. sleep 1 # Halt for 1 second rm -i "$i" # -i for interactive prompt. # Remember, "" around $i is super necessary! Because except it, you'll get an error with filenames containing spaces. else echo "There are no matching \"C\" files to Prompt in this directory." sleep 2 clear=no fi done if test "$clear" != "no"; then # If clear=no then there are no C Files to display. clear echo "Remaining C files in the Directory..." ls -1 $path/*.c # 1 result per line (-1) fi else # Else part contains, where user passes the name of C files, which should exist in the current working directory as the parameter. for i in $path/*.C do if [ "$i" != $path/'*.C' ]; then # if There are no matches ie if there is no C file in given dir, 'i' will be ''$path/*.C'' mv "$i" "${i/.C/}".c # Renames every .C files to .c clear fi done for i in $* # When filenames are passed as parameters. do clear i="${i/.c/}" # Removes an extension from file variable 'i' Only in the case of extension is also passed within the filename parameter. i="$(pwd)/$i.c" # Makes i the complete path of a file, including extension.. # Last two lines are necessary because user, may and may not enter filename including extension. if [ -f "$i" ]; then # Checks for the existence of given filename, into pwd echo "File name is $i" head -n10 "$i" | nl sleep 1 rm -i "$i" else # Error for non-Existent files. echo "There is no such a file with name: \"$i\" in current working directoy" sleep 3 fi done clear echo "Remaining C files in the Directory..." ls -1 *.c sleep 1 fi
27. Write a script that deletes all leading and trailing spaces...
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
Home >> mca >> Sem 3 >> 630007 - Programming Skills-V (OS) [2/3]
11. Write a script to display the directory in the descending...
11 Write a script to display the directory in the descending order of the size of each file. ************************************************************************************************************************************ echo -n "Enter directory name : " read dname ls $dname > file echo "No of files in directory : $(grep [^*$] file-c)" rm -f file ls | sort -r find . -size +1000c -print or echo Enter Directory read direc cd $direc ls -Sl ~ "111.sh" 5L, 51C written [divya@linux ~]$ sh 111.sh Enter Directory d1 total 28 drwxrwxr-x 3 divya divya 4096 2010-08-10 17:10 jk -rw-rw-r-- 1 divya divya 27 2010-09-18 16:36 second -rw-rw-r-- 1 divya divya 18 2010-10-04 11:50 first -rw-rw-r-- 1 divya divya 0 2010-09-27 16:17 file ******************************************************************
11. Write a script to display the directory in the descending order of the size of each file. echo Enter Directory read direc cd $direc ls -Sl
12. Write a script to implement the following commands:...
13. Write a script for generating a mark sheet after reading...
13. Write a script for generating a mark sheet after reading data from a file. File contains student roll no, name , marks of three subjects. ****************************************************************** count=1 total=0 per=0 rem1=0 wc -l s > temp echo "************************************************************" >>s.out echo " Students Result Are As Follows....." >> s.out echo "************************************************************" >> s.out echo "Rno NAME SUB1 SUB2 SUB3 TOTAL PERCENTAGE GRADE RESULT" >> s.out echo "************************************************************" >> s.out while [ $count -le `awk '{print $1}' temp` ] do let total=0 head -$count s > temp1 tail -1 temp1 > temp2 rollno=`awk '{print $1}' temp2` name=`awk '{print $2}' temp2` sub1=`awk '{print $3}' temp2` sub2=`awk '{print $4}' temp2` sub3=`awk '{print $5}' temp2` let total=$total+`awk '{print $3}' temp2` let total=$total+`awk '{print $4}' temp2` let total=$total+`awk '{print $5}' temp2` echo "STUDENT NAME IS :" `awk '{print $2}' temp2` echo "TOTAL IS :" $total let per=`expr $total / 3` let rem1=`expr $total % 3` if test $rem1 -ne 0 then let per=$per+1 fi echo "PERCENTAGE IS :" $per if [ $per -ge 70 ] then grade="A+" elif test $per -ge 60 then grade="A" elif test $per -ge 50 then grade="B" elif test $per -ge 45 then grade="C" fi echo "GRADE IS :" $grade let count=$count+1 #WRITING DATA INTO THE OUTPUT FILE echo $rollno $name " " $sub1 " " $sub2 " " $sub3 " " $total " " $per " " $grade >> s.out echo "-----------------------------------------------------" >> s.out done rm temp rm temp1 rm temp2 output [divya@linux ~]$ cat s 1 divya 40 30 70 2 Raj 60 40 90 [divya@linux ~]$ sh 131.sh ************************************************************ Students Result Are As Follows..... ************************************************************ Rno NAME SUB1 SUB2 SUB3 TOTAL PERCENTAGE GRADE RESULT ************************************************************ STUDENT NAME IS : divya TOTAL IS : 140 PERCENTAGE IS : 47 GRADE IS : C 1 divya 40 30 70 140 47 C ----------------------------------------------------- STUDENT NAME IS : Raj TOTAL IS : 190 PERCENTAGE IS : 64 GRADE IS : A 2 Raj 60 40 90 190 64 A ----------------------------------------------------- ******************************************************************
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
14. Write a script to make following file and directory management...
#!/usr/bin/env bash # Code written By: Rushyang Darji # Last Updated: 13.08.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. : << -- 14. Write a script to make following file and directory management operations menu based: Display current directory List directory Make directory Change directory Copy a file Rename a file Delete a file Edit a file -- while true; do sleep 1 echo -e "\n" echo -e " 1. Display current directory 2. List directory 3. Make directory 4. Change directory 5. Copy a file 6. Rename a file 7. Delete a file 8. Edit a file 9. Exit Enter your choice: \c" read selection clear case $selection in 1) pwd ;; 2) ls -l ;; 3) echo -n "Enter name of directory you wanna make: " read DIR mkdir "$DIR" if [ "$?" == "0" ]; then echo "Directory $DIR made successfully!" sleep 1 ls -l fi ;; 4) echo -n "Enter the Absolute Path to change the directory: " read -e apath cd $apath if [ $? -eq 0 ]; then echo "Working path changed successfully!" sleep 1 pwd fi ;; 5) echo -n "Enter name of file: " read filename echo -n "Copy where? " read -e apath cp $filename $apath if [ $? -eq 0 ]; then sleep 1 echo "File $filename copied successfully to $apath" fi ;; 6) echo -n "Enter old name of the file: " read oname echo -n "Enter new name: " read nname mv $oname $nname ;; 7) echo -n "Enter filename to delete: " read fdel if [ -f $fdel ]; then rm -i $fdel fi ;; 8) echo -n "Enter filename to open it in Text Editor: " read filename vi $filename ;; 9) clear echo "============ HAVE A NICE DAY ============" sleep 2 clear exit ;; *) echo "Invalid choice, Please try again!: " ;; esac done
14 Write a script to make following file and directory management operations menu based: ****************************************************************** Display current directory List directory Make directory Change directory Copy a file Rename a file Delete a file Edit a file ****************************************************************** clear choice=y while [ "$choice" = "y" ] do echo " MAIN MENU " echo "***************************" echo "1 - DISPLAY CURRENT DIRECTORY" echo "2 - LIST DIRECTORY" echo "3 - MAKE DIRECTORY" echo "4 - CHANGE DIRECTORY" echo "5 - COPY A FILE" echo "6 - RENAME A FILE" echo "7 - DELETE A FILE" echo "8 - EDIT A FILE" echo "9 - EXIT" echo "ENTER THE CHOICE :- " read choice case $choice in 1) echo "DISPLAYING CURRENT DIRECTORY" pwd ;; 2) echo "\nLIST THE CURRENT DIRECTORY" ls -l read ;; 3) echo "\nMAKE A NEW DIRECTORY" echo "\nENTER THE DIRECTORY NAME :-\c" read dirname mkdir "$dirname" read dirname ;; 4) echo "\nCHANGE THE CURRENT DIRECTORY" echo "\nENTER THE DIRECTORY TO WHICH YOU WANT TO read dirname pwd cd .. ;; 5) echo "\nCOPY THE GIVEN FILE TO THE GIVEN DESTINAT echo "\nENTER THE FILE TO COPY :- \c" read file echo "\nENTER THE DESTINATION TO COPY :- \c" read destin cp "$file" "$destin" ;; 6) echo "\nRENAMING THE GIVEN FILE" echo "\nENTER THE FILE TO RENAME :- \c" read file1 echo "\nENTER THE NEW NAME :- \c" read file2 mv "$file1" "$file2" ;; 7) echo "\nDELETING THE GIVEN FILE" echo "\nENTER THE FILE TO DELETE :- \c" read file rm "$file" ;; 8) echo "\nEDITING A GIVEN FILE" echo "\nENTER FILE TO EDIT :- \c" read file vi "$file" :q ;; 9) exit ;; *) echo "Invalid Choice ....." ;; esac echo -e "Do u want to continue.....? [y/n]" read choice case $choice in Y|y) choice=y;; N|n) choice=n;; *) choice=y;; esac done output MAIN MENU *************************** 1 - DISPLAY CURRENT DIRECTORY 2 - LIST DIRECTORY 3 - MAKE DIRECTORY 4 - CHANGE DIRECTORY 5 - COPY A FILE 6 - RENAME A FILE 7 - DELETE A FILE 8 - EDIT A FILE 9 - EXIT ENTER THE CHOICE :- 1 DISPLAYING CURRENT DIRECTORY /home/Raj [Divya@linux ~]$ ENTER THE CHOICE :- 2 \nLIST THE CURRENT DIRECTORY total 96 -rw-rw-r-- 1 Raj Raj 1590 2010-07-26 16:27 14.sh -rw-rw-r-- 1 Raj Raj 21 2010-07-26 16:33 d drwxrwxr-x 2 Raj Raj 4096 2010-07-26 16:28 divya drwxrwxr-x 4 Raj Raj 4096 2010-07-26 16:22 family -rw-rw-r-- 1 Raj Raj 232 2010-07-23 16:39 fib.sh -rw-rw-r-- 1 Raj Raj 171 2010-07-26 16:52 log.sh -rw-rw-r-- 1 Raj Raj 9 2010-07-26 16:15 palin -rw-rw-r-- 1 Raj Raj 288 2010-07-26 16:48 pal.sh -rw-rw-r-- 1 Raj Raj 167 2010-07-26 14:19 pm.sh drwxrwxr-x 3 Raj Raj 4096 2010-07-24 16:38 r -rw-rw-r-- 1 Raj Raj 304 2010-07-26 16:55 time.sh -rw-rw-r-- 1 Raj Raj 264 2010-07-24 16:29 zero.sh ENTER THE CHOICE :- 3 \nMAKE A NEW DIRECTORY \nENTER THE DIRECTORY NAME :-\c divya1 [Divya@linux ~]$ ls 14.sh divya family log.sh pal.sh r zero.sh d divya1 fib.sh palin pm.sh time.sh ENTER THE CHOICE :- 5 \nCOPY THE GIVEN FILE TO THE GIVEN DESTINATION \nENTER THE FILE TO COPY :- \c d \nENTER THE DESTINATION TO COPY :- \c t [Divya@linux ~]$ cat t target +destinatyion ENTER THE CHOICE :- 6 \nRENAMING THE GIVEN FILE \nENTER THE FILE TO RENAME :- \c d \nENTER THE NEW NAME :- \c dest [Divya@linux ~]$ ls 14.sh divya family log.sh pal.sh r time.sh dest divya1 fib.sh palin pm.sh t zero.sh ENTER THE CHOICE :- 7 \nDELETING THE GIVEN FILE \nENTER THE FILE TO DELETE :- \c t [Divya@linux ~]$ ls 14.sh divya family log.sh pal.sh r zero.sh dest divya1 fib.sh palin pm.sh time.sh ENTER THE CHOICE :- 8 \nEDITING A GIVEN FILE \nENTER FILE TO EDIT :- \c dest target +destinatyion rr Insert mode and save [Divya@linux ~]$ cat dest target +destinatyion rr ******************************************************************
************************************************************************************************* pro14. Write a shell script to make the following file and management operations menu based: a) Display the current directory. b) List directory. c) Make directory. d) Change directory. e) Copy of a file. f) Rename a file. g) Delete a file. h) Edit a file. ************************************************************************************************* choice=y while [ "$choice" = "y" ] do echo -e " MENU " echo -e "1. Display the current directory" echo -e "2. List directory" echo -e "3. Make directory" echo -e "4. Change directory" echo -e "5. Copy of a file" echo -e "6. Rename a file" echo -e "7. Delete a file" echo -e "8. Edit a file" echo -e "9. Exit" echo "\n Enter your Choice :- " read choice case "$choice" in 1) pwd;; 2) ls;; 3) echo -e "enter the name of new directory to be created" read dir1 mkdir $dir1;; 4) cd;; 5) echo -e "enter the source and destination files" read f1 read f2 cp $f1 $f2;; 6) echo -e "enter the old and new names of files" read f1 read f2 mv $f1 $f2;; 7) echo -e "which file to be removed" read fname rm $fname;; 8) echo -e "which file should be edited" read fname vi $fname;; 9) exit;; esac echo -e "Do u want to continue.....? [y/n]" read choice case $choice in Y|y) choice=y;; N|n) choice=n;; *) choice=y;; esac done
/* 14. Write a script to make following file and directory management operations menu based: Display current directory List directory Make directory Change directory Copy a file Rename a file Delete a file Edit a file */ echo " 1...Display current Directory" echo " 2...List Directory" echo " 3...Make directory " echo " 4...Change directory " echo " 5...Copy a file " echo " 6...Rename a fIle " echo " 7...Delete a file " echo " 8...Edit a file " echo " Enter Your Choice: " read ch case $ch in 1) pwd ;; 2) ls ;; 3) echo Enter directory name to make read dir_name mkdir $dir_name ;; 4) cd ;; 5) echo Enter two file name to copy read f_name1 f_name2 cp $f_name1 $f_name2 ;; 6) echo Enter file name to rename read fname mv $fname ;; 7) echo Enter file name to delete read fname rm $fname ;; 8) echo Enter file name to edit read fname vi $fname ;; *) echo You hv entered wrong choice ;; esac
15. Write a script which reads a text file and output the following...
15.Write a script which reads a text file and output the following Count of character, words and lines. File in reverse. Frequency of particular word in the file. Lower case letter in place of upper case letter. ****************************************************************** echo -e "\n Enter Filename : " read filen echo "--------------------------------------------------------------" echo " MENU " echo "--------------------------------------------------------------" echo " a) Count the no. of char.s, words, lines." echo " b) Display a file in reverse." echo " c) Count the no. of occurrences of a particular word." echo " d) Convert Lower case to UppeCase" echo "--------------------------------------------------------------" echo -e "\n Enter Your Choice : " read c case "$c" in a) echo -e "\n Total lines,words,char.s " ; wc $filen ;; b) rev $filen read buf;; c) echo -e "\nEnter word to find :" read w for i in `cat $filen` do echo $i >> f1.txt done echo -e "Total no. of words= \c" ;grep -c $w f1.txt grep $w f1.txt rm f1.txt;; *) echo "Invalid choice" esac ~ ~ "15.sh" 28L, 894C written [divya@linux ~]$ sh 15.sh Enter Filename : s -------------------------------------------------------------- MENU -------------------------------------------------------------- a) Count the no. of char.s, words, lines. b) Display a file in reverse. c) Count the no. of occurrences of a particular word. d) Convert Lower case to UppeCase -------------------------------------------------------------- Enter Your Choice : a Total lines,words,char.s 7 22 193 s [divya@linux ~]$ sh 15.sh Enter Filename : b -------------------------------------------------------------- MENU -------------------------------------------------------------- a) Count the no. of char.s, words, lines. b) Display a file in reverse. c) Count the no. of occurrences of a particular word. d) Convert Lower case to UppeCase -------------------------------------------------------------- Enter Your Choice : b rev: b: No such file or directory [5]+ Stopped sh 15.sh [divya@linux ~]$ sh 15.sh Enter Filename : s -------------------------------------------------------------- MENU -------------------------------------------------------------- a) Count the no. of char.s, words, lines. b) Display a file in reverse. c) Count the no. of occurrences of a particular word. d) Convert Lower case to UppeCase" -------------------------------------------------------------- Enter Your Choice : b akram eman on llor 05 ilahsiav 1 04 jariv 2 05 lanos 3 03 layap 4 04 lahen 5 06 layap 6 [6]+ Stopped sh 15.sh [divya@linux ~]$ sh 15.sh Enter Filename : s -------------------------------------------------------------- MENU -------------------------------------------------------------- a) Count the no. of char.s, words, lines. b) Display a file in reverse. c) Count the no. of occurrences of a particular word. d) Convert Lower case to UppeCase -------------------------------------------------------------- Enter Your Choice : c Enter word to find : payal Total no. of words= 2 payal payal [divya@linux ~]$ sh 15.sh Enter Filename : s -------------------------------------------------------------- MENU -------------------------------------------------------------- a) Count the no. of char.s, words, lines. b) Display a file in reverse. c) Count the no. of occurrences of a particular word. d) Convert Lower case to UppeCase -------------------------------------------------------------- Enter Your Choice : d ROLL NO NAME MARKs 1 VAISHALI 50 2 VIRAJ 40 3 SONAL 50 4 PAYAL 30 5 NEHAL 40 6 PAYAL 60 [divya@linux ~]$ ***************************************************************
************************************************************************************************ pro15. Write a script which reads a text file and output the following: I) Count of characters, words, lines. II) File in reverse." III) Frequency of particular word in the file. IV) Lower case letters in place of uppercase alphabets. ************************************************************************************************ echo -e "\n Enter Filename : \c" read filen echo "--------------------------------------------------------------" echo " MENU " echo "--------------------------------------------------------------" echo " a) Count of characters, words, lines." echo " b) File in reverse." echo " c) Frequency of particular word in the file.word." echo " d) Lower case letters in place of uppercase alphabets." echo "--------------------------------------------------------------" echo -e "\n Enter Your Choice : \c" read c case "$c" in a) echo -e "\n Total lines,words,char.s \c" ; wc $filen ;; b) rev $filen read buf;; c) echo -e "\nEnter word to find : \c" read w for i in `cat $filen` do echo $i >> f1.txt done echo -e "Total no. of words= \c" ;grep -c $w f1.txt grep $w f1.txt rm f1.txt ;; d) echo "Enter the text (Enter ctrl+D for end>:" cat > temp.txt echo "See temp.txt file for output..." tr [a-z] [A-Z] <>
15. Write a script which reads a text file and output the following Count of character, words and lines. File in reverse. Frequency of particular word in the file. Lower case letter in place of upper case letter. echo "Enter File Name" read fname echo " 1...Count character,words and lines 2...Reverse File 3...Found frequency of word 4...Convert upper to lower 5...Convert lower to upper ------------------------------------" echo "Enter Choice: " read ch case $ch in 1) echo Total Character: wc -c $fname echo Total Words: wc -w $fname echo Total Lines: wc -l $fname ;; 2) rev $fname ;; 3) echo Enter word to find read w echo Frequency: grep -c "$w" $fname ;; 4) echo Enter Text read text echo $text | tr "[A-Z]" "[a-z]" ;; 5) echo Enter Text read text echo $text | tr "[a-z]" "[A-Z]" ;; *) echo Wrong Choice ;; esac /* output [mca0914@ctilinux-1 mca0914]$ sh 15.sh Enter File Name abc.txt 1...Count character,words and lines 2...Reverse File 3...Found frequency of word 4...Convert upper to lower 5...Convert lower to upper 6...-------------------------------- Enter Choice: 1 Total Character: 15 abc.txt Total Words: 4 abc.txt Total Lines: 2 abc.txt ****************************************************** Enter Choice: 2 olleH ?u r woH ****************************************************** Enter Choice: 3 Frequency: 2 ****************************************************** Enter Choice: 4 Enter Text HET het ****************************************************** Enter Choice: 5 Enter Text het HET ******************************************************
16. Write a shell script to check whether the named user is...
#!/usr/bin/env bash # Code written By: Rushyang Darji # My Online Repository: http://github.com/rushyang/GTU-OS-Bash-Scriptss : << -- This shell script will verify whether the user is logged in or not. -- echo -e "Enter username to verify whether he is logged in or not: \c" read myname who | awk '{print $1}' > allusers_dummy.log sed -n /^$myname$/p allusers_dummy.log > finalusers_dummy.log SIZE=`ls -s finalusers_dummy.log | awk '{ print $1 }'` if [ $SIZE -eq 0 ]; then echo "User is not logged in" else echo "User: $myname is currently logged in" sleep 2 who | sed -n /^$myname/p fi rm allusers_dummy.log rm finalusers_dummy.log # OR #!/usr/bin/env bash # Code written By: Rushyang Darji # My Online Repository: http://github.com/rushyang/GTU-OS-Bash-Scripts echo -e "Enter name of user: \c" read myname finaluser=`who | awk '{print $1}' | sed -n /^$myname$/p | head -n1` if [ "$myname" = "$finaluser" ]; then echo "User is currently logged in." sleep 1 who | sed -n /^$myname/p else echo "User is not currently logged in. " fi
16.Write a shell script to check whether the named user is currently logged in or not **************************************************************************************** clear echo -e "Enter The User name : " read name (who | grep $name ) && clear echo "The User " $name && echo " Is Currently Logged In " || echo "is logged out" ~ ~ ~ ~ ~ ~ ~ ~ Output Enter The User name : Raj The User Raj Is Currently Logged In [Divya@linux ~]$ *************************************************************
kuldeep-SSIT ************************************************************************************************ 10. To Find That Given User is Currently Logged In Or Not. ************************************************************************************************ clear echo "Enter The User name :\c " read name >>temp echo "The User \c " $name (who | grep $name >> temp ) && echo " Is Currently Logged In " || echo "is not logged in" ************************************************************************************************ OR **** #message Broadcast for a Single User,Multiple Users and to All# who > e.txt echo "For Broadcasting Messages." if [ $# = 0 ] then echo "Enter the User Name:\c" read nam p=`grep $nam e.txt` if [ "$p" != "" ] then write $nam rm e.txt exit else echo "The user is not logged in" fi rm e.txt exit fi if [ "$1" = "all" ] then wall fi for nam in $* do p=`grep $nam e.txt` if [ "$p" != "" ] then write $nam else echo "The User $nam is not logged in" fi done ************************************************************************************************ #OR **** echo "enter user name" read n who | grep "^$n" if [ $? -eq 0 ] then echo "currently log in" else echo "not log in" fi ************************************************************************************************
17. Write a Script for Simple Database Management System...
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
18. Write A Script To Perform Following String Operations...
/*18. Write A Script To Perform Following String Operations Using Menu: COMPARE TWO STRINGS. JOIN TWO STRINGS. FIND THE LENGTH OF A GIVEN STRING. OCCURRENCE OF CHARACTER AND WORDS E. REVERSE THE STRING. */ while true do echo --------------------------------------- echo 1...COMPARE TWO STRINGS echo 2...JOIN TWO STRINGS echo 3...FIND THE LENGTH OF A GIVEN STRING echo 4...OCCURRENCE OF CHARACTER AND WORDS echo 5...REVERSE THE STRING echo --------------------------------------- echo Enter Choice: read ch echo --------------------------------------- case $ch in 1) echo Enter String1: read str1 echo Enter String2: read str2 if [ $str1 = $str2 ] then echo String is equal else echo String is not equal fi ;; 2) echo Enter String1: read str1 echo Enter String2: read str2 str3=$str1$str2 echo Join String: $str3 ;; 3) length=0 echo Enter String1: read str1 length=`expr $str1 | wc -c` length=`expr $length - 1` echo Length: $length ;; 4) echo Enter String: read str echo Enter Word to find read word echo $str | cat > str1.txt grep -o $word str1.txt | cat > str2.txt count=`grep -c $word str2.txt` echo Count: $count ;; 5) echo Enter String1: read str len=`expr $str | wc -c` len=`expr $len - 1` while [ $len -gt 0 ] do rev=`expr $str | cut -c $len` ans=$ans$rev len=`expr $len - 1` done echo Reverse String: $ans ;; *) echo Wrong Choice ;; esac echo Do u want to continue? read ans if [ $ans = n ] then exit fi done
19. Write a script to calculate gross salary for any number...
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
20. Write a script to check whether a given string...
/* 20. Write a script to check whether a given string is palindrome or not. */ echo Enter String read str length=0 cnt=1 flag=0 length=`expr $str | wc -c` length=`expr $length - 1` temp=`expr $length / 2` while test $cnt -le $temp do rev=`expr $str | cut -c $cnt` ans=`expr $str | cut -c $length` if [ $rev != $ans ] then flag=1 fi cnt=`expr $cnt + 1` length=`expr $length - 1` done if [ $flag -eq 0 ] then echo String is palindrome else echo String is not palindrome fi
Home >> Sem 3 >> 630007 - Programming Skills-V (OS) [1/3]
1. Check the output of the following commands.....
****************************************************************** 1.Check the output of the following commands. mkdir and rmdir ****************************************************************** [Divya@linux ~]$ mkdir family [Divya@linux ~]$ cd family [Divya@linux family]$ mkdir grpr [Divya@linux family]$ rmdir grpr [Divya@linux family]$ mkdir parent [Divya@linux parent]$ mkdir child [Divya@linux parent]$ cd child [Divya@linux child]$ cat c1 it is first fle ^z [Divya@linux child]$ls c1 [Divya@linux child]$cd ../ [Divya@linux family]$ cd ../.. [Divya@linux home]$
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
2. Write shell script...
2. ****************************************************************** 2.2 Accept the string and checks whether the string is palindrome or not. ****************************************************************** echo "enter the string" read a b=`echo $a|rev` #c=`echo $a -eq $b` if [ "$a" == "$b" ] ; then echo "pali " else echo "not pla" fi [ketan@linux ~]$ sh k2.sh enter the string mom pali [ketan@linux ~]$ sh k2.sh enter the string divya not pla ****************************************************************** 2.3 Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number. ****************************************************************** echo "Enter the number" read n if [ $[$n % 2] -eq 0 ] then echo " $n is even" else echo " $n is odd " fi ****************************************************************** or ****************************************************************** 2.3 Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number ****************************************************************** echo "Enter the Number" read a z=$a cont=0 sum=0 sd=0 while [ $a -gt 0 ] do sd=`expr $a % 10` cont=`expr $cont + 1` sum=`expr $sum + $sd` a=`expr $a / 10` done echo "Sum of digit for numner is $sum" echo " Total length $cont" b=`expr $z % 2` if test $b -eq 0 then echo "Even number" else echo "Odd Number" fi ****************************************************************** 2.4 Accept strings and replace a string by another string. ****************************************************************** echo enter string read stringname echo enter replacable string read replacename echo enter file name read filename sed s/$stringname/$replacename/g $filename > temp ~ [divya@linux ~]$ sh 4.sh enter string payal enter replacable string pallavi enter file name s [divya@linux ~]$ cat temp roll no name marks 1 vaishali 50 2 viraj 40 3 sonal 50 4 pallavi 30------------- 5 nehal 40 6 pallavi 60-------------- [divya@linux ~]$ cat s roll no name marks 1 vaishali 50 2 viraj 40 3 sonal 50 4 payal 30 5 nehal 40 6 payal 60 ************************************************************************************************************************************ 2.5 Accept filename and displays last modification time if file exists, otherwise display appropriate message. ************************************************************************************************************************************ echo "Enter the file name" read a z=`pwd $a` if [ -d $z/$a ] then b=`ls -lt $a` echo $b elif [ -f $z/$a ] then q=`ls -lt $a` echo $q else echo " no such file or directory exist" fi "5.sh" 19L, 204C written [divya@linux ~]$ ls 1 3.sh 6.sh d2 exist.sh first s temp2 2.sh 5.sh d1 d3 f login.sh temp1 [divya@linux ~]$ sh 5.sh Enter the file name first -rw-rw-r-- 1 divya divya 182 2010-07-31 14:58 first [divya@linux ~]$ sh 5.sh Enter the file name d1 total 8 -rw-rw-r-- 1 divya divya 4 2010-07-30 13:38 first [divya@linux ~]$ sh 5.sh Enter the file name d4 no such file or directory exist ****************************************************************** 2.6 Fetch the data from a file and display data into another file in reverse order. ****************************************************************** echo "Enter the file name" read a b=`cat $a` echo $b `sort -r $a > first ` "2.sh" 8L, 80C written [divya@linux ~]$ cat first [divya@linux ~]$ cat s roll no name marka 1 vaishali 50 2 viraj 40 3 sonal 50 4 payal 30 5 nehal 40 [divya@linux ~]$ sh 2.sh Enter the file name s roll no name marka 1 vaishali 50 2 viraj 40 3 sonal 50 4 payal 30 5 nehal 40 [divya@linux ~]$ cat first roll no name marka 5 nehal 40 4 payal 30 3 sonal 50 2 viraj 40 1 vaishali 50 ******************************************************************
#2.a) Accept numbers and perform addition, subtraction, division and multiplication. a=10 echo "Enter The Value of A: " read a b=10 echo "Enter The Value of B: " read b c=0 c=`expr $a + $b` echo "Addition " $c c=`expr $a - $b` echo "Subtraction " $c c=`expr $a / $b` echo "Division " $c c=`expr $a \* $b` echo "Multiplication " $c #b) Accept the string and checks whether the string is palindrome or not. l=0 cnt=1 tag=0 echo "Enter a String?" read str l=`echo $str |wc -c` l=`expr $l - 1` lh=`expr $l / 2` while [ $cnt -le $lh ] do c1=`echo $str|cut -c$cnt` c2=`echo $str|cut -c$l` if [ $c1 != $c2 ] then tag=1 fi cnt=`expr $cnt + 1` l=`expr $l - 1` done if [ $tag -eq 0 ] then echo "String is Palindrome" else echo "String is not Palindrome" fi #c.Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number. echo "Enter number:" read no c=`expr $no % 2` if [ $c -eq 0 ] then echo "No is even " else echo "No is odd" fi cnt=`echo $no | wc -c` cnt=`expr $cnt - 1` echo "word count =" $cnt i=1 while [ $i -le $cnt ] do j=`echo $no | cut -c $i` k=`expr $k + $j` i=`expr $i + 1` done echo "total =" $k #d.Accept strings and replace a string by another string. echo "Enter any string:" read str echo "Your string is :" $str echo "Enter word that your what replace with other word" read oldstr echo "Enter new string" read newstr str1=`echo $str | sed s/$oldstr/$newstr/g` str2=`echo $str | replace $oldstr $newstr` echo "after replacing " $str1 echo "after replacing " $str2 #e.Accept filename and displays last modification time if file exists, otherwise display appropriate message. echo "Enter file name" read fnm if [ ! -f $fnm ] then echo "File Not Found" exit 1 fi dt=`date -r $fnm` echo "modification date is:" $dt #f.Fetch the data from a file and display data into another file in reverse order. echo "enter file name for fatching data and store in other file" read fnm if [ ! -f $fnm ] then echo "File not found" exit 1 fi echo "enter new file name:" read newfile while read -n1 char; do str=$char$str done<$fnm echo $str>$newfile echo $newfile "successfully created on" `pwd` " path"
3. Write a script to find the global complete path for any file....
#!/usr/bin/env bash # GTU3: Write a script to find the global complete path for any file. # Code written By: Rushyang Darji # Last Updated: 19.10.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. echo -e "Enter the filename to search in current directory: \c" read FILE args=`find . -name $FILE | xargs` # xargs builds arguments from find, for using in "for loop"... Remember, you should never Parse result of "ls" in any case because unix allows every character to be used in naming files, even if a "new line character"... execute "touch $'A\nFile'" to make file and ls to know it. Google "parsing output of ls" to know more. for i in $args do if [ -f "$i" ]; then CPATH=`readlink -f "$i"` # readlink returns the symbolic link, -f canonicalize by every parent directory recursively. echo $CPATH fi done noargs=${#args} # noargs stores total number of arguments. if [ "$noargs" -eq "0" ]; then echo "No such a file exists" fi
3.Write a script to find the global complete path for any file. ****************************************************************** echo "enter the filename" read a b=`pwd $a` if [ -f $b/$a ] then echo "file exist" echo $b else echo "file does not exist" fi ~ ~ "3.sh" 11L, 131C written [divya@linux ~]$ sh 3.sh Enter the filename f file exist /home/divya [divya@linux ~]$ sh 3.sh Enter the filename d1 file does not exist or echo "enter file name to find:" read fnm ans=0; if [ ! -f $fnm ] then if [ ! -d $fnm ] then echo "File not found" exit 1 fi fi ans=`find /home -name $fnm ` echo $ans ******************************************************************
#3.3. Write a script to find the global complete path for any file. echo "enter file name to find:" read fnm ans=0; if [ ! -f $fnm ] then if [ ! -d $fnm ] then echo "File not found" exit 1 fi fi ans=`find /home -name $fnm ` echo $ans
4. Write a script to broadcast a message to a specified user or a group...
4. Program :- message Broadcast for a Single User,Multiple Users and to All ****************************************************************** who > e.txt echo "For Broadcasting Messages." if [ $# = 0 ] then echo "Enter the User Name:\c" read nam p=`grep $nam e.txt` if [ "$p" != "" ] then write $nam rm e.txt exit else echo "The user is not logged in" fi rm e.txt exit fi if [ "$1" = "all" ] then wall fi for nam in $* do p=`grep $nam e.txt` if [ "$p" != "" ] then write $nam else echo "The User $nam is not logged in" fi done output [Divya@linux ~]$ sh 4.sh For Broadcasting Messages. Enter the User Name:\c bhoomika hellow to u [8]+ Stopped sh 4.sh [Divya@linux ~]$ sh 4.sh For Broadcasting Messages. Enter the User Name:\c ketan The user is not logged in [Divya@linux ~]$ ******************************************************************
echo "Enter the User name :" read uname echo "Enter String u want to send (End with Control-D) :" write $uname echo -e "\nYour message send successfully...."
5. Write a script to copy the file system from two directories to a...
#!/usr/bin/env bash # GTU 5 - Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories. # Code written By: Rushyang Darji # Last Build: 24.08.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. EXIT=n while [ $EXIT != y ] do sleep 1 echo -e "\n" echo -e " 1. Display PWD 2. Long Listing 3. Change Directory 4. Copy Newest File. 5. Exit Enter Choice: \c" read ch case $ch in 1) clear pwd ;; 2) clear pwd ls -l ;; 3) echo -n "Enter Absolute Path to change directory: " read apath cd $apath if [ $? -eq 0 ]; then # We can also check for availibility of directory before 'cd' command by 'test -d $apath' i.e. 'if [ -d $apath ]' clear echo "Working Directory Changed successfully to.." sleep 1 pwd else clear echo "Please check your PATH." fi ;; 4) clear echo "Enter filenames to copy. ( * - for ALL Files, ELSE Separate files by spaces )" read allfiles if [ -f $allfiles ]; then echo "Enter Absolute path, where to copy these files: " read -e cpath if [ -d $cpath ]; then cp -u "$allfiles" $cpath # -u copies only when the SOURCE file is newer than the destination file or when the destination file is missing else echo "There is no such a directory!" fi else echo "There is/are no such file(s)!" fi ;; 5) clear echo -n "Exiting.." sleep 1 echo -n "." sleep 1 echo -n "." clear exit ;; *) clear echo "Invalid Choice" ;; esac done
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
6. Write a script to compare identically named files in two different...
#!/usr/bin/env bash # Code written By: Rushyang Darji # Last Build: 09.10.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. while true; do read -e -p "Enter first Directory's Absolute path: " path1 || exit [[ -d $path1 ]] && break echo "Invalid path, Try Again!" done while true; do read -e -p "Enter second Directory's Absolute path: " path2 || exit [[ -d $path2 ]] && break echo "Invalid path, Try Again!" done while true; do read -e -p "Enter Third Directory's Path, to copy files in case of exact match: " path3 || exit [[ -d $path3 ]] && break echo "Invalid Path, Try, Again!" done temp=$(mktemp) for i in $path1/* do if [ -f "$i" ]; then for j in $path2/* do if [ -f "$j" ]; then base1=`basename "$i"` base2=`basename "$j"` if [ "$base1" = "$base2" ]; then diff "$i" "$j" > $temp size=`ls -s $temp | awk '{print $1}'` if [ "$size" -eq "0" ]; then echo "File: \"$base1\" was found same in both directories." cp "$i" "$path3" echo "Copied to \"$path3\" successfully!" fi fi fi done fi done rm $temp : << -- OUTPUT (Attempt 1) rushyang@Maverick_Meerkat: GTU-MCA $ bash GTU6-2.sh Enter first Directory's Absolute path: /home/rushyang/sldjf Invalid path, Try Again! Enter first Directory's Absolute path: /home/rushyang/Experiments/1510/mydir1/ Enter second Directory's Absolute path: /home/rushyang/lsajlj Invalid path, Try Again! Enter second Directory's Absolute path: /home/rushyang/Experiments/1510/mydir2/ Enter Third Directory's Path, to copy files in case of exact match: /home/rushyang/Experiments/1510/mydir3/ File: "1" was found same in both directories. Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully! File: "new file" was found same in both directories. Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully! OUTPUT (Attempt 2) rushyang@Maverick_Meerkat: GTU-MCA $ bash GTU6-2.sh Enter first Directory's Absolute path: /home/rushyang/Experiments/1510/mydir1/ Enter second Directory's Absolute path: /home/rushyang/Experiments/1510/mydir2/ Enter Third Directory's Path, to copy files in case of exact match: /home/rushyang/Experiments/1510/mydir3/ File: "1" was found same in both directories. Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully! File: "new file" was found same in both directories. Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully! -- ######################## OR ######################## #!/usr/bin/env bash # Code written By: Rushyang Darji # Last Build: 02.10.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. : << -- GTU 6: Write a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory Code Developed By: Rushyang Y Darji (rushyang@yahoo.co.in) Init Build: 04.08.2010 Last Build: 15.10.2010 v1.6 -- clear temp=$(mktemp tmp.XXXXXXXX) #mktemp makes temporary file into /temp directory with r+w permissions only for creator. echo -n "Enter 1st Directory: " read dir1 PATH1="$(pwd)/$dir1" #Converted whole path of dir1 into PATH1 variable echo "PATH1=$PATH1" main=$(pwd) if test -d $PATH1; then #Test condition to make sure dir1 is a directory. echo -n "Enter 2nd Directory: " read dir2 PATH2="$(pwd)/$dir2" #Same as Line 14 echo "PATH2=$PATH2" if test -d $PATH2; then cd $PATH1 #Secured for using for i loop. for i in * do cd $PATH2 #Secured for using for j loop. for j in * do if test "$i" == "$j"; then cd $main #Back to $(basedir) cmp "$PATH1/$i" "$PATH2/$j" > $temp # cmp checks byte by byte.. can be little slower than 'diff' size=`ls -s $temp | awk '{print $1}'` # '-s' for listing size, and 'awk' for fetching size of $temp if test "$size" == "0"; then # if size of temporary file is 'ZERO', then both files are exactly same. echo "File: \"$i\" was found same in both directories." echo -n "Enter Directory name (must be in current working directory) to copy it: " read dir3 PATH3="$(pwd)/$dir3" #Same as Line 14 if test -d $PATH3; then cp -i "$PATH1/$i" "$PATH3" # "$PATH2/$j" instead of "$PATH1/$i" will also do. else echo "There is no directory named $dir3 in $pwd" #Line 41 fi fi fi done done else echo "Invalid Directory Name for dir2." #Error Message of missing dir2 fi else echo "Invalid Directory Name for dir1." #Error Message of missing dir3 fi rm $temp : << -- rushyang@Maverick_Meerkat: 1510 $ ls mydir1/ 1 2 new file rushyang@Maverick_Meerkat: 1510 $ ls mydir2/ 1 new file T rushyang@Maverick_Meerkat: 1510 $ ls mydir3/ OUTPUT (Attempt 1) Enter 1st Directory: asdf PATH1=/home/rushyang/Experiments/1510/asdf Invalid Directory Name for dir1. OUTPUT (Attempt 2) Enter 1st Directory: mydir1 PATH1=/home/rushyang/Experiments/1510/mydir1 Enter 2nd Directory: mydir2 PATH2=/home/rushyang/Experiments/1510/mydir2 File: "1" was found same in both directories. Enter Directory name (must be in current working directory) to copy it: mydir3 File: "new file" was found same in both directories. Enter Directory name (must be in current working directory) to copy it: mydir3 OUTPUT (Attempt 3) Enter 1st Directory: mydir1 PATH1=/home/rushyang/Experiments/1510/mydir1 Enter 2nd Directory: mydir2 PATH2=/home/rushyang/Experiments/1510/mydir2 File: "1" was found same in both directories. Enter Directory name (must be in current working directory) to copy it: mydir3 cp: overwrite `/home/rushyang/Experiments/1510/mydir3/1'? y File: "new file" was found same in both directories. Enter Directory name (must be in current working directory) to copy it: mydir3 --
6.Write a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory. ****************************************************************** echo Enter the name of 1st directory read dir1 echo Enter the name of 2nd directory read dir2 echo Enter the name of the directory in which you want to copy the duplicate file read dir3 cd `find /home/divya -type d -name $dir1` for fl1 in * do echo $fl1 cd `find /home/divya -type d -name $dir2` for fl2 in * do if [ $fl1 == $fl2 ] then cd .. cp ./$dir2/$fl2 ./$dir3 cd `find /home/divya -type d -name $dir2` rm $fl2 cd `find /home/divya -type d -name $dir1` rm $fl1 fi done cd .. mv ./temp1/* ./$dir2/$fl2 done ~ ~ ~ ~ ~ "6.sh" 29L, 876C written [divya@linux ~]$ sh 6.sh Enter the name of 1st directory d1 Enter the name of 2nd directory d2 Enter the name of the directory in which you want to copy the duplicate file d3 first [divya@linux ~]$ ******************************************************************
echo "Enter 1st file full path(/home/mca/diz/) :" read p1 echo "Enter 2nd file full path(/home/mca/diz/) :" read p2 echo "Enter new path for copy" read dest a=`cmp $p1 $p2` if [ $? -eq 0 ] then `cp $p2 $dest` echo "File successfully copied on path: "$dest else echo "File not identically same" fi
7. Write a script to delete zero sized files from a given directory ...
#!/usr/bin/env bash # Code Developed By: Rushyang Darji # Last Build: 14.09.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. while true; do read -e -p "Enter the Absolute Path: " path || exit [[ -d $path ]] && break echo "Invalid Directory!" done args=`find "$path" -empty -print0 | xargs -0` for i in $args do if [ -f "$i" ]; then rm -i "$i" fi done
7.Write a script to delete zero sized files from a given directory (and all its sub-directories). ****************************************************************** echo Enter the file you want to delete read zerofile if [ ! -f $zerofile ] then echo "File is does not exists" elif [ ! -s $zerofile ] then rm -i $zerofile else echo " file has some data" fi "7.sh" 19L, 303C written [divya@linux ~]$ sh 7.sh Enter the file you want to delete el.lst File is does not exists [divya@linux ~]$ ls 6.sh 71.sh 7.sh d1 d2 d3 e.lst emp.lst temp1 [divya@linux ~]$ sh 7.sh Enter the file you want to delete e.lst rm: remove regular empty file `e.lst'? y [divya@linux ~]$ ls 6.sh 71.sh 7.sh d1 d2 d3 emp.lst temp1 or echo Enter the file extention you want to delete : read ext if [ -n *.$ext ] then rm -i *.$ext else echo No such extension file fi for i in file * do if [ ! -s $zerofile ] then rm -i $zerofile fi done ******************************************************************
for i in `find /home/mca/diz/diz -type f -size 0c` do echo "$i has Zero size" echo `rm $i` done
8. Write a script to display the name of those files (in the given directory...
#!/usr/bin/env bash # Write a script to display the name of those files (in the given directory) which are having multiple links. # Developed By: Rushyang Darji # Init Build: 15.10.2010 # Last Build: 15.10.2010 # v1.0 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. echo "Enter Absolute path of directory" read path if test -d $path; then cd $path for i in * do for j in * do if test "$i" != "$j"; then if test "$i" -ef "$j"; then echo "$i" >> $$.temp fi fi done done cat $$.temp | uniq rm $$.temp cd - else echo "Check your path." fi ############### OR ############### #!/usr/bin/env bash # Write a script to display the name of those files (in the given directory) which are having multiple links. # Developed By: Rushyang Darji # Init Build: 15.10.2010 # Last Build: 15.10.2010 # v1.0 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. temp=$(mktemp tmp.XXXXXXXXXX) while true; do read -e -p "Ente the Absolute Path: " path || exit [[ -d $path ]] && break echo "Invalid Directory!" done for i in $path/* do for j in $path/* do base1=`basename $i` base2=`basename $j` echo "$i" echo "$j" if test "$base1" != "$base2"; then if test "$base1" -ef "$base2"; then echo "$base1" >> $temp fi fi done done cat $temp | uniq rm $temp
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
9. Write a script to display the name of all executable...
#!/usr/bin/env bash # GTU 9 # Code Developed by: Rushyang Darji # Last Updated: 14.10.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. # Remember, You should NEVER parse the output of "ls" command and use into scripts. Because UNIX filesystem allows all kind of characters to assign a filename, including "new line". For more info.. Google "parsing output of ls". # read -e enables Tab Completion which is way too easier than entering whole absolute path. read -e -p "Enter Absolute Path: " path || exit temp=$(mktemp) if test -d $path; then for i in $path/* do if test -x "$i"; then echo "$i" >> "$temp" fi done else echo "Invalid Directory" fi cat $temp rm $temp
9. Write a script to display the name of all executable files in the given directory. ****************************************************************** i=0 ls > test1 set `wc -l test1` l=$1 while [ $l -ne 0 ] do line=`head -$l test1 | tail -1` if [ -x $line ] then i=`expr $i + 1` fi l=`expr $l - 1` done echo -e "\t\t\tTotal no. of executable files : " echo $i ~ ~ ~ ~ "9.sh" 15L, 267C written [divya@linux ~]$ ls -l total 136 -rw-rw-r-- 1 divya divya 395 2010-07-31 16:01 13.sh -rw-rw-r-- 1 divya divya 108 2010-07-31 14:58 2.sh -rw-rw-r-- 1 divya divya 131 2010-07-31 15:31 3.sh -rw-rw-r-- 1 divya divya 204 2010-07-31 15:28 5.sh -rw-rw-r-- 1 divya divya 848 2010-07-29 16:40 6.sh -rw-rw-r-- 1 divya divya 267 2010-07-31 16:07 9.sh drwxrwxr-x 2 divya divya 4096 2010-07-30 13:38 d1 ------------- drwxrwxr-x 2 divya divya 4096 2010-07-29 16:41 d3 ------------ -rw-rw-r-- 1 divya divya 122 2010-07-31 15:17 exist.sh -rw-rw-r-- 1 divya divya 182 2010-07-31 14:59 f -rw-rw-r-- 1 divya divya 182 2010-07-31 14:58 first -rw-rw-r-- 1 divya divya 257 2010-07-29 16:37 login.sh -rw-rw-r-- 1 divya divya 182 2010-07-31 14:55 s -rw-rw-r-- 1 divya divya 260 2010-07-31 15:53 s1 drwxrwxr-x 2 divya divya 4096 2010-07-29 16:41 temp1 ------------------ -rw-rw-r-- 1 divya divya 25 2010-07-31 15:13 temp2 -rw-rw-r-- 1 divya divya 86 2010-07-31 16:06 test1 [divya@linux ~]$ sh 9.sh Total no. of executable files : 3 ******************************************************************
Share this program... Send it to gtumca@gmail.com with your Name - College name.. and share what you want ... at same mail id... Thanx in advance... Be connected... D_i_Z
10.Write a script to display the date, time and a welcome ...
#!/usr/bin/env bash # GTU10: Write a script to display the date, time and a welcome message (like Good Morning should be displayed with “a.m.” or “p.m.” and not in 24 hours notation. # Code written By: Rushyang Darji # Last Updated: 19.08.2010 # Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts. msg2=`date +%H` echo "Welcome $USERNAME!" time=`date +"%F %I:%M:%S %p"` echo "Current Time is: $time" if [[ "$msg2" -ge "5" ]] && [[ "$msg2" -lt "12" ]]; then echo "Good Morning..!" elif [[ "$msg2" -ge "12" ]] && [[ "$msg2" -lt "17" ]]; then echo "Good Afternoon..!" elif [[ "$msg2" -ge "21" ]] && [[ "$msg2" -lt "19" ]]; then echo "Good Evening..!" else echo "Good Night..!" fi
10 Write a script to display the date, time and a welcome message (like Good Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24 hours notation ****************************************************************** tput clear hr=`date +%I` set `who am i` a=`echo $1` set `date +%r` b=`echo $1 $2` if [ $hr -lt 12 ] then echo "welcome `$a $b` Good Morning" elif [ $hr -ge 12 ] then echo "Welcome $a $b GOOD Afternoon" elif [ $hr -ge 16 -o $hr -le 20 ] then echo "Good Evening" else echo "God Night" fi or tput clear hr=`date +%H` if [ $hr -lt 12 ] then echo "welcome `who` Good Morning" elif [ $hr -gt 12 ] then echo "Welcome `who` GOOD Afternoon" elif [ $hr -eq 17 || $hr -lt 24 ] then echo "Good Evening" else echo "God Night" fi ~ ~ Output [Divya@linux ~]$ sh time.sh Welcome GOOD Afternoon or time=`date +%H%M%S` d1=`date +%d/%m/%y` t1=`date +%I:%M:%S` echo "date:"$d1 if [ $time -ge 000000 -a $time -lt 120000 ] then echo "time:"$t1" a.m" else echo "time:"$t1" p.m" fi if [ $time -lt 120000 -a $time -gt 60000 ] then echo "GooD MorninG" else if [ $time -eq 120000 ] then echo "GooD NooN" else if [ $time -gt 120000 -a $time -lt 180000 ] then echo "GooD AfterNooN" else if [ $time -gt 180000 -a $time -lt 210000 ] then echo "GooD EveninG" else echo "GooD NighT" fi fi fi fi "101.sh" 33L, 661C written [divya@linux ~]$ sh 101.sh date:04/10/10 time:12:35:28 p.m GooD AfterNooN [divya@linux ~]$ or clear date >dt s=`cut -c 12-13 dt` echo $s if [ $s -lt 12 ] then echo "GOOD MORNING" elif [ $s -gt 12 -o $s -lt 16 ] then echo "GOOD AFTERNOON" elif [ $s -gt 16 -o $s -lt 20 ] then echo "GOOD EVENING" else echo "good night" fi ~ ~ ~ 16 GOOD AFTERNOON [divya@linux ~]$ ****************************************************************
time=`date +%H%M%S` d1=`date +%d/%m/%y` t1=`date +%I:%M:%S` echo "date:"$d1 if [ $time -ge 000000 -a $time -lt 120000 ] then echo "time:"$t1" a.m" else echo "time:"$t1" p.m" fi if [ $time -lt 120000 -a $time -gt 60000 ] then echo "GooD MorninG" else if [ $time -eq 120000 ] then echo "GooD NooN" else if [ $time -gt 120000 -a $time -lt 180000 ] then echo "GooD AfterNooN" else if [ $time -gt 180000 -a $time -lt 210000 ] then echo "GooD EveninG" else echo "GooD NighT" fi fi fi fi