BLACK CAT PROGRAMMER

bash script notes

Get input arguments

test.sh -n=Tom –phone=1234

#!/bin/bash

for i in "$@"
do
case $i in
    -n=*|--name=*)
    NAME="${i#*=}"
    ;;
    -p=*|--phone=*)
    PHONE="${i#*=}"
    ;;
esac
done

echo NAME = $NAME
echo POST = $PHONE

Get positional arguments

Save below script to test.sh and run it as

#!/bin/bash

echo $0 $1 $2

>> test.sh a bb cc

result:
test.sh a bb

Get process id of last executed command

python3 HelloForever.py &

pid=$!
echo $pid

Assign variables

GREETING_WORD="Hello world!"
echo $GREETING_WORD

>> Hello world!

Loop

for (( i=0; i<5; i++)) do
	echo $i
done

echo date

TODAY=`date +%Y%m%d`
echo $TODAY

Changing all files in working directory to lowercase

for i in *; do mv $i `echo $i | tr [:upper:] [:lower:]`; done

Adding prefix to all files in working directory

for a in *.png; do mv "$a" "$1$a"; done

Posted in notesTagged ,