This post contains few functions, or tips I often use, so I keep them close.
awk:
# match regexp and print
awk '/^path\.logs/{print $2}' elasticsearch.yml
cat:
cat > file.txt << "EOF"
some text
EOF
cat << EOF
Some text
to print
EOF
envvars:
# Source envvars in the same dir as the scriptENVVARS="$(echo "${0%/*}")"/envvars
[[ -f "$ENVVARS" ]] && . "$ENVVARS" || msg fatal "envvars not found"
expect:
#!/bin/bash # copy folder/file to remote servers (list provided from file)
# uses expect to answer yes/no and to input password
cat << EOF
USAGE:
first argument: file/folder (e.g. .ssh) you want to copy onto remote machine
second argument: file with machine list
EOF
read -p "Continue only if you have all neccassary arguments given! " w8
[[ -r $1 ]] || { echo "ERROR: nothing to copy..."; exit 3; }
[[ -r $2 ]] || { echo "ERROR: no machines list given"; exit 4; }
stty -echo
read -p "Enter password: " password; echo
stty echo
for machine in $(cat $2); do
expect -c "
spawn scp -r "$1" $machine:
expect {
"yes/no" {
send "yes\\r"
expect {
"*password:" {
send $password\r
interact
}
}
}
"*password:" {
send $password\r
interact
}
}
"
done
exit 0
find:
# find files in search dir, exept search dir and remove all which were changed more than 30 days ago
find /home/krisko/downloads -maxdepth -mindepth -ctime +30 -exec rm -rf {} \;
#NOTE: use -ok instead of -exec to be prompted
# run different action based on file types
find . '(' -type d -exec chmod o=rx {} ';' ')' -o '(' -type f -exec chmod o=r {} ';' ')'
# remove file containing text 2008
find . -type f -exec grep -q 2008 {} ';' -exec rm {} ';'
motd figlet:
while read host name; do
figlet $host |
ssh $name "echo "cat /home/admin/krisko/.motd" >> /home/admin/mkrissak/.bashrc; cat - >> /home/admin/krisko/.motd";
done <tmpmotd
figlet SOME_MGS | ssh <machine> "echo "cat /home/admin/krisko/.motd" >> /home/admin/mkrissak/.bashrc; cat - >> /home/admin/krisko/.motd"
msg():
# message printing with exit on ERROR/FATAL; SYNTAX: msg SEVERITY "message text"
msg() {
SEV=$(echo -e "$1" | tr [:lower:] [:upper:])
echo "$SEV": "$2"
[[ "$SEV" == "ERROR" || "$SEV" == "FATAL" ]] && exit 1
}
msg error "some error message"
nohup:
nohup my_command > my.log 2>&1
echo $! > save_pid.txt
paste:
# subtract numbers in first two rowshead -2 /tmp/clusterstatt | paste -sd- - | bc
redirecting:
# redirect all (SDTOUT and STDERR)
command &>/dev/null
command &>file
command >& file
readlink:
ENVVARS="$(readlink -e "${0%/*}"/envvars)" || exit 3
. "$ENVVARS"
exec $(readlink -e $0 | sed -e 's/run.sh/XMind/')
readVal():
# read value syntax: readVal "description" "default value"
readVal(){
read -p "Enter $[$2]: " VALUE
[[ -n "$VALUE" ]] || VALUE="$2"
echo $VALUE
}
[[ -n "$PORT" ]] || PORT=$(readVal "listen port" "3306")
sed:
# replace <TAG> in my.cnf with values in variables ($TAG)
for VAR in PORT SOCKET BIND_ADDRESS; do
VALUE=$(eval echo "$"$VAR)
sed -i "s#<$VAR>#$VALUE#" "my.cnf"
done
# apply only on matching line
sed -i '/^#node.name/s/^#//' "$INSTANCE_CONF_DIR/elasticsearch.yml"
# print only matched lines
echo -e "1\n12\n123\n1234" | sed -n '/^12[0-9]\{0,1\}$/p'
# delete Nth line
sed -i '102d' /home/SK-KOSICE/mkrissak/.ssh/known_hosts
select:
# get binaries path
[[ -n "$BINARIES" ]] || {
info "select binaries..."
select BINARIES in $(ls -d /pkg/*mysql/*/); do
[[ -d "$BINARIES" ]] && break || warn "invalid folder"
done
}
stat:
# get folder owner
PACKAGE_OWNER=$(stat -c %U folder)
tail:
# w8 for some pattern in file, then continuefifo=/tmp/tmpfifo.$$
mkfifo "${fifo}" || msg error "I'm unable to wait for LS to start"
tail -n 0 -f "$INSTANCE_LOGS_DIR/logstash.log" >${fifo} &
tailpid=$! # optional
grep -m "started" "${fifo}"
kill "${tailpid}" # optional
rm "${fifo}"
tail -fn0 "$INSTANCE_LOGS_DIR/logstash.log" | grep -m "Sending logstash logs" | xargs echo "" >> "$INSTANCE_LOGS_DIR/logstash.log" \;
w8():
# handle wait time, show some fancy progressbar (syntax w8 <time in seconds>)
w8() {
RESET=$(tput sgr0);
SETCOL=$(tput sgr0);
TIME=$(echo "$1/10" | bc -l);
PERC=10;
HASH='##';
for sec in {1..10}; do
[[ "PERC" -ge 100 ]] && SETCOL=$(tput setaf 2);
let "col=26-${#HASH}";
printf '%s%s%s%*s%s%s\r' "$SETCOL" "$HASH" "$RESET" $col "$PERC""%";
HASH=${HASH}"##";
let "PERC+=10";
sleep $TIME;
done;
echo -e "\n"
}
while
# w8 for status (with timeout)
while $(status &>/dev/null) && [[ "$TOUT" -gt 0 ]]; do
echo -n "."; sleep 1; let $((TOUT-=1))
done
No comments:
Post a Comment