Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a 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.

write a 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 54 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:

Takes exactly one argument (otherwise print out usage message and exit).

Checks that the argument is a regular file (otherwise print out a message and exit).

Use file and look for the "executable" keyword in the output to determine if the file is an executable type or not.

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 "-".

If the argument is an executable type then check each execute permission and:

If the execute permission is set do nothing.

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.

If the argument is not an executable type then check each execute permission and:

If it is set, use chmod to remove the permission and print a message to inform the user.

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 lab4]$" are the terminal prompts where you type in a command, not part of the script.

[user@localhost lab4]$ ls -l

total 40

-rwxrw-r-- 1 user user 41 Feb 12 00:02 executable.sh

-rwxrw-r-- 1 user user 1201 Feb 11 23:45 fix_exec.sh

drwx------ 2 root root 16384 Feb 11 21:45 lost+found

-rw-rw-r-x 1 user user 16 Feb 12 00:02 regular_file

[user@localhost lab4]$ file executable.sh regular_file

executable.sh: Bourne-Again shell script text executable

regular_file: ASCII text

[user@localhost lab4]$./fix_exec.sh

usage: ./fix_exec.sh filename

[user@localhost lab4]$./fix_exec.sh too many args

usage: ./fix_exec.sh filename

[user@localhost lab4]$./fix_exec.sh regular_file

removing others execute bit from regular_file

[user@localhost lab4]$./fix_exec.sh executable.sh

should the execute bit for executable.sh be set for the group (y/n)? y

should the execute bit for executable.sh be set for others (y/n)? y

[user@localhost lab4]$ ls -l

total 40

-rwxrwxr-x 1 user user 41 Feb 12 00:02 executable.sh

-rwxrw-r-- 1 user user 1201 Feb 11 23:45 fix_exec.sh

drwx------ 2 root root 16384 Feb 11 21:45 lost+found

-rw-rw-r-- 1 user user 16 Feb 12 00:02 regular_file

[user@localhost lab4]$ urgent , need before 4:00pm EST

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago