Shell Tips

Aug 9, 2017

if file exists

if [ -f "$file" ]
then
  echo "$file aleady exists"
else
  echo "$file not found"
fi

if var is empty

if [[ -z "$var" ]]
then
  echo "var is empty"
else
  echo "var is not empty"
fi

if var has been set

if [[ -z ${var+x} ]]
then
  echo "var is empty"
else
  echo "var is not empty"
fi

error handing

#!/bin/bash
set -eu -o pipefail

more details in there, see also docker/cli

generate file with given size

random content

dd if=/dev/random of=file_500k bs=500k count=1

same content

dd if=/dev/zero of=file_500k bs=500k count=1

current dir

echo ${PWD##*/}

remove first line

sed '1d' file.txt

string split

echo "hello.world" | cut -d "." -f1
# hello
echo "hello.world" | cut -d "." -f2
# world
echo "hello.matrix.world" | cut -d "." -f2-
# matrix.world

parallel

jobs=(
  "echo 'a'" \
  "echo 'b'" \
  "echo 'c'" \
)
parallel ::: "${jobs[@]}"

example in docker/cli

remove empty lines

grep . file.txt

replace space to newline

cat file.txt |tr " " "\n"

remove char in string

cat "hello" | tr -d '"'

put file content in one line

foo=$(cat file.txt)
echo $foo

csv viewer

column -s, -t < filename.csv | less -#2 -N -S

send logs to syslog

# Write to /dev/log
logger "hello"

# Write to tcp 514
echo "<133>${0##*/}[$$]: Hello" | nc -w1 127.0.0.1 514

# Write to udp 514
echo "<133>${0##*/}[$$]: Hello" | nc -w1 -u 127.0.0.1 514

Store multiple lines in var

a=""
for line in *.txt
do
    a+="$(cat $line)\n"
done
printf "${a}\n"

Merge multiple xlsx files to csv

random_file=$(ls *.xlsx | head -n 1)
header=$(in2csv $random_file | head -n 1)
tickets=""
for file in *.xlsx
do
    echo "Handling $file"
    tickets+="$(in2csv $file |sed '1d')\n"
done
printf "${header}\n${tickets}" > output.csv

Rename mutilple files

# add prefix- to all files
$ rename 's/^/prefix-/' *

# add suffix .txt to all files
$ rename 's/$/.txt/' *

# remove prefix- in all prefix-* files
$ rename 's/^prefix-//' prefix-*

Remove duplicate lines

$ sort -n file.txt | uniq