Bash: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(31 intermediate revisions by the same user not shown) | |||
Line 19: | Line 19: | ||
for i in {1..50};do \ | for i in {1..50};do \ | ||
history -d 2013;done;\ | history -d 2013;done;\ | ||
history -w | |||
</source> | |||
<source lang="bash"> | |||
count=`wc -l ~/.bash_history|awk '{print $1+1}'`;start_line=$(($count-10));\ | |||
for ((line=$count;line>=$start_line;--line));do history -d $line;done;\ | |||
history -w | history -w | ||
</source> | </source> | ||
Line 27: | Line 33: | ||
type __git_ps1 >/dev/null 2>&1 || { echo >&2 "__git_ps1 not installed";} | type __git_ps1 >/dev/null 2>&1 || { echo >&2 "__git_ps1 not installed";} | ||
# check by using if else conditional | # check by using if else conditional block | ||
if hash __git_ps1 2>/dev/null;then echo '__git_ps1';\ | if hash __git_ps1 2>/dev/null;then echo '__git_ps1';\ | ||
else echo 'no __git_ps1';fi | else echo 'no __git_ps1';fi | ||
</source> | </source> | ||
==Bash Arrays== | |||
<source lang='bash'> | |||
declare -ax HADOOP_SCHEDULERS=(dev prod);\ | |||
declare -Ax MINMAX_CAPACITIES=([dev]='50 50' [prod]='50 70');\ | |||
for HADOOP_SCHEDULER in ${HADOOP_SCHEDULERS[@]};do \ | |||
IFS=' ' read -r -a MINMAX_CAPACITY <<< "${MINMAX_CAPACITIES[${HADOOP_SCHEDULER}]}";\ | |||
echo "${HADOOP_SCHEDULER} » Min » ${MINMAX_CAPACITY[0]} » Max » ${MINMAX_CAPACITY[1]}";\ | |||
done | |||
</source> | |||
==Date Format== | |||
{| | |||
| valign="top" | | |||
<source lang='bash'> | |||
echo "$(date '+%d/%m/%Y %H:%M:%S [%:z %Z]')" | |||
echo "$(date '+%Y-%m-%d-T%Hh%Mm%Ss-MYT')" | |||
echo "$(date '+%Y-%m-%d-T-%H-%M-%S-MYT')" | |||
</source> | |||
| valign="top" | | |||
<source lang='bash'> | |||
echo "$(date '+%Y-%m-%d-%HH%M-%S-MYT')" | |||
echo "$(date '+%Y-%m-%d-%HH%MM%S-MYT')" | |||
echo "$(date '+%Y-%m-%dT%H:%M:%SMYT')" | |||
</source> | |||
| valign="top" | | |||
<source lang='bash'> | |||
echo "$(date '+%Y-%m-%d-T%H%M%S-MYT')" | |||
echo "$(date '+D%Y%m%d-T%H%M%S-MYT')" | |||
echo "$(date '+%Y%m%d-T%H%M%S-MYT')" | |||
</source> | |||
|- | |||
| valign="top" colspan="3" | | |||
---- | |||
|- | |||
| valign="top" | | |||
<source lang='bash'> | |||
echo "$(date '+%Y%m%dT%H%M%SMYT')" | |||
echo "$(date '+%F-%kh%M')" | |||
</source> | |||
| valign="top" | | |||
| valign="top" | | |||
|} | |||
==Lines Match Between== | |||
<source lang='bash' highlight='6,8,12' line> | |||
BASH_REMOTE_SCRIPT_FRMT=$(cat <<'EOF' | |||
# suppress stdout | |||
echo '/* $STDOUT$' | |||
echo "${HOSTNAME}${HOME}" | |||
ssh -qt gtw.vpc.chorke.org <<'EOF_00' | |||
echo "${HOSTNAME}${HOME}" | |||
ssh -qt app.vpc.chorke.org <<'EOF_01' | |||
echo "${HOSTNAME}${HOME}" | |||
echo '$STDOUT$ */' | |||
pg_dump postgres://academia:%[email protected]:5432/%s | |||
# suppress stdout | |||
echo '/* $STDOUT$' | |||
EOF_01 | |||
EOF_00 | |||
echo '$STDOUT$ */' | |||
EOF | |||
) | |||
</source> | |||
---- | |||
<source lang='bash' start='21' highlight='5' line> | |||
BASH_REMOTE_SCRIPT_PASS='sadaqah!' | |||
BASH_REMOTE_SCRIPT_PGDB='academia_keycloak_staging' | |||
printf -v BASH_REMOTE_SCRIPT_EXEC "${BASH_REMOTE_SCRIPT_FRMT}" "${BASH_REMOTE_SCRIPT_PASS}" "${BASH_REMOTE_SCRIPT_PGDB}" | |||
printf -v BASH_EXPORT_SCRIPT_DUMP '%s.sql' "${BASH_REMOTE_SCRIPT_PGDB}" | |||
bash -c "${BASH_REMOTE_SCRIPT_EXEC}" > ${BASH_EXPORT_SCRIPT_DUMP} | |||
</source> | |||
---- | |||
<source lang='bash' start='27' highlight='1-3' line> | |||
sed '/^\/\* \$STDOUT\$$/,/^\$STDOUT\$ \*\/$/{/^\/\* \$STDOUT\$$/!{/^\$STDOUT\$ \*\/$/!d}}' -i ${BASH_EXPORT_SCRIPT_DUMP} | |||
sed -z 's|\n/\* $STDOUT$\n$STDOUT$ \*/||g' -i ${BASH_EXPORT_SCRIPT_DUMP} | |||
sed -z 's|/\* $STDOUT$\n$STDOUT$ \*/\n||g' -i ${BASH_EXPORT_SCRIPT_DUMP} | |||
gzip ${BASH_EXPORT_SCRIPT_DUMP} | |||
</source> | |||
==Package Status== | |||
<source lang='bash'> | |||
TMUX_INSTALL_STATUS="$(dpkg-query -W --showformat='${db:Status-Status}' 'tmux' 2>&1)" | |||
if [[ ! $? -eq 0 ]]||[[ ! "${TMUX_INSTALL_STATUS}" -eq 'installed' ]];then | |||
printf 'tmux \033[0;31mnot found! \033[0m:(\n';exit 1 | |||
fi | |||
POWERLINE_INSTALL_STATUS="$(dpkg-query -W --showformat='${db:Status-Status}' 'powerline' 2>&1)" | |||
if [[ ! $? -eq 0 ]]||[[ ! "${POWERLINE_INSTALL_STATUS}" -eq 'installed' ]];then | |||
printf 'powerline \033[0;31mnot found! \033[0m:(\n';exit 1 | |||
fi | |||
</source> | |||
==Knowledge== | |||
sudo apt install neofetch | |||
neofetch --cpu-brand off | |||
neofetch | |||
# add the new key(s) to known_hosts | |||
ssh-keyscan -H git.academia.chorke.org >> ~/.ssh/known_hosts | |||
# remove the old key(s) from known_hosts | |||
ssh-keygen -R git.academia.chorke.org | |||
cat /etc/os-release | |||
==References== | ==References== | ||
{| | |||
| valign="top" | | |||
* [[Execute bash script from URL]] | * [[Execute bash script from URL]] | ||
* [[Bash/SQLPlus/Input/Redirect|Bash/SQLPlus Input Redirect]] | * [[Bash/SQLPlus/Input/Redirect|Bash/SQLPlus Input Redirect]] | ||
Line 42: | Line 163: | ||
* [[Bash/CPU]] | * [[Bash/CPU]] | ||
* [[Bash/RAM]] | * [[Bash/RAM]] | ||
| valign="top" | | |||
* [https://stackoverflow.com/questions/28830225/ Bash read a properties file contains keys & values] | |||
* [https://stackoverflow.com/questions/39162846/ What does <code>set -e</code> and <code>set -a</code> do in bash] | |||
* [https://stackoverflow.com/questions/918886/ Bash split a string using delimiter] | |||
* [https://stackoverflow.com/questions/5725296/ Difference between sh & bash] | |||
* [https://tldp.org/LDP/abs/html/bashver4.html Bash Arrays, version 4] | |||
* [https://www.hostinger.my/tutorials/bash-array Use Bash Arrays] | |||
* [[PostgreSQL]] | |||
* [[Sendmail]] | |||
* [[JQ Tool]] | |||
| valign="top" | | |||
* [https://stackoverflow.com/questions/6287755/ <code>sed</code> » Delete All Lines between two match] | |||
* [https://stackoverflow.com/questions/1298066/how-can-i-check-if-a-package-is-installed-and-install-it-if-not/54239534#54239534 Check if a package is installed or not] | |||
* [https://askubuntu.com/questions/445469/ Meaning of -x Inside If Condition] | |||
* [https://unix.stackexchange.com/questions/10226/ <code>sed</code> » Multi-line Pattern Match] | |||
* [https://stackoverflow.com/questions/29161323/ Keep Associative Array Order] | |||
* [https://tldp.org/LDP/abs/html/comparison-ops.html Other Comparison Operators] | |||
* [https://tldp.org/LDP/abs/html/fto.html File Test Operators] | |||
* [[TMux]] | |||
|} |
Latest revision as of 08:45, 2 March 2024
sudo systemctl stop firewalld
sudo systemctl stop ipchains
sudo systemctl stop iptables
sudo systemctl restart jira
sudo systemctl restart atlbitbucket
# clear all
history -c
history -w
# delete line
history -d 2013
history -w
for i in {1..50};do \
history -d 2013;done;\
history -w
count=`wc -l ~/.bash_history|awk '{print $1+1}'`;start_line=$(($count-10));\
for ((line=$count;line>=$start_line;--line));do history -d $line;done;\
history -w
command -v __git_ps1 >/dev/null 2>&1 || { echo >&2 "__git_ps1 not installed";}
hash __git_ps1 >/dev/null 2>&1 || { echo >&2 "__git_ps1 not installed";}
type __git_ps1 >/dev/null 2>&1 || { echo >&2 "__git_ps1 not installed";}
# check by using if else conditional block
if hash __git_ps1 2>/dev/null;then echo '__git_ps1';\
else echo 'no __git_ps1';fi
Bash Arrays
declare -ax HADOOP_SCHEDULERS=(dev prod);\
declare -Ax MINMAX_CAPACITIES=([dev]='50 50' [prod]='50 70');\
for HADOOP_SCHEDULER in ${HADOOP_SCHEDULERS[@]};do \
IFS=' ' read -r -a MINMAX_CAPACITY <<< "${MINMAX_CAPACITIES[${HADOOP_SCHEDULER}]}";\
echo "${HADOOP_SCHEDULER} » Min » ${MINMAX_CAPACITY[0]} » Max » ${MINMAX_CAPACITY[1]}";\
done
Date Format
echo "$(date '+%d/%m/%Y %H:%M:%S [%:z %Z]')"
echo "$(date '+%Y-%m-%d-T%Hh%Mm%Ss-MYT')"
echo "$(date '+%Y-%m-%d-T-%H-%M-%S-MYT')"
|
echo "$(date '+%Y-%m-%d-%HH%M-%S-MYT')"
echo "$(date '+%Y-%m-%d-%HH%MM%S-MYT')"
echo "$(date '+%Y-%m-%dT%H:%M:%SMYT')"
|
echo "$(date '+%Y-%m-%d-T%H%M%S-MYT')"
echo "$(date '+D%Y%m%d-T%H%M%S-MYT')"
echo "$(date '+%Y%m%d-T%H%M%S-MYT')"
|
| ||
echo "$(date '+%Y%m%dT%H%M%SMYT')"
echo "$(date '+%F-%kh%M')"
|
Lines Match Between
BASH_REMOTE_SCRIPT_FRMT=$(cat <<'EOF'
# suppress stdout
echo '/* $STDOUT$'
echo "${HOSTNAME}${HOME}"
ssh -qt gtw.vpc.chorke.org <<'EOF_00'
echo "${HOSTNAME}${HOME}"
ssh -qt app.vpc.chorke.org <<'EOF_01'
echo "${HOSTNAME}${HOME}"
echo '$STDOUT$ */'
pg_dump postgres://academia:%[email protected]:5432/%s
# suppress stdout
echo '/* $STDOUT$'
EOF_01
EOF_00
echo '$STDOUT$ */'
EOF
)
BASH_REMOTE_SCRIPT_PASS='sadaqah!'
BASH_REMOTE_SCRIPT_PGDB='academia_keycloak_staging'
printf -v BASH_REMOTE_SCRIPT_EXEC "${BASH_REMOTE_SCRIPT_FRMT}" "${BASH_REMOTE_SCRIPT_PASS}" "${BASH_REMOTE_SCRIPT_PGDB}"
printf -v BASH_EXPORT_SCRIPT_DUMP '%s.sql' "${BASH_REMOTE_SCRIPT_PGDB}"
bash -c "${BASH_REMOTE_SCRIPT_EXEC}" > ${BASH_EXPORT_SCRIPT_DUMP}
sed '/^\/\* \$STDOUT\$$/,/^\$STDOUT\$ \*\/$/{/^\/\* \$STDOUT\$$/!{/^\$STDOUT\$ \*\/$/!d}}' -i ${BASH_EXPORT_SCRIPT_DUMP}
sed -z 's|\n/\* $STDOUT$\n$STDOUT$ \*/||g' -i ${BASH_EXPORT_SCRIPT_DUMP}
sed -z 's|/\* $STDOUT$\n$STDOUT$ \*/\n||g' -i ${BASH_EXPORT_SCRIPT_DUMP}
gzip ${BASH_EXPORT_SCRIPT_DUMP}
Package Status
TMUX_INSTALL_STATUS="$(dpkg-query -W --showformat='${db:Status-Status}' 'tmux' 2>&1)"
if [[ ! $? -eq 0 ]]||[[ ! "${TMUX_INSTALL_STATUS}" -eq 'installed' ]];then
printf 'tmux \033[0;31mnot found! \033[0m:(\n';exit 1
fi
POWERLINE_INSTALL_STATUS="$(dpkg-query -W --showformat='${db:Status-Status}' 'powerline' 2>&1)"
if [[ ! $? -eq 0 ]]||[[ ! "${POWERLINE_INSTALL_STATUS}" -eq 'installed' ]];then
printf 'powerline \033[0;31mnot found! \033[0m:(\n';exit 1
fi
Knowledge
sudo apt install neofetch neofetch --cpu-brand off neofetch
# add the new key(s) to known_hosts ssh-keyscan -H git.academia.chorke.org >> ~/.ssh/known_hosts # remove the old key(s) from known_hosts ssh-keygen -R git.academia.chorke.org
cat /etc/os-release