Skip to content

Instantly share code, notes, and snippets.

@maxcnunes
Last active August 29, 2016 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcnunes/6ea7637b68843a25fe63 to your computer and use it in GitHub Desktop.
Save maxcnunes/6ea7637b68843a25fe63 to your computer and use it in GitHub Desktop.
List of handful bash commands
#!/bin/bash
# signals bash to stop execution on any fail
set -e
# if variable not defined
if [[ -z "$BACKUP_FILE" ]]; then
...
fi
# if variable defined
if [[ -n "$BACKUP_FILE" ]]; then
...
fi
# increment number into a variable
DAYS_TO_KEEP=$(($DAYS_TO_KEEP + 1))
# split whitespaces into break lines
echo "1 2 3 4 5" | tr " " "\n"
echo "1 2 3 4 5" | tr '[[:space:]]' '\n'
# get item from xargs
ls . | xargs -I {} echo File {}
# get result after row 10
ls | awk "NR>10"
# count words
cat myfile | wc -w
# count rows
cat myfile | wc -l
# for loop
for FILE in $FILES; do
echo ">FILE: $FILE"
done
# set variable with multiline text
# To understand the "| true" below:
# https://stackoverflow.com/questions/15429330/how-to-specify-a-multi-line-shell-variable/15429426#comment21828145_15429426
read -d '' SCRIPT <<EOF || true
ls
pwd
echo "Teste"
EOF
# convert break lines in ";"
SCRIPT=$(echo "$SCRIPT" | tr '\n' ';')
# read a variable from host machine when executing a lftp command in remote server
lftp \
-u "user,pwd" \
-e "set ftp:ssl-allow no; cd /data/; rm $(echo $FILES); quit" \
"my-remote-server"
# find content in files ignoring path
grep --exclude-dir="<ignore-path>" '<term>' -R .
# loop find results
for file in $(find "<path>"); do
echo file
done
# check type exist
type_exists() {
if [ $(type -P $1) ]; then
return 0
fi
return 1
}
if ! ( type_exists "gnome-shell" ) ; then
echo "Does not exist"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment