In: Computer Science
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:
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:
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.
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.
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