Unix Scripting Lab
Part 1: Shell Review
Create a new directory for the course in your home folder called
lab. Inside, create the following files:
Create the following directories under
Display all files starting with an
- Copy all files and folders starting with a capital letter to a new directory called capitals
- Delete all files whose extension is a single letter
- Rename both occurences of
- Delete all files containing m
lsto list all files containing a lowercase letter in their name, AND the nonexistant file named
- Now show the same list, but redirect standard output to a file
- Now show the same list, but redirect standard error to a file
- Combine 2 and 3: Redirect standard output to one file, and standard error to another
- Create 3 files:
hostnameto write the current host name into
- Prevent file clobbering
- Repeat (6). Did you get an error ?
- Fix the error keeping the noclobber option set
Part 2: Environment
- Create a new directory named:
I have $5
- Create an alias that finds all files larger than 2k but smaller than 5k
- Create an alias that finds all directories in /tmp owned by the current user
- Create an alias that finds all files modified within the last 4 hours
- Create a shell function that finds partial matches of a file name, so you could type:
findpartial txtto get all files with txt in their name
- Create an alias for
cpthat turns it to
- Create an alias for
rmthat turns it to
- Create an alias that prints how many files exist under current directory
- Create an alias that prints how many executable files exist under current directory
- Create a shell function that takes a date and prints how many files were modified in that date
Part 3: Getting Parameters
- Write a shell script that takes a file name as input and prints the file backwards
- Write a shell script that takes two file names as inputs, and replaces their contents.
- Write a shell script that reads a file name from the user, prints its contents and the number of lines in the file.
- Write a shell script that takes a Windows file (lines end with
\r\n) and converts it to a Unix file (lines end with
Part 4: Conditionals
- Write a shell script that takes an input argument and tells if it's a string or a number (Hint: try
expr a + 0)
- Write a shell script that takes 3 input arguments and prints out the largest one
- Write a shell script that reads a name from the user - if that name is an executable program run it, otherwise print its content. If it's not a file print an error message.
- Write a shell script that takes two file names, and prints the contents of the larger one.
- Write a shell script that asks the user for a number, if the user chooses 7 - print "You Win".
- Write a
safedelscript. The script takes a file name as command line input, and moves that file to a
~/TRASHdirectory instead of deleting it.
Upon invocation, script should check
~/TRASHfor files older than 48 hours and delete them (hint: use
- Write a shell script that reads a file name from the user, checks that the file is valid, and lowecases its name. For example, running
lc MyFileshould rename the file
Part 5: Loops
- Write a shell script that takes input as command line arguments and
prints them out backwards (first argument printed last). Hint: use
- Write a shell script called "wait_for_user" that takes a user name and checks if the user is logged in. If she's not logged in, the script sleeps for 5 seconds and checks again in a loop - until the user logs in.
- Write a shell script that reads a file and prints its content double-spaced (adding a blank line after each line)
- Write a shell script that reads a file and prints its content with no blank lines.
- Write a shell script that reads a file and prints out only the longest line
- Write a shell script that takes a two file extensions as input (call them ext1 and ext2), and renames all files ending with ext1 to end with ext2.
- write a shell script that takes several file names as inputs, and copies itself to each of the files. don't forget to set execute permissions on the target files.
Part 6: Getopt and Select
- Take the lc shellscript from part 4 (which lowercases a file) and modify it so it asks the user for a file name from all the files in the directory.
- Write a shell script that asks the user for a file extension (select from all existing extensions in the directory) and prints how many files in the directory have that extension
- Modify the previous script to take the extension using a command line switch. Running example:
count_files -e mp3should print how many mp3 files are in the directory. Running without the switch should perform the same behaviour as the original script.
- Write a shell script that takes a file name and a new name, and changes all files with the given name (recursively in all folders) to the new name. The script should accept a boolean switch
-n. When running with -n it should just print all the mv operations without running them.
Part 7: Named Pipes
- Create a named pipe called
- Print out the list of files to the named pipe. Notice ls blocks.
- Read the contents of the pipe using cat. Notice ls unblocks.
Write a shell script that creates a named pipe and listens on it. For every new line it reads from the pipe, it should create a new file whose name is the first word in the line. Can you delete the named pipe when the script ends ?
Write a shell script that creates a named pipe and then executes
find /. If it reads the word "exit" from the named pipe, it should stop the find and quit. Hint:
$!is the process id of the last started process
Part 8: Functions
- Write a shell function called sum that returns the sum of its arguments
- Write a shell function called countExecutables() that takes a directory name as parameter and returns the number of executable files in that directory.
- Write a shell function that prints out the multiplication table. Function should take a number
nand print a table sized
n*n. For example, running
mul 5should produce:
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
- Write a shell script that includes the following functions:
add_contacttakes a name and an email.
list_contactsprints out a list of all available contact details and emails
email_contacttakes a name and some text, and sends the text to the contact's email address (as specified before when the contact was added).
Use a contacts.txt file to store the data.
Now write another script which uses the functions
- Write a shell function called
treethat prints an indented directory tree (without using the
Part 9: Awk and Sed
- Add a blank line after each line of input
- Change an existing file, so each line should start with a '> '
- Write a shell script that takes a file name as input and performs:
- If a line starts with
#, replace each character with a
- For all other lines, replace each character with a
- If a line starts with
- (*) Delete duplicate words from input line
- Replace the first and last word in every line
- The command
/sbin/ifconfigprints out information about network interfaces. Here's a sample output for the command:
Write a shell script that runs
en3: flags=8963 mtu 1500 options=60 ether 32:00:18:24:c0:00 media: autoselect status: inactive p2p0: flags=8843 mtu 2304 ether 06:38:35:47:96:24 media: autoselect status: inactive
/sbin/ifconfigand creates an information file for each network interface. For example the above interface should produce two files: first is named en3.info and has the en3 block, and the second is named p2p0.info and has the second block