Skip to main content

Bash scripting

Redirect Streams

#!/usr/bin/env python3
data = input("This will come from STDIN: ")
print("Now we write to STDOUT: " + data)
raise ValueError("Now generate an error to STDERR")
./streams_err.py < new_file.txt 2> error_file.txt

Get word frequency:

cat spider.txt | tr ' ' '\n' | sort | uniq -c | sort -nr | head
#!/usr/bin/env python3
import sys
for line in sys.stdin:
print(line.strip().capitalize())
cat file.txt | capitalize.py

If Statements

if grep "127.0.0.1" /etc/hosts; then
echo "Everythin ok"
else
echo "ERROR! 127.0.0.1 is not in /etc/hosts"
fi

check if variable is empty

if test -n "$PATH"; then echo "Your path is not empty"; fi

or

if [ -n "$PATH" ]; then echo "Your path is not empty"; fi

While Loops

#!/bin/bash

n=1
while [ $n -le 5 ]; do
echo "Iteration number $n"
((n+=1))
done

Retry to execute a command

#!/bin/bash
n=0
command=$1
while ! $command && [ $n -le 5 ]; do
sleep $n
((n=n+1))
echo "Retry #$n"
done;
retry.sh some_program.py

For Loops

for fruit in peach orange apple; do
echo "I like $fruit!"
done

Rename files

for file in *.HTM; do
name=$(basename "$file" .HTM)
echo mv "$file" "$name.html"
done

Advanced Command Interaction

tail /var/log/syslog | cut -d' ' -f5-

Source