The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 113     Votes:  4     ✅ Solution
Tags: bash   scripts  
Link: 🔍 See Original Answer on Ask Ubuntu ⧉ 🔗

URL: https://askubuntu.com/q/840293
Title: I need some help understanding this bash script
ID: /2016/10/22/I-need-some-help-understanding-this-bash-script
Created: October 22, 2016    Edited:  June 12, 2020
Upload: April 8, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


The magic of built-in bash variable $SECONDS

Your script highlights the built-in bash variable $SECONDS that keeps track of how many seconds a bash script has been running. Initially it starts at zero and the working variable current is set to this value at the beginning of the script. Then the script loops and increments current each time $SECONDS changes and displays “1, 2, 3… 10” on the screen.


Analyzing bash script line by line:

` #!/bin/bash` tells the system this is a bash script

` current=0` sets the variable current to 0

` while [ $SECONDS -le 10 ]; do` When the number of seconds this script has been running is less than or equal to 10 do the following

` if [ $SECONDS -eq ${current} ]; then` if the value of current is equal to number of seconds then:

` echo ${current}` display the current value (0 initially, then 1, 2, 3… to 10)

` current=$((${current}+1))` increment current value

` fi` End of If statement, required for syntax rules

` done` end of While loop, required for syntax rules


Testing the script

To test this script copy and paste the OP’s text to a new file. For our purposes call the file seconds. Mark the file as executable with the command:

chmod +x seconds

Then call the bash script with the current directory prefix in front:

./seconds
⇧ Ubuntu changing default audio output after suspend Xubuntu 16.10 won't wake up correctly after opening lid  ⇩