Question

In: Computer Science

create an executable bash runtests.sh as follows: The script must include the definition of a recursive...

create an executable bash runtests.sh as follows:

  1. The script must include the definition of a recursive function, explore
  2. The script prompts the user to enter a directory name and an executable name, and checks that they are both readable and executable (terminating with an error message if not). It then passes the two names to the explore function.
  3. The explore function carries out a recursive traversal of the directory and all its subdirectories, and runs the executable on each of the readable files found, using the file content as input, e.g.
    someexe < filefound

Solutions

Expert Solution

Writing and naming

A shell script is a sequence of commands for which you have a repeated use. This sequence is typically executed by entering the name of the script on the command line. Alternatively, you can use scripts to automate tasks using the cron facility. Another use for scripts is in the UNIX boot and shutdown procedure, where operation of daemons and services are defined in init scripts.

To create a shell script, open a new empty file in your editor. Any text editor will do: vim, emacs, gedit, dtpad et cetera are all valid. You might want to chose a more advanced editor like vim or emacs, however, because these can be configured to recognize shell and Bash syntax and can be a great help in preventing those errors that beginners frequently make, such as forgetting brackets and semi-colons.

Syntax highlighting in vim

  

In order to activate syntax highlighting in vim, use the command

:syntax enable

or

:sy enable

or

:syn enable

You can add this setting to your .vimrc file to make it permanent.

Put UNIX commands in the new empty file, like you would enter them on the command line. As discussed in the previous chapter (see Section 1.3), commands can be shell functions, shell built-ins, UNIX commands and other scripts.

Give your script a sensible name that gives a hint about what the script does. Make sure that your script name does not conflict with existing commands. In order to ensure that no confusion can rise, script names often end in .sh; even so, there might be other scripts on your system with the same name as the one you chose. Check using which, whereis and other commands for finding information about programs and files:

which -a script_name

whereis script_name

locate script_name

script1.sh

In this example we use the echo Bash built-in to inform the user about what is going to happen, before the task that will create the output is executed. It is strongly advised to inform users about what a script is doing, in order to prevent them from becoming nervous because the script is not doing anything.

Write this script for yourself as well. It might be a good idea to create a directory ~/scripts to hold your scripts. Add the directory to the contents of the PATH variable:

export PATH="$PATH:~/scripts"

If you are just getting started with Bash, use a text editor that uses different colours for different shell constructs. Syntax highlighting is supported by vim, gvim, (x)emacs, kwrite and many other editors; check the documentation of your favorite editor.

Executing the script

The script should have execute permissions for the correct owners in order to be runnable. When setting permissions, check that you really obtained the permissions that you want. When this is done, the script can run like any other command:

willy:~/scripts> chmod u+x script1.sh

willy:~/scripts> ls -l script1.sh

-rwxrw-r-- 1 willy willy 456 Dec 24 17:11 script1.sh

willy:~> script1.sh

The script starts now.

Hi, willy!

I will now fetch you a list of connected users:

3:38pm up 18 days, 5:37, 4 users, load average: 0.12, 0.22, 0.15

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

root tty2 - Sat 2pm 4:25m 0.24s 0.05s -bash

willy :0 - Sat 2pm ? 0.00s ? -

willy pts/3 - Sat 2pm 3:33m 36.39s 36.39s BitchX willy ir

willy pts/2 - Sat 2pm 3:33m 0.13s 0.06s /usr/bin/screen

I'm setting two variables now.

This is a string: black

And this is a number: 9

I'm giving you back your prompt now.

willy:~/scripts> echo $COLOUR

willy:~/scripts> echo $VALUE

willy:~/scripts>

This is the most common way to execute a script. It is preferred to execute the script like this in a subshell. The variables, functions and aliases created in this subshell are only known to the particular bash session of that subshell. When that shell exits and the parent regains control, everything is cleaned up and all changes to the state of the shell made by the script, are forgotten.

If you did not put the scripts directory in your PATH, and . (the current directory) is not in the PATH either, you can activate the script like this:

./script_name.sh

A script can also explicitly be executed by a given shell, but generally we only do this if we want to obtain special behavior, such as checking if the script works with another shell or printing traces for debugging:

rbash script_name.sh

sh script_name.sh

bash -x script_name.sh

The specified shell will start as a subshell of your current shell and execute the script. This is done when you want the script to start up with specific options or under specific conditions which are not specified in the script.

If you don't want to start a new shell but execute the script in the current shell, you source it:

source script_name.sh

source = .

  

The Bash source built-in is a synonym for the Bourne shell . (dot) command.

The script does not need execute permission in this case. Commands are executed in the current shell context, so any changes made to your environment will be visible when the script finishes execution:

willy:~/scripts> source script1.sh

--output ommitted--

willy:~/scripts> echo $VALUE

9


Related Solutions

Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
The name of your bash script must be script1 and below is a description of what...
The name of your bash script must be script1 and below is a description of what it should do when executed. The script displays a message Guess, what is zip program's location Then it displays a message Enter the full path e.g. /etc/zip or /proc/bus/zip Then it reads the users response If the user's response is correct, it displays a message Great guess, zip's location is XXX, where XXX is the correct location of zip , and the script terminates....
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result.
Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
Determine if each of the following recursive definition is a valid recursive definition of a function...
Determine if each of the following recursive definition is a valid recursive definition of a function f from a set of non-negative integers. If f is well defined, find a formula for f(n) where n is non-negative and prove that your formula is valid. f(0) = 1, f(n) = -f(n-1) + 1 for n ≥ 1 f(0) = 0, f(1) = 1, f(n) = 2f(n-1) +1 for n ≥ 1 f(0) =0, f(n) = 2f(n-1) + 2 for n ≥...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT