-->

Variable Creating Bash Scripts – Part 2


Previously we looked at creating your first Bash script, Hello World, and adding a system variable. Now you can expand these and see what you can do when you start to play around with creating your own unique variables.

Just as in every other programming language a Bash script can store and call certain variables from the system, either generic or user created.


Step 1


xed hello.sh. In it enter: 
#!/bin/bash, then,
echo Hello $1. 

Save the file and exit Xed. Back in the Terminal make the script executable with: 

chmod +x hello.sh.





Step 2


As the script is now executable, run it with

./ hello.sh.

Now, as you probably expected a simple ‘Hello’ is displayed in the Terminal. However, if you then issue the command with a variable, it begins to get interesting. For example, try

./hello.sh David.





Step3


The output now will be Hello David. This is because Bash automatically assigns variables for the user, which are then held and passed to the script. So the variable ‘$1’ now holds ‘David’. You can change the variable by entering something different: 
./hello.sh Mint.







Step 4


You can even rename variables. Modify the hello. sh script with the following:

firstname=$1,
surname=$2, echo Hello $firstname $surname.

Putting each statement on a new line. Save the script and exit back into the Terminal.






Step 5


When you run the script now you can use two custom variables: 
./hello.sh David Hayward.

Naturally change the two variables with your own name; unless you’re also called David Hayward. At the moment we’re just printing the contents, so let’s expand the two-variable use a little.





Step 6


Create a new script called addition.sh, using the same format as the hello.sh script, but changing the variable names. Here we’ve added firstnumber and secondnumber, and used the echo command to output some simple arithmetic by placing an integer expression, echo The sum is $(($firstnumber+$secondnumber)). Save the script, and make it executable (chmod +x addition.sh).





Step 7



When you now run the addition.sh script we can enter two numbers: 
./addition.sh 1 2

The result will hopefully be 3, with the Terminal displaying ‘The sum is 3’. Try it with a few different numbers and see what happens. See also if you can alter the script and rename it do multiplication, and subtraction.





Step 8


Let’s expand things further. Create a new script called greetings.sh. Enter the scripting as below in the screenshot, save it and make it executable with the chmod command. You can see that there are a few new additions to the script now.





Step 9



We’ve added a –n to the echo command here which will leave the cursor on the same line as the question, instead of a new line. The read command stores the users’ input as the variables firstname and surname, to then read back later in the last echo line. And the clear command clears the screen.






Step 10


As a final addition, let’s include the date variable we used in the last section. Amend the last line of the script to read: 

echo Hello $firstname $surname, how are you on this fine $(date +%A)?. 

The output should display the current day of the week, calling it from a system variable.



0 Response to "Variable Creating Bash Scripts – Part 2"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel