https://linuxconfig.org/bash-scripting-tutorial
which bash
Bash shell script 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
.
#!/bin/bash
# enable extglobbing for selective removal of file
shopt -s extglob
cd /home/miranda/workspace/CodeBench/exercises/migrations
# rm all but __init__.py
rm !(__init__.py)
# clear mysql database
# read password from enviroment variable $MYSQL_DB_PASS
echo "DROP DATABASE IF EXISTS codebench;CREATE DATABASE codebench;" | mysql -uroot -p$MYSQL_DB_PASS
cd /home/miranda/workspace/CodeBench/
python manage.py makemigrations
python manage.py migrate
# python manage.py createsuperuser --username=admin [email protected]
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', '[email protected]', 'admin')" | python manage.py shell
chmod +x *.sh
./clear_data.sh
https://help.ubuntu.com/community/EnvironmentVariables
System-wide environment variables
sudo nano /etc/environment
You may add a line like:
DB_PASS="looooooooooooooooooooooogpass"
check if it has been setup correctly:
echo $TERM
If you are testing in a different (already opened) terminal, you need to restart it after the change.
Shell config files such as ~/.bashrc
, ~/.bash_profile
, and ~/.bash_login
are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
: '
This is a
very neat comment
in bash
'
to explain: :
is shorthand for true and true does not process any parameters.
manual page: SYNOPSIS true [ignored command line arguments]
https://stackoverflow.com/questions/43158140/way-to-create-multiline-comments-in-bash#
http://www.linfo.org/echo.html
No qoutes arount string. e.g. echo The number is $x.
$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
https://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
## declare an array variable
declare -a arr=("element1"
"element2" "element3"
"element4"
)
## now loop through the above array
for i in "${arr[@]}"
do
echo "$i"
# or do whatever with individual element of the array
done
# You can access them using echo "${arr[0]}", "${arr[1]}" also
https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash
https://www.thegeekstuff.com/2010/06/bash-array-tutorial/
PS4='Line ${LINENO}: ' bash -x install.sh
https://stackoverflow.com/a/17805088/646732
sed -i '/<anker line>/a <new line to append after anker line>' path/to/file
-i.bak
save a back up file with extension .bak
sed -i 's/^psql_pswd=".*"/psql_pswd="'$psql_pswd'"/' $db_script
echo 'This is a foo test' | sed -e 's/\<foo\>//g'
sed -e 's/\<regex-for-word\>//g' input > output
https://stackoverflow.com/questions/9633114/unix-script-to-remove-the-first-line-of-a-csv-file
sed 1q file.csv # print 1 line then quit
sed 1d file.csv > newfile # delete 1st line, save content to new file, original file not changed
To delete lines matching a pattern that contains forward slashes, a nonstandard character for a pattern delimiter can be used, the first use of that character must be escaped:
$ sed -i '\|^/dev/xvdb|d' /etc/fstab
https://misc.flogisoft.com/bash/tip_colors_and_formatting
awk -F, '{print $2}' file.csv
# https://stackoverflow.com/a/5750463/646732
set -o xtrace
.
.
.
# set it back after script
set +o xtrace
# exit the script if any command fails with exit 1
# https://stackoverflow.com/a/4346420/646732
set -e
https://ss64.com/bash/read.html
while IFS="" read -r p || [ -n "$p" ]
do
printf '%s\n' "$p"
done < $tempfile2
https://stackoverflow.com/a/1521498/646732
https://www.thegeekstuff.com/2013/05/uniq-command-examples/
cat $input | sort | uniq > $output
echo " foo bar " | xargs
https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
https://www.gnu.org/software/bash/manual/bashref.html#Redirections
Redirecting Input >
> file
initialize file empty
Redirecting Output <
while IFS="" read -r line || [ -n "$line" ]
do
done < $tempfile2
feed file coment to some command
Appending Output >>
Here Documents <<
Here Strings <<<
http://linuxcommand.org/lc3_wss0120.php
#!/bin/bash
echo "Positional Parameters"
echo '$0 = ' $0
echo '$1 = ' $1
echo '$2 = ' $2
echo '$3 = ' $3
dos2unix file.sh
https://stackoverflow.com/questions/11616835/r-command-not-found-bashrc-bash-profile/32912867