Basic syntax notes for shell script

table of contents

--Variables, special variables --Array --Numerical calculation

Variables, special variables

variable

read -p "Pick 3 colors: " c1 c2 c3
echo $c1
echo $c2
echo $c3

Special variables

echo $0
echo $1
echo $2
echo $#
echo $@
./test a b c
./test
a
b
3
a b c

Array

colors=(blue red green)
echo "${colors[@]}"
echo "${colors[0]}"

colors+=(pink white) #Array addition
echo "${colors[@]}"
blue red green
blue
blue red green pink white

Numerical calculation

# + - * / % ** ++ --
n=1
((n++)) #n=2
((n=n+5)) #n=7
echo $n
((n=n/7)) #n=1
echo $n

if

read -p "name :" name

if [[ $name == "mhori" ]]; then
    echo "welcome"
elif [[ $name =~ ^m ]]; then #When starting from m
    echo "Are you mhori?"
else
    echo "you are not allowed"
fi

Whether there are files or directories

read -p "searching name:" name
if [[ -f $name ]]; then
	echo "file $name exists"
elif [[ -d $name ]]; then
	echo "directory $name exits"
fi

String comparison

# = == != -z(String length is 0) =~(Regular expressions)

read -p "string: " str

if [[ -z $str ]]; then
    echo "string is empty"
elif [[ $str =~ ^m ]]; then
    echo "string starts with m"
else 
    echo $string
fi

for, while

for

colors=(blue red pink)
for color in ${colors[@]}; do
    echo "I love $color"
done
I love blue
I love red
I love pink

while

i=0
while ((i < 10)); do
    ((i++))
    if ((i == 3)); then
        continue
    elif ((i == 6)); then
        break
    fi
    echo $i
done
1
2
4
5

keep reading in an infinite loop

while :
do
    read -p "command : " cmd
    if  [[ $cmd == "quit" ]]; then
        break
    else
        echo $cmd
    fi
done
command : p
p
command : m
m
command : qiot
qiot
command : quit

Read from file

Read line by line and output

## 1 ##
while read line; do
    echo $line
done < colors.txt

## 2 ##
cat colors.txt | while read line
do
	echo $line
done

case & select

case

read -p "Signal color : " color
case "$color" in
	red)
		echo "stop"
		;;
	blue|green)
		echo "go"
		;;
	*)
		echo "wrong signal"
esac

select

select color in red blue green yellow ; do
	case "$color" in
		red)
			echo "stop"
			;;
		blue|green)
			echo "go"
			;;
		yellow)
			echo "caution"
			;;
		*)
			echo "wrong signal"
			break
	esac
done
1) red
2) blue
3) green
4) yellow
#? 1
stop
#? 2
go
#? 3
go
#? 4
caution
#? 0
wrong signal

function

Check exit status

On Unix, 0 is normal termination, 1 is abnormal termination, and it is useful to know that you can check with $?.


hello() {
	if [[ $1 == "mhori" ]]; then
		echo "hello $1"
		return 0
	else
		echo "goodbye $1"
		return 1
	fi
}

hello mhori; echo $?  #hello mhori  0
hello mmm; echo $?   #goodbye mmm  1

Recommended Posts

Basic syntax notes for shell script
Shell script basics # 2
Theano's basic notes
Shell script for batch update of pip3 packages
Easy-to-understand C # notes for pythonist (only basic ones)
FX_tool for Hython Basic02
Shell script command replacement
Shell script special variables
Shell script @ study memo
FX_tool for Hython Basic01
Installation notes for TensorFlow for Windows
Python Tkinter notes (for myself)
Let's try a shell script
Basic Python grammar for beginners
Basic commands for file operations
Notes for HHKB Programming Contest 2020
Tips for data analysis ・ Notes
Atom: Note for Indentation Error when copying Python script to shell
Shell script for people who have trouble with mkdir + touch __init__.py