Bash Scripting
Tip
Prerequisites
First you need to find out where is your bash interpreter located. Enter the following into your command line:
fabgt@fabgt-SAT:~/gitclone/Pico-WiFi-Air-Monitoring-Expansion$ which bash
/usr/bin/bash
Hello World!
Open up you favorite text editor and create file called hello_world.sh. Insert the following lines to a file:
NOTE:Every bash shell script in this tutorial starts with shebang:”#!” which is not read as a comment. First line is also a place where you put your interpreter which is in this case: /bin/bash.
Here is our first bash shell script example:
#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING
Navigate to a directory where your hello_world.sh is located and make the file executable:
$ chmod +x hello_world.sh
Now you are ready to execute your first bash script:
./hello_world.sh
Variables
In this example we declare simple bash variable and print it on the screen ( stdout ) with echo command.
#!/bin/bash
STRING="HELLO WORLD!!!"
echo $STRING
test: https://linuxhint.com/bash-test-command/ https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/
Empty Variable
How to Check if Variable is Empty
A popular and simple way to check if a variable is empty is to use the -z option in the condition statement.
The -z $var returns true if a variable is empty and false if not.
The general syntax for such a test is:
#!/bin/bash
if [[ -z $var ]]
then
do
else
do
fi
Example Script
Let us illustrate a simple script that emulates the cd command and navigates the specified directory.
Consider the script below:
#!/bin/bash
echo "Enter path to navigate to: "
read _path
while [[ -z $_path ]]; do
echo "Please provide a path"
done
echo "Navigating to $_path"
cd $_path
The script starts by asking the user to enter the directory to navigate to. It then checks if the variable is empty. If empty, it recursively asks the user for a path until the variable is not empty.
Once the path is available, it navigates to the set directory and prints the status.
Concatenate strings
Concatenating Strings
The simplest way to concatenate two or more string variables is to write them one after another:
VAR1=”Hello,” VAR2=” World” VAR3=”$VAR1$VAR2” echo “$VAR3”
The last line will echo the concatenated string:
Hello, World
You can also concatenate one or more variable with literal strings:
VAR1=”Hello, ” VAR2=”${VAR1}World” echo “$VAR2”
Hello, World
Conditions
unary operator: https://codefather.tech/blog/bash-unary-operator-expected/
Find files
locate command in bash:
sudo apt-get install mlocate
To search a file called xorg.conf, enter:
locate xorg.conf
Sample outputs:
/etc/X11/xorg.conf /etc/X11/xorg.conf.backup /etc/X11/xorg.conf.failsafe /home/vivek/Downloads/xorg.conf.txt /usr/share/man/man5/xorg.conf.5.gz
Instead of writing file names on scree, write the number of matching entries only, enter:
locate -c xorg.conf
Sample outputs:
5
Ignore case matching (i.e. match foo.txt, FOO.TXT, foo.Txt and so on):
locate -i filename
Only find and limit search to one file at a time:
locate -n 1 filename
Only find and limit search to three files at a time:
locate -n 3 filename
To search for a file named exactly NAME (not NAME), use
locate -b '\FILENAME'
Add to PATH
CURL
https://www.geeksforgeeks.org/curl-command-in-linux-with-examples/
ZIP
Assuming I have zip installed:
sudo apt-get install zip
zip files.zip -@ < zip.lst
Or you could skip the list and just glob
zip files.zip *.txt *.jpg