bashRead everything before doing anything!
Write a script that will take a single filename as an argument and adds execute permission to the file for the user, but only if the file is a regular file. Name the script addexec.sh Your script must check to see that there is exactly one argument. If there are no arguments or more than one arguments, your script must produce a "usage" message that tells how to use the script. You write it this way:
echo "Usage: $0 filename"
The $0 (that’s a zero, not the letter “Oh”), is a special variable that contains the name of the script file that is currently running.
You must check to see if the file exists; if it doesn't, give an error message. You must check to see if the file is a regular file; if not, give an appropriate message. Here is the result of running the script several times, with extra blank lines to make things more readable.
linux199@linux:~> touch file1 linux199@linux:~> mkdir dir1 linux199@linux:~> ./addexec.sh Usage: ./addexec.sh filename linux199@linux:~> ./addexec.sh file1 file2 Usage: ./addexec.sh filename linux199@linux:~> ./addexec.sh dir1 dir1 is not a regular file. linux199@linux:~> ./addexec.sh nosuchfile nosuchfile does not exist. linux199@linux:~> ls -l file1 -rw-r--r-- 1 linux199 users 0 2006-11-21 17:38 file1 linux199@linux:~> ./addexec.sh file1 linux199@linux:~> ls -l file1 -rwxr--r-- 1 linux199 users 0 2006-11-21 17:38 file1
Note: You should not presume that all files have permission 0644. You should add execute permission ONLY for the user, and leave all other permissions exactly as they were.
Because most of you are not programmers, here is some “pseudo-code” (half Bash script, half English) to tell you how the program should look. Text in italic is text that you need to put in yourself.
#!/bin/bash
if # of arguments is one
then
if file in $1 exists
then
if file in $1 is a regular file
then
add execute permission
else
print message that this is not a regular file
fi
else
print message that the file does not exist
fi
else
print the "Usage" message
fi
Write a script that will do an ls -l command on
each of its arguments only if the argument is the name of a regular file.
This script will have a
for loop like the one on page 451, and inside the loop will
an if test that uses one of the file test options listed
on page 439.
This script is independent of the previous script.
This script will do an ls -l on regular files.
It will not touch their permissions at all.
Name your script listfiles.sh
Here’s how it will work. Presume you have files named xyz and abc, and a directory named mydir. If you runs the script:
./listfiles.sh xyz mydir abc nosuchfile
You will get output like this, since xyz and abc are the only two regular files:
-rw-r--r-- 1 linux199 users 24490 2006-11-21 15:26 xyz -rw-r-xrw- 1 linux199 users 5963 2006-11-16 18:47 abc
Upload both scripts to Moodle.