Difference between revisions of "Bash"
From Bobs Projects
Bob Edwards (Talk | contribs) |
Bob Edwards (Talk | contribs) (→Tips) |
||
(2 intermediate revisions by one user not shown) | |||
Line 9: | Line 9: | ||
% echo ${test_string:6:5} | % echo ${test_string:6:5} | ||
+ | === Parameter Expansion === | ||
+ | * ${VAR#pattern} # delete shortest match of pat from the beginning | ||
+ | * ${VAR#pattern} # delete shortest match of pat from the beginning | ||
=== Splitting String === | === Splitting String === | ||
(from [http://stackoverflow.com/a/918931 best answer on stackoverflow]) | (from [http://stackoverflow.com/a/918931 best answer on stackoverflow]) | ||
Line 42: | Line 45: | ||
==== option 3 ==== | ==== option 3 ==== | ||
find . -type f -not \( -name '*zip' -or -name '*iso' \) -delete | find . -type f -not \( -name '*zip' -or -name '*iso' \) -delete | ||
+ | |||
+ | === Reading dash files === | ||
+ | (from [https://unix.stackexchange.com/questions/189251/how-to-read-dash-files How to Read Dash Files] on Unix.Stackexchange) | ||
+ | cat -- -file | ||
+ | or | ||
+ | cat < -file | ||
+ | |||
+ | == Programming tips == | ||
+ | === raw URL encoding === | ||
+ | from [https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command How to urlencode data for curl command?] | ||
+ | <pre> | ||
+ | rawurlencode() { | ||
+ | local string="${1}" | ||
+ | local strlen=${#string} | ||
+ | local encoded="" | ||
+ | local pos c o | ||
+ | |||
+ | for (( pos=0 ; pos<strlen ; pos++ )); do | ||
+ | c=${string:$pos:1} | ||
+ | case "$c" in | ||
+ | [-_.~a-zA-Z0-9] ) o="${c}" ;; | ||
+ | * ) printf -v o '%%%02x' "'$c" | ||
+ | esac | ||
+ | encoded+="${o}" | ||
+ | done | ||
+ | echo "${encoded}" # You can either set a return variable (FASTER) | ||
+ | REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p | ||
+ | } | ||
+ | </pre> | ||
== ShellShock (CVE-2014-7186 and CVE-2014-7187) == | == ShellShock (CVE-2014-7186 and CVE-2014-7187) == |
Latest revision as of 15:32, 28 October 2020
Bourne-Again SHell (bash) is a Unix/Linux shell.
Contents |
Tips
Substrings
(from Bash Guide for Beginners)
% export test_string="Hello World" % echo ${test_string:6:5}
Parameter Expansion
- ${VAR#pattern} # delete shortest match of pat from the beginning
- ${VAR#pattern} # delete shortest match of pat from the beginning
Splitting String
(from best answer on stackoverflow)
read -ra ADDR <<< "$IN"
can be modified, thusly:
IFS=';' read -ra ADDR <<< "$IN"
Testing Exit Value
if [ $? -eq 0 ] ; then good ; else bad ; fi
Redirection
To redirect stderr and stdout to a file:
prog &> file
Remove all but some files
(from Linux: Bash Delete All Files In Directory Except Few)
option 1
shopt -s extglob rm !(*.zip|*.iso) shopt -u extglob
option 2
(see GLOBIGNORE in bash(1))
GLOBIGNORE=*.zip:*.iso rm -v * unset GLOBIGNORE
option 3
find . -type f -not \( -name '*zip' -or -name '*iso' \) -delete
Reading dash files
(from How to Read Dash Files on Unix.Stackexchange)
cat -- -file
or
cat < -file
Programming tips
raw URL encoding
from How to urlencode data for curl command?
rawurlencode() { local string="${1}" local strlen=${#string} local encoded="" local pos c o for (( pos=0 ; pos<strlen ; pos++ )); do c=${string:$pos:1} case "$c" in [-_.~a-zA-Z0-9] ) o="${c}" ;; * ) printf -v o '%%%02x' "'$c" esac encoded+="${o}" done echo "${encoded}" # You can either set a return variable (FASTER) REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p }
ShellShock (CVE-2014-7186 and CVE-2014-7187)
- Shellshock (software bug) at wikipedia
- Collection of Proof of Concepts and Potential Targets for #ShellShocker at github
- What does SELinux do to contain the the bash exploit?
- CVE-2014-6271: remote code execution through bash from a mailing list
- Environment variable of a name which is often used
External links
- Bash Guide for Beginners at www.tdlp.org
- Shell Variables you should know (including $* and $@)
- Bash at Wikipedia
- What is the difference between ${var}, “$var”, and “${var}” in the Bash shell? from stackoverflow