4.4 KiB
Executable File
Table of Contents
Bash Scripting
Bash (für Bourne-again shell) ist eine freie Unix-Shell und Teil des GNU-Projekts.
There are some conventions to follow to ensure that your computer is able to find and execute your Bash scripts. The beginning of your script file should start with #!/bin/bash on its own line. This tells the computer which type of interpreter to use for the script. When saving the script file, it is good practice to place commonly used scripts in the ~/bin/ directory.
The script files also need to have the “execute” permission to allow them to be run. To add this permission to a file with filename: script.sh use: $ chmod +x script.sh. To ensure that scripts in ~/bin/ are available, you must add this directory to your PATH within your configuration file: PATH=~/bin:$PATH. The configuration file is ~/.bashrc (Linux) or ~/.bash_profile (OSX). Now any scripts in the ~/bin directory can be run from anywhere by typing the filename.
Variable
greeting="Hello"
echo $greeting
Conditionals
A complete conditional in a bash script uses the following syntax:
if [ $index -lt 5 ]
then
echo $index
else
echo 5
fi
Bash scripts use a specific list of operators for comparison. Here we used -lt which is “less than”. The result of this conditional is that if $index is less than 5, it will print to the screen. If it is 5 or greater, “5” will be printed to the screen.
Here is the list of comparison operators for numbers you can use within bash scripts:
Equal: -eq
Not equal: -ne
Less than or equal: -le
Less than: -lt
Greater than or equal: -ge
Greater than: -gt
Is null: -z
When comparing strings, it is best practice to put the variable into quotes ("). This prevents errors if the variable is null or contains spaces. The common operators for comparing strings are:
Equal: ==
Not equal: !=
For example, to compare if the variables foo and bar contain the same string: if [ "$foo" == "$bar"]
Loops
for loop
for word in $paragraph
do
echo $word
done
while/until loops
Within bash scripting until and while are very similar. while loops keep looping while the provided condition is true whereas until loops loop until the condition is true.
while [ $index -lt 5 ]
do
echo $index
index=$((index + 1))
done
until [ $index -eq 5 ]
do
echo $index
index=$((index + 1))
done
# example:
#!/bin/bash
first_greeting="Nice to meet you!"
later_greeting="How are you?"
greeting_occasion=0
while [ $greeting_occasion -lt 3 ]
do
if [ $greeting_occasion -lt 1 ]
then
echo $first_greeting
else
echo $later_greeting
fi
greeting_occasion=$((greeting_occasion + 1))
done
arrays
declare
declare ELEMENTS=("value1" "value2")
loop values
for e in "${ELEMENTS[@]}"; do echo "$e" done
associative arrays
declare ELEMENTS=(["key1"]="value1" ["key2"]="value2")
# loop keys:
for e in "${!ELEMENTS[@]}"; do echo "$e" done
# loop values:
for e in "${ELEMENTS[@]}"; do echo "$e" done
User Input
Read
echo "Guess a number"
read number
echo "You guessed $number"
Options
#!/bin/bash
# Bash Menu Script Example
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice $REPLY which is $opt"
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done
access files
files=/some/directory/*
# You can then iterate through each file and do something. Here, lets just print the full path and filename:
for file in $files
do
echo $file
done
iterate file line by line
assuming you have a file delete.txt with id's line by line.
#!/bin/bash
input="delete.txt"
while IFS= read -r line
do
mysql -uroot -pD66-twcpaeb -e "use atom; delete from object where id = $line"
done < "$input"
echo to console AND log file
echo "Some console and log file message" | tee /home/user/logfile.log