BASH Variables
When people first start writing scripts they think of them as single use. If they need to make an adjustment then they need to reedit the script in multiple places to get it to work for the new task. Or maybe you write a script on one server that you then want to move to other servers and you need to pick up say the server name in the script. You can make your script much more versatile using something called variables. In programming languages a variable can be thought of as a box that can hold a value that you can easily change, and you can retrieve that value at any time easily. Sometimes you will set the value of the variable at the top of the program or script and repeatedly reference it farther down. If you need to make some sort of change that affects every time this value is used in the script you simply change the variable once and it automatically "resets" the value through the whole program.
Let's take a simple, maybe a little silly, example. The script will call me by name as it runs and gives me statuses.
Let's take a simple, maybe a little silly, example. The script will call me by name as it runs and gives me statuses.
Basic variable use
|
|
I created a variable called USRNAME and then put the value of my name inside of it. Now to use the value all I need to do is start with a $ then the name of the variable. In bash your variable does not have to be all upper case, but a lot of people use that as a good practice to easily identify them. In this case I am simply echoing a comment on screen and where the variable $USRNAME is the script will insert my name instead.
If I were to change the value from Rusty to say Sally then the next time I ran the program it would say Sally instead of Rusty each time. So I can change what name is displayed in multiple commands just by making a single edit. It is not uncommon to see a series of variables at the top of a script. Then if something changes in the environment you can adjust the script to accommodate by simply adjusting the appropriate variables.
However, this is still fairly static, and it requires you to edit the file to change the variable name. We can get the program to ask the user for their name and then use that name in the rest of the script. We will use the bash built in command read to do this. I can use the -p to give a prompt asking the user what information I want. What they type in will then be stored in the variable which is named as the end of the command.
If I were to change the value from Rusty to say Sally then the next time I ran the program it would say Sally instead of Rusty each time. So I can change what name is displayed in multiple commands just by making a single edit. It is not uncommon to see a series of variables at the top of a script. Then if something changes in the environment you can adjust the script to accommodate by simply adjusting the appropriate variables.
However, this is still fairly static, and it requires you to edit the file to change the variable name. We can get the program to ask the user for their name and then use that name in the rest of the script. We will use the bash built in command read to do this. I can use the -p to give a prompt asking the user what information I want. What they type in will then be stored in the variable which is named as the end of the command.
Populating a variable with read
|
|
So in our last example I prompted the user for some information. This is handy for an interactive script. But let's say I want to write a script once, and put it on a number of different servers. I want the script to know the name of the server it is on. I can send the output of a command into the variable. The trick here is that to send the output of the bash command is that you have to enclose the entire command in back ticks. They look like single quotes, but they go the other direction. You can typically find that character on the same key as the tilde over to the left of the number 1 key. Make sure you use that and not a single quote.
Send output of command into variable
This example script will retrieve the hostname of the server and put it in the MYSRV variable which we can then use farther down the script. We are using it both to write on the first line of the file the name of the server this came from. and we are also using the variable as part of the filename. The script then emails the resulting report file out automatically too. So I am able to write this script once, and then copy it to as many servers as I need and I don't have to make any change. BTW this would be a perfect candidate for using a cron job then to automatically run the job every day. That way I don't have to log into 30 different servers just to run it each day.
Home |
About |
Services |
Copyright © 2016