Question

In: Computer Science

Your First Bash Script Now that we know how to execute multiple commands and error check...

Your First Bash Script

Now that we know how to execute multiple commands and error check using the command-line, we will start to put logic into a script. Bash scripts are interpreted by bash just like commands on the command-line.

Use an editor to make the following script, called count.sh:

echo “1”

echo “2”

echo “3”


You can now run this script using the command:

$ bash count.sh


This feeds the script to the bash interpreter, which interprets the commands.

Executable Scripts

We can also write scripts that you can “run” like a program, meaning they are executable and they have a comment that helps the shell realize it should be fed to the bash interpreter.

Modify your script so it looks like:

#!/bin/bash




echo “1”

echo “2”

echo “3”


Make the file executable. Now, you can run it like this:

$ ./count.sh

Flag this Question

Bash Syntax

We will learn in this section what variables, loops and conditionals look like in bash.

Variables

Variables are case sensitive. It is common convention to use uppercase for global variables and lower-case for local variables or loop variables. By default:

  • Variables are created the moment they are referenced.
  • They hold whatever value they are assigned.
  • Their values are accessed using $ notation.
  • Variables hold their value until they are ‘unset.’

Try this.

$ PLAY=$RANDOM; echo $PLAY; echo $PLAY; unset PLAY; echo $PLAY


Try the above command again several times. The $RANDOM variable is a special bash built-in variable and returns a different value each time.

Now, modify the assignment instruction slightly, and try the below:

$ PLAY = $RANDOM; echo $PLAY


Notice that bash is whitespace sensitive: the spaces around this assignment operator result in an error. There are no spaces before or after a valid assignment statement.

Try it.

Try the below commands to see the difference.

$ whoami               # a simple command

$ echo whoami          # command is not called

$ whoami | echo        # using pipe to run and redirect

$ echo $(whoami)   # command expansion

$ echo `whoami`    # inline execution


The last two commands causes bash to execute whoami, take the output and call echo with that string as its first argument.

Positional Arguments

Positional arguments are accessed through automatic names: $1 for the first argument, $2 for the second argument, etc.

Exercise

Modify your count.sh script to look like:

#!/bin/bash




echo “$1”

echo “$2”

echo “$3”


Now run the command:

$ ./count.sh it is my script

If you get a "permission denied" error, your file likely needs to get execute privileges on the owner. Use chmod appropriately and feel free to refer to the prior HTML lab for this.

If you get an error like "bad interpreter: no such file or directory", you likely copied and pasted the code above, and you're running into character encoding issues between the doc and unix. You can either:

  1. Rewrite the script in vim/emacs
  2. Use the dos2unix command on the script by running the following:
dos2unix ./count.sh

Reproduce the output below. It should have 3 lines; fill in each line below,

______________

_______________

_______________

Now run the command:

$ ./count.sh it is “my script”


Reproduce the output below:

_________________

__________________

__________________

This example begins to show the role of quoting for preserving whitespace. The careful treatment of whitespace can be one of the most challenging aspects of bash. We discuss two types of quoting next.

Solutions

Expert Solution

Executed these command in Linux as request. Let me know if you have doubts.

Use an editor to make the following script, called count.sh: echo “1” echo “2” echo “3”

echo will print the values in the terminal.

You can now run this script using the command: $ bash count.sh

Modify your script so it looks like: #!/bin/bash echo “1” echo “2” echo “3”

Make the file executable. Now, you can run it like this: $ ./count.sh

$ PLAY=$RANDOM; echo $PLAY; echo $PLAY; unset PLAY; echo $PLAY


Try the above command again several times. The $RANDOM variable is a special bash built-in variable and returns a different value each time.

RANDOM will randomize the value every time it is executed.

Now, modify the assignment instruction slightly, and try the below:

$ PLAY = $RANDOM; echo $PLAY

Try the below commands to see the difference.

$ whoami               # a simple command

Displays user, group and privileges information for the user who is currently logged on to the local system
$ echo whoami          # command is not called

$ whoami | echo        # using pipe to run and redirect

$ echo $(whoami)   # command expansion

$ echo `whoami`    # inline execution

Modify your count.sh script to look like: #!/bin/bash echo “$1” echo “$2” echo “$3” Now run the command: $ ./count.sh it is my script

If you get a "permission denied" error, your file likely needs to get execute privileges on the owner. Use chmod appropriately and feel free to refer to the prior HTML lab for this.

  1. Use the dos2unix command on the script by running the following:
dos2unix ./count.sh

Reproduce the output below. It should have 3 lines; fill in each line below,

Echo will print the value of 1, 2 , 3 which is not declared here to the output will be empty.

Since the values of $1 , $2, $3 were not defined, the output to each line is blank.

Now run the command: $ ./count.sh it is “my script” - It is not clear what should be done or reproduced.

If you have any doubts, leave a comment below before rating. I'll be happy to assist you further


Related Solutions

Write a Matlab script-file probl1.m to execute the requested commands (as much as possible) in the...
Write a Matlab script-file probl1.m to execute the requested commands (as much as possible) in the exercises below. Increase N a number of times according to N = 4, 8, 16, 32, 64, 128, . . . (1) Determine for each N the (exact) error. (2) Determine for N ≥ 16 also the convergence ratio q(h/2). This script should be based on a function-file trap.m (trapezoidal integration) as follows: function [totarea] = trap(N) format long; a = 0; b =...
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within...
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within your script  Provide user feedback  Use loops  Use conditionals Remember to use chmod +x to make your file executable! Each script is 5 points in the Specifications portion of the rubric. Don’t forget to maintain good standards and comments. Script 1 – Echo-back some information Write a script name hello.sh that will take the user’s first name as a command line...
What is a working set? What is the first line of every bash script? What is...
What is a working set? What is the first line of every bash script? What is the permission string (RWXRWXRWX format) of the number 311 What is the base directory of a file system called on windows, and on linux What does the term 'super user' mean in terms of operating systems
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....
Write a bash script that will allow you to: Read in your Your name Course name...
Write a bash script that will allow you to: Read in your Your name Course name and Instructor’s name Then prompt the user to enter two numbers and determine which number is greater. Ex: If 10 is entered for the first number and 3 for the second, you should output Your name Course name Instructor’s name 10 is greater than 3. If 33 is entered for the first number and 100 for the second, you shou output Your name Course...
In a multiple linear regression how do we calculate the standard error of B2 ( we...
In a multiple linear regression how do we calculate the standard error of B2 ( we have two independent variables and a constant so we have B0 B1 and B2) how do we calculate the standard error of the three.
Plato is concerned with the question of how we now what we know. What are the...
Plato is concerned with the question of how we now what we know. What are the fundamental issues that he is raising?
How do we check if the independent variables are statistically significant and contribute to the multiple...
How do we check if the independent variables are statistically significant and contribute to the multiple regression models?
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Linux Question: Describe how you created above file and its contents in VI. a) Bash/VI commands:...
Linux Question: Describe how you created above file and its contents in VI. a) Bash/VI commands: 20pts; b) Non-brute-force solution: 10pts). Note: Brute-force solution means that entering characters one by one Install JDK 1.8 by the following command: $ sudo dnf –y install java-1.8.0-openjdk-devel.x86_64 Use VI to create a file “YourFirstNameHomework.java” (e.g., “MollyHomework.java”) and add the following contents into it. (grading details: file name format:10 pts, a screenshot of the file content in the VI environment: 10pts, paragraphs: 10pts, empty...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT