You are on page 1of 1

Sample Shell Scripts

Write a shell script that prints your full name and today's date. Expand it so that it includes the user's home
directory, and the current directory.

Write a shell script that checks whether I (wachsmut) am currently logged in. If so, it should say "the doctor is in",
otherwise it should say "the doctor is out" (recall that "who" gives you a list of everyone logged in)

Modify the above script to check whether a particular user specified on the command line is logged in.

Write a program that says "The end is far away" if today between Jan to April, or "The end is near" in May to
August" or "The end is coming" in Sept to Dec.

Write a program that checks if any of a list of users given on the command line is logged in. For each user it should
say whether he/she is logged in or not.

What does the following script do:

#!/bin/sh
while who |grep -s $1 >/dev/null
do
sleep 60
done
echo "$1 has logged out"

Create a script based on the above example that waits for somebody to login, then prints an appropriate message

Create a shell script that prints a prompt, then waits for a command. If the user enters a command, it executes it and
returns to the "command prompt".

For the remaining scripts, note that "test –f NAME" checks if NAME is an existing file, and "test –d NAME" checks
if NAME is an existing directory.

Write a shell script that checks whether a given file exists. If so, copy the file to another name that is the original
name + the .backup extension

Write a shell script that checks whether a given file exists. If so, compress the file via the 'gzip' command, otherwise
show an error message

Write a shell script that copies all files in the current directory into a "Backup" directory (if the Backup directory
does not exist, it will be created first).

Write a shell script that copies all files in a current directory into a "Backup" directory, then combines all files in
that directory into on "tar" file via the "tar" program, and finally compresses the tarred file via 'gzip'.

Write a shell script that backs up files into a backup directory whose name is today's date. If the directory already
exists, remove the files in it and replace them by the most recent backup.

Write a shell script that backs up all files in a directory into a Backup directory for every day of the week. In other
words, on Monday all files go in a "Monday" or "1" backup directory, on Tuesday they all go into a "Tuesday"
directory, and so forth. If a directory for today already exists, overwrite the files in it, otherwise create the directory.

You might also like