This article is primarily aimed at those who are newer to shell scripting. I go into depth to hopefully show beginners a practical use for bash scripting, and provide a full explanation for what the computer is being instructed to do.
Have you ever wanted to run a command to display the date, time, temperature, and battery charge on your computer? More than likely, you might not need such a script, but if you end up in the command line, or if your system, for some reason stops displaying certain things on your panel, then this might prove useful.
Just open the text editor of your choice and save it as “stats” (without the quotes, of course), or whatever you want to name it. Then type the following code, which I will explain afterwards:
#!/bin/bash echo -en “\e[?25l” while [ true ] do clear echo -en “\e[31m” date echo -en “\n\e[33mCPU Temperature: “ cat /sys/class/thermal/thermal_zone0/temp echo -en “\n\e[32mBattery State:\n\n” cat /proc/acpi/battery/BAT0/state echo -en “\e[0m” if ! read -sn 1 -t 1 cmd ; then continue fi if [ $cmd == 'q' ] ; then break fi done echo -en “\e[?25h” exit
Now, to walk you through the script so you know what is happening:
#!/bin/bash tells the terminal that it is a bash script so it is able to read the commands accordingly.
echo -en sends output to the terminal, basically something to read.
“\n” prints a new line so it doesn’t put everything onto one line.
“\e[” are what I refer to as escape – e sequences. They are usually followed by a number, or two numbers separated by a semicolon, and ending with a letter. The letter tells you what the general purpose of that particular sequence is, “m” for example tells you the font color is going to change.
“\e[32m” basically tells it to print green text, 31 is red, and 33 is yellow. If you use 41, 42, 43, etc, it will change the background color. You can read up on ansi escape sequences for scripts you write in the future.
Last escape sequences “\e[?25l” turns off the cursor so the display looks, in my opinion, more professional. Follow the 25 with a lowercase ‘h’ and it will turn the cursor back on.
The while [ true ] loop basically executes from the “do” to the “done” lines an infinite number of times. If you are into programming in other languages, you might be familiar with setting different conditions in it to check against.
if ! read -sn 1 -t 1 cmd ; then
continue
fi
if called an “if-statement”, which checks the top line, and if applicable, executes what is in it. The continue basically tells it to start the loop over. An exclamation mark is a negation, like saying it is not the case. read is a command telling the shell to read input from the user. -s after read is an argument that tells it to not show what is being written into it. -n 1 tells it to automatically hit enter after the first key is pressed. -t 1 tells it to give you 1 second to press the key.
Basically, it is saying “if you don’t read silently the first keystroke in one second to a variable called “cmd” then continue this loop”. The second if statement tells it to break out of the loop if ‘q’ is entered.
cat is short for concatenate, and essentially puts the contents of a plain text document onto the screen without having to open a text editor. Understanding the Linux file system, and where certain information goes is one way to take advantage of all the features of your operating system. The directories that concatenated here are different on a Mac, or some other filing conventions used by some distros. Take time to learn your file system.
Lastly, I will show you the display and explain how to read it.
Mon Apr 18 11:16:32 CDT 2016
CPU temperature: 46000
Battery state:
present: yes
capacity state: ok
charging state: charging
present rate: 310 mA
remaining capacity: 1331 mAh
present voltage: 16800 mV
I like a black background for my terminal, so these colors contrast it quite nicely. The CPU temperature is ok generally up to 80000 on my computer, yours might be different. I’d suggest you Google your own computer specs so you can keep it in a good temperature and not fry it.
For the battery state you will want to pay attention to the charging state for either charging, charged, or discharging. This will let you know if your charger is working. The remaining capacity is going to tell you how full your battery is. If you check it when your battery is fully charged you can get a rough idea on its percentage when it’s not fully charged.
The last thing you will want to do with the script is
# sudo mv stats /bin/ # sudo chmod +x /bin/stats
The first command moves your script to your /bin/ directory. This is a directory that the computer checks when you enter a command into the terminal. You can find all your command paths by entering
# echo $PATH
into the terminal. You can move this to any of those directories and it will execute your script no matter what directory you are in when you type it.
The second line chmod +x makes your script executable. Otherwise your computer will never have permission to execute it.
I hope this can be useful in case you ever need it. Bash scripting is a useful programming language to learn. There are all sorts of tutorials online, you can Google it, or even watch videos on YouTube if you want to learn even more. Happy coding!