sudo with echo redirect:
sudo bash -c "echo hi > "
AWK IS FUN
awk -F"DIVIDER" '{print $NF}'
$NF returns the last colunm
$(NF-2) returns the last column minus 2
BASH TIP
echo -WHATGOESHERE is needed when using escape characters in echo, to make newlines with \n or the computer scream with \a or whatever
assign values to variables, depending on if there's already a value set
ASSIGN IF UNSET:
echo $var; echo ${var:=dogs}; echo $var
ASSIGN TEMPORARILY IF UNSET:
same but with :- instead of :=
ASSIGN IF SET:
same, with :+
that works if var is unset or set to null
to work ONLY if unset, remove the :
get random number btween 1 and 76:
echo $(( $RANDOM% 76 + 1 ))
works bcuz of modulus or smth
if [[ condition ]] && [[ condition ]]; then # AND syntax
if [[ condition ]] || [[ condition ]]; then # OR syntax
if -n [[ condition ]] # true if condition is not empty
if -z [[ condition ]] # true if condition is empty
scroll terminal:
[Shift]+[Page (Up|Down)]
tee -a # will append to file instead of overwriting
GET MULTILINE VAR OUTPUT
var=$(command with multiline output)
cat <<< "$var"
yes the quotes are necessary
this works wonderfully:
while read MULTILINE_VAR; do
grep "stuff"
done <<< "$(command with multiline output)"
var=$(ls)
cat <<< "$var"
double quotes preserve newlines!
DO MATH IN LINUX
#!/bin/bash
yum install -y bc
n=19
x=8
p=$(echo "14/$n" | bc -l)
q=$(echo "5/$n" | bc -l)
factn=$(seq -s '*' 1 $n | bc)
factx=$(seq -s '*' 1 $x | bc)
nminx=$(echo "$n-$x" | bc)
factnminx=$(seq -s '*' 1 $nminx | bc)
echo "$factn * ($p^$x) * ($q^($n-$x)) / ($factnminx * $factx)" | bc -l
PASSWORD GENERATION FOR KICKSTARTS
install dovecot, is a mail server with a good password generator
dovecot pw -s SHA512-CRYPT -p PASSWORD
ignore stuff in the {}, the rest of it is the good stuff
yes you need the weird dollar signs and the stuff in between them, that's the salt
can also use it to test if a hash matches a password with -t HASH
START READING FILE FROM DIFFERENT PLACE (EXAMPLE: START AT LINE 2)
tail -n +2
HOW TO NAME VARS BASED ON THE CONTENT OF OTHER VARS
either of these ways work, according to your needs:
a=cats
b=dogs
setvars() {
declare -g ${a}${b}_newvar=birds # -g means global; will make vars work outside of function they're set in
}
runfunc() {
testvar=${a}${b}_newvar # squiggly brackets are because bash gets confused about where the var is supposed to end, and the underscore is not on its list of end-this-here markers
if [[ ${!testvar} == "birds" ]]; then
echo yee
fi
}
setvars
runfunc
echo '$a $b ${a}${b}_newvar $testvar ${!testvar}'
echo "$a $b ${a}${b}_newvar $testvar ${!testvar}"
> yee
OLD OUTDATED ATTEMPTS, GET RID OF THESE LATER
declare $a$b=birds
echo $catsdogs
> birds
a=cats
b=dogs
declare $a$b=birds
lizards=$a$b
echo ${!lizards}
> birds
ARRAYS ARE LIKE THIS
array=$(command with space-deliniated output)
for i in ${array[@]}; do
echo $i
done
DANGER!! this happens:
unset var
if $var; then echo yee; fi
> yee
basically, unset vars act like true
to get around this, set them to false at beginning of script
READ CONTENT INTO VAR, LINE BY LINE
and
KEEP READ PAUSE COMMAND FROM EATING HERESTRINGS
while read -u 10 var; do
echo "$var"
read pause
done 10<<< "(command with multiline output)"
the 10 is arbitray, just use a higher number than any of the terminals you have
good way to write some quick notes to a file:
cat >> /path/to/file << EOF
stuff here
EOF
good way to log stuff:
echo hi > log 2>&1
while sleep #; do
stuff
done
while getopts ":a:b:c:" flag; do
case $flag in
a) var1="$OPTARG" ;;
b) var1="$OPTARG" ;;
c) var1="$OPTARG" ;;
esac done
confirm response
if [[ "$confirm" =~ ^([yY][eE][sS]|[yY])+$ ]]
then else fi
do a thing only when zero options present
while [ $# -eq 0 ] do done
while [ ! $# -eq 0 ] do done #for NOT
bash variable with default setting
default = "defval"
testvar = ${testvar : -defval} #both "testvar"s on this line dont have to be the same thing!! just easy to demo this way
clean spaces from input
var = ${ var// / }
clean slashes
var = $(tr -d '/' <<< "$var")
lowercaseify
var = ${ var,, }
.bash-profile executed when login shell logs you in
.bashrc executed when user logs in
/etc/profile executed for all users uopon login
/proc/uptime
uptime = $(cut -f1 -d' ' /proc/uptime)
uptime = $(printf "%.0f" $uptime )
at TIME <<< "COMMAND"
if [ "$var" = "STRING" ] ; then fi
stomp the atq
atrm $(atq | cut -f1)
temp files go in /var/temp if persistence needed