




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A collection of shell scripts that solve various programming tasks and challenges. The scripts cover a wide range of topics, including file management, user input handling, string manipulation, arithmetic operations, and more. Detailed explanations of the scripts, their usage, and the testing procedures. It serves as a valuable resource for students and professionals who are learning or practicing shell scripting. The scripts are well-organized and easy to understand, making them suitable for use as study notes, lecture materials, or reference material for assignments and projects.
Typology: Cheat Sheet
1 / 28
This page cannot be seen from the preview
Don't miss anything!
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB UNIX & SHELL PROGRAMMING LAB BATCH:2019 (R19) Lab cycles including bridge experiments FINAL EXPERIMENTS SCHEDULE CYCLE- DETAILS DATE A B C REMARKS Submission CYCLE- B-I WEEK-1 13-09- 21,15-09- 21 INTRODUCTION,NAVIGATION, AND BASIC UTILITIES PRACTICE SOFTCOPY/ONLINE CYCLE- B-II WEEK-2 22-09- 21,29-09- 21 MORE UTILITIES SOFTCOPY/ONLINE CYCLE- B-III a & b WEEK-3 06-10-21, 13-10- SESSIONS AND CHAINED COMMANDS SOFTCOPY/ONLINE CYCLE-I WEEK-4 20-10-21, 27-10- BASIC SHELL SCRIPTS-1 RECORD SHEETS/OFFLINE CYCLE-II WEEK-5 03-11-21, 10-11- BASIC SHELL SCRIPTS-2 RECORD SHEETS/OFFLINE CYCLE- III a & b WEEK-6 17-11-21, 24-11- grep, sed exercises SOFTCOPY/ONLINE CYCLE- IV WEEK- 04-12-21, 11-12- awk exercises SOFTCOPY/ONLINE CYCLE-V WEEK-8 18-12-21 ADV SHELL SCRIPTS-1 ( Using GREP, SED, AWK) RECORD SHEETS/OFFLINE CYCLE- VI WEEK-9 25-12-21 ADV SHELL SCRIPTS-2 RECORD SHEETS/OFFLINE INT. EXAM WEEK- 10 INTERNAL ASSESSMENT Cycle-V &VI List of Shell Scripts:
1. Create a script that, given a user name, finds the home directory of the user using the /etc/passwd file. Preparation: None Script: Script Name: findHomeDirectory.scr Arguments: One, The user name. Validation: The minimum validation requirements are : i. Ensure that there is only one argument. Body Section: Create a script that, given the name of a user (as the only argument), prints the absolute pathname of the user's home directory
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB Testing the Script: Test the script with two or more arguments. Test the script with no arguments. Test the script with one argument. Testing the Effect of the Script: Verify the script by using your user name. SOLUTION SCRIPT $pico findHomeDirectory.scr #!/bin/bash
if [ $# -ne 1 ] then echo “INVALID USAGE” echo “USAGE:$0 username” exit fi cat /etc/passwd | grep $1 |cut –d’:’ –f TESTING
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB
3. In a C Program, there is only one comment format. All comments must start with an open comment token, /*, and end with a close comment token, /. C++ programs use the C tokens for comments that span several lines. Single-line comments start with two slashes (//). In either case, the start token can be anywhere on the line. Write a script to change every single-line comment in a C++ source file that uses C program start and end comment tokens to a single-line comment starting with a C++ single-line token. The comment itself is to be unchanged. Preparation: Create at least five C++ source files in your home directory. The files do not have to be real C++ source files; they can contain only a few lines of comments, some with C program tokens and some with C++ single-line tokens. Each program should have at least one multiple comment and at least one single-line comment that uses the C program tokens. Use one or more blank lines between comments. The name of the files should have C++ extension (.c++), such as file1.c++. Script: Script Name: commentType.scr Arguments: None Validation: The minimum validation requirements are : i. Ensure that there is no argument. Body Section: Create a script that finds all files with extension (.c++) under your directory and change only the lines with comments. The name of the files should be preserved. If a file has the name file1.c++, the name still should be file1.c++ after the change. Testing the Script: Test the script with one or two arguments. Test the script with no arguments. Testing the Effect of the Script: Check to see if the comments are changed in the files. SOLUTION SCRIPT $pico commentType.scr #!/bin/bash # performs conversion of comments if [ $# -ge 1 ] then echo “INVALID USAGE “ echo “USAGE: $0 filename exit fi for f in ` ls |grep “.c++$” do sed –e ‘s/^(/*)(.)(*/)$///\2/g’ ./$f >tmp
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB mv tmp ./$f done echo “Conversion completed” TESTING #1: $bash commentType.scr abc xyz INVALID USAGE USAGE: commentType.scr #2: $bash commentType.scr Conversion completed
4. Write a script to backup and archive a list of files. Preparation: Create a file and type in it the list of files (in your home directory) that you want to backup and archive Create a directory in which you will store the backed-up files and archive file. Script Script Name: backup.scr Arguments: A filename and a directory. The filename holds the list of the files that should be backed-up. The directory is where the backed-up files should be stored. Validation: The minimum validation requirements are : i. Ensure that exactly two arguments are entered. ii. Check that the first argument is the name of a file exists iii. Check that the second argument is the name of the directory that exists Body Section: Create backup files for files listed in the first argument. The backup files should have the same name as the original file with the extension bak. They should be copied to the directory given as the second argument. Testing the Script: Test the script with no arguments Test the script with one argument Test the script with three arguments Test the script with two arguments in which the first one is not the name of the file Test the script with two arguments in which the second one is the name of a file rather than a directory. Test the script with name of the file and the name of the directory you created in the preparation section. Testing the Effect of the Script: Check the contents of the directory to be sure that the files are copied SOLUTION SCRIPT
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB Script: Script Name: softLinkFinder.scr Arguments: A filename. The file for which we want to find the soft links. Validation: The minimum validation requirements are: i. Ensure that exactly one argument is entered. ii. Check that only argument is the name of a file and that the specified file exists. Body Section: Use ls -l and grep command to find all the soft links attached to $1 positional parameter. Note that a file of type soft link is distinguished by lower case l. Be sure to find the soft links to the file defined in $1 and not other files. Testing the Script: Test the script with no arguments Test the script with one argument Test the script with one argument that is not a file Test the script with one valid argument. Testing the Effect of the Script: Check to make sure all the soft links you created are included in the list of soft links. SOLUTION SCRIPT $pico softLinkFinder.scr #!/bin/bash
if [ $# -ne 1 ] then echo “INVALID USAGE “ echo “USAGE: $0 filename “ exit fi if [! -e $1 –o! –f $1 ] then n=ls –l | grep –c “^l.* $1$”
ls –l | grep “^l.* $1$” ` echo –e “$n softlinks are there for $1\nSoftlink finding completed” else echo “ some thing wrong with the given input” fi TESTING
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB #1: $bash softLinkFinder.scr INVALID USAGE USAGE: softLinkFinder.scr filename #2: $bash softLinkFinder.scr anonfile Some thing wrong with the given input #3: $bash backup.scr afile 5 softlinks are there for afile Softlink finding completed CYCLE-VI
6. Create a script that simulates the ls -l command but prints only three columns of our choice. Preparation: None Script: Script Name: ls.scr Arguments: Three numeric arguments defining the column number of the ls -l output to be printed in the order we specify. Validation: The minimum validation requirements are: i. Ensure that exactly three arguments are entered. ii. Ensure that all three arguments are numeric iii. Ensure that each argument is less than or equal to the actual number of columns in the ls -l command output. Body Section: Creates a new command that shows the output of the ls -l command to be printed in three columns in the order we like. Testing the Script: Test the script with no arguments. Test the script with one argument. Test the script with two arguments. Test the script with three arguments, one of them nonnumeric. Test the script with three arguments, two of them nonnumeric. Test the script with three arguments, one of them too large. Test the script with three arguments,1 4 5 Test the script with three arguments,3 7 1 Testing the Effect of the Script: None SOLUTION SCRIPT $pico ls.scr #!/bin/bash
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB USAGE: ls.scr col1 col2 col #7: $bash ls.scr 1 4 5 #8: $bash ls.scr 3 5 1
7. Create a script that sends contents of a message file to everybody who logged in. Preparation: Create a file of a short friendly message and mention that this is a test message that should be discarded by the receiver Script: Script Name: message.scr Arguments: One argument, a message file. Validation: The minimum validation requirements are: i. Ensure that exactly one argument is entered. ii. Ensure that the argument is a readable filename. Body Section: Create a script that uses awk to create a temporary file containing the usernames of those users who are logged into the system at this moment. Then send the message contained in the first argument to every logged-in user. Note that a user who has logged in more than once should receive only one message. Testing the Script: Test the script with no arguments. Test the script with two arguments. Test the script with one argument that is not a readable file. Test the script with one valid argument. Testing the Effect of the Script: You should include yourself in the recipient list. Check to see if you have received the message. SOLUTION SCRIPT $cat > testmessage.txt Hi all Greetings. This is a test message. Please discord after reading! Have a Good bash…! $pico message.scr
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB #!/bin/bash
if [ $# -ne 1 ] then echo “INVALID USAGE “ echo “USAGE: $0 msg-file-name“ exit fi if [! -r $1 ] then echo “$1 message file NOT readable” exit fi who|awk ‘{print $1}’ >loglist wall $logllist $ echo messages sent to logged in users TESTING #1: $bash message.scr INVALID USAGE USAGE: message.scr msg-file-name #2: $bash message.scr afile bfile INVALID USAGE USAGE: message.scr msg-file-name #3: $bash message.scr afile afile is Not readable #4: $bash message.scr msg-file Messages sent to logged-in in users
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB echo “hi friends” fi echo “security check done” TESTING #1: $bash security.scr afile INVALID USAGE USAGE: security.scr #2: on terminal /dev/pts/ $bash security.scr hi friends security check done #3: on a different terminal $bash security.scr Security check done
9. Create a script that finds each line in a file that contains a specified string. Preparation: Create a file of at least 20 lines and insert a double quoted string, such as "hello," in several lines. Script: Script Name: search.scr Arguments: Two arguments, the first is the string to be found; the second is the name of the file. Validation: The minimum validation requirements are : i. Ensure that exactly two arguments are entered. ii. Ensure that the second argument is the name of the file that exists and is not empty. Body Section: Create a script that uses grep and loops to find the line numbers in which the string is found. Note that grep should be applied to each line, not the whole file. The script should print the result in the following format: Line Number : [Line contents] Testing the Script: Test the script with no arguments. Test the script with one argument.
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB Test the script with two arguments but the second one is not a file. Test the script with two correct arguments. Testing the Effect of the Script: Compare the results of your script with a printout of the file. SOLUTION SCRIPT $pico search.scr #!/bin/bash
if [ $# -ne 2 ] then echo “INVALID USAGE “ echo “USAGE: $0 string_to_search file“ exit fi if [! -f $2 ] then echo “$1 NOT a file” exit fi while read line do grep –n $1 $l done “searching completed” TESTING #1: $bash search.scr INVALID USAGE USAGE: search.scr string_to_search filename
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB Testing the Effect of the Script: Verify that executable files were created under your home directory. SOLUTION SCRIPT $pico compile.scr #!/bin/bash
if [ $# -ge 1 ] then echo “INVALID USAGE “ echo “USAGE: $0 “ exit fi for f in ls | grep “\.c$”
do cc –o $f.exe $f done echo “compiled all C files” TESTING #1: $bash compile.scr afile bfile INVALID USAGE USAGE: compile.scr #2: $bash search.scr Compiled all C files
11. Create a script that finds all files in subdirectories that have the same filename.
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB Preparation: Make several directories, at different levels, under your home directory. For example, make ~/A, ~/B, ~/C, ~/A/AA, ~/A/BB, ~/A/AA/AAA, and so on until you have at least 15 directories. Copy a small junk file named file under some of these directories; do not change its name. Copy another small junk file named file2 under some other directories. Copy a third junk file under several directories. Be sure that some directories get a combination of file1 and file2 or file1 and file3. In at least three of the directories, create a junk file with a unique name. Script: Script Name: duplicateName.scr Arguments: None Validation: The minimum validation requirements are : i. Ensure that there is no argument. Body Section: Create a script that uses find and awk commands to create a list of files that are duplicated; use the full pathname for the duplicated filenames. Hint: Use a basename command and an array in awk. The output should look like the following example: file1: ~/A/file1 ~/A/AA/file ~/A/B/BB/BBB/file file2: ~/B/file2 ~/C/file
... Testing the Script: Test the script with one argument. Test the script with no arguments. Testing the Effect of the Script: Use a recursive long list command to list the complete contents of your home directory. Verify the output of your script against the list command output. CYCLE-I &II I. Develop BASH SCRIPTS for the following operations. (1) To print all files in a given directory. Script $pico sh1.bash #!/bin/bash for f in ls $1
do cat ./$1/$f done Run $bash sh1.bash myDir
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB (4) Write a menu driven shell script to copy, display, rename and delete a file Script $pico sh4.bash #!/bin/bash select op in copy diaplay rename delete end do case $op in “copy”) echo enter src file name to copy read src echo enter tar file name to copy read tgt cp –i $src $tgt ;; “display”) echo file name to display read fname cat $fname ;; “rename”) echo enter file name to rename read fname echo enter new name read nname mv $fname $name ;; “delete”) echo enter file name to delete read fname rm –i $fname ;; “end”) echo ‘thanx for using” exit;; *) echo “invalid option...! try again…!” ;; esac done Run $bash sh4.bash 1)copy 2)display 3)rename 4)delete 5)end #?)
DEPARTMENT OF INFORMATION TECHNOLOGY III B.TECH I SEM UNIX AND SHELL PROGRAMMING LAB And continues…. (5) Write a shell script for concatenation of two strings. Script $pico sh5.bash echo “enter string1” read str echo “enter string1” read str strnew= $str1$str echo the concatenated string=$strnew Run $bash sh5.bash Enter string
Enter string
The concatenated string=____ (6) Write a shell which will display Fibonacci series up to a given number. Script $pico sh6.bash #!/bin/bash f0=0;f1= fn=expr $f0 + $f1
echo “$f0,$f1” while [ $fn –le $1 ] do echo –e “,$fn” f0=$f1; f1=$fn; done Run