write a linux script named fix_exec.sh that will help set execution bits correct
ID: 673559 • Letter: W
Question
write a linux script named fix_exec.sh that will help set execution bits correctly based on whether or not a file is an actual executable or not. To do this, we are going to need the file command, the chmod command, and the ls command. This is
a difficult script with quite a few moving parts. Before even creating the script file be sure
you understand what the purpose of the script is by reading everything below here. Then,
use the chmod command by hand a few times with some sample files you create to make sure
you know how it works. As always, when you write a script, stop and test often or you will
have a heck of a time debugging at the end. For reference, my script came in at 48 lines
(without comments).
Specifically, the script should take a single argument. The file command should be used to
determine generically what kind of file the argument is and in particular if the file is an
executable type or not. The output of the file command will include the special
"executable" keyword if the file is an executable type. Second, you need to determine the
status of each of the three execute permission bits for the file. To do that, you'll need to use
ls -l and examine the individual execute permissions to see if they are "x" or "-". This
requires looking at substrings of the ls output. You could use cut for this, but there is a
simpler way. Go to this website and scroll down a little ways to the "Substring
Extraction" section to see how to work with substrings:
http://tldp.org/LDP/abs/html/string-manipulation.html
If the file is an executable type, then you need to check each of the execute permissions and if
one is not set, then the script should prompt the user asking them if they would like to set that
execute permission. If so, then use chmod to change that permission. If the execute permission
is already set then don't do anything. On the other hand, if the argument is not an executable
type (as determined by the file command) then you still need to check each execute
permission. In this case, if one is set then you should use chmod to remove that execute
permission and print out a message informing the user of the change.
So, here are the requirements (really pseudocode) for fix_exec.sh:
1. Takes exactly one argument (otherwise print out usage message and exit).
2. Checks that the argument is a regular file (otherwise print out a message and exit).
3. Use file and look for the "executable" keyword in the output to determine if the
file is an executable type or not.
4. Use ls -l and bash substrings to determine which of the three (user, group, other)
execute bits are set to "x" and which are set to "-".
5. If the argument is an executable type then check each execute permission and:
a. If the execute permission is set do nothing.
b. If it isn't set, prompt the user (using read) and ask if they want to set the execute
permission. If they say yes, then use chmod to add the permission.
6. If the argument is not an executable type then check each execute permission and:
a. If it is set, use chmod to remove the permission and print a message to inform the
user.
b. If it isn't set, do nothing.
Note that you may do multiple operations in either case. For example, you may remove
all three execute permissions if they are set incorrectly, or add all three if the file is an
executable type. This does not mean that you need a loop (you don't!).
To test your script, you'll need to add some non-executable files (just plain normal text files) and
some executable files (like shell scripts). Then use your fix_exec.sh script and test each of
the cases to make sure it does the right thing.
Here is some sample output to help guide you. Note that the bolded lines that start with
"[user@localhost ]$" are the terminal prompts where you type in a command, notpart of the script.
Once you've got it tested and working,
Explanation / Answer
whether or not a file is an actual executable or not :
SVN ignores UNIX file permissions when commiting. The way that you set exec permissions on a file is to change the svn properties for that file. If you are on Windows, the easy way to do this is to install Tortoise SVN. Then after you have committed, use Tortoise to open the repo-browser. Find a file that needs to be executable and right-click it to open properties.
The dialog that appears is the SVN properties dialog. Click New to add a new property, select svn:executable from the dropdown list, and set the property value to *.
That's it. When you click OK, it is committed to the SVN repo. The next time you, or the build machine, do an svn update, that file will have executable permissions on Unix/Linux.
Script will take a single argument:
#!/bin/bash
if [ "$1" == "" ]; then
echo "$0: Please provide a directory name"
exit 1
fi
if [ ! -d "$1" ]; then
echo "$0: $1 is not a directory name"
exit 1
fi
echo "$1"
This how you test a file :
Name
file - determine file type
Synopsis
file [-bchikLNnprsvz0] [--apple] [--mime-encoding] [--mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...
file -C [-m magicfiles]
file [--help]
Description
This manual page documents version 5.04 of the file command.
file tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests. The first test that succeeds causes the file type to be printed.
The type printed will usually contain one of the words text (the file contains only printing characters and a few common control characters and is probably safe to read on an ASCII terminal), executable (the file contains the result of compiling a program in a form understandable to some UNIX kernel or another), or data meaning anything else (data is usually 'binary' or non-printable). Exceptions are well-known file formats (core files, tar archives) that are known to contain binary data. When modifying magic files or the program itself, make sure to preserve these keywords. Users depend on knowing that all the readable files in a directory have the word 'text' printed. Don't do as Berkeley did and change 'shell commands text' to 'shell script'.
Change permissions to a file :
The chmod(1) command is used to change permission. The simplist way to use the chmod command is to add or subtract the permission to a file. A simple plus or minus is used to add or sumtract the permission.
You may want to prevent yourself from changing an important file. Remove the write permission of the file "myfile" with the command
chmod -w myfile
If you want to make file "myscript" executable, type
chmod +x myscript
You can add or remove more than one of these attributes at a time
chmod -rwx file
chmod +wx file
You can also use the "=" to set the permission to an exact combination This command removes the write and execute permisison, while adding the read permission:
chmod =r myfile
Note that you can change permissons of files you own. That is, you can remove all permissions of a file, and then add them back again. You can make a file "read only" to protect it. However, making a file read only does not prevent you from deleting the file. That's because the file is in a directory, and directories also have read, write and execute permission.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.