Skip to content

Instantly share code, notes, and snippets.

@quachngocxuan
Created December 16, 2017 17:07
Show Gist options
  • Save quachngocxuan/0e25629705e0a5dfb192d258261acf99 to your computer and use it in GitHub Desktop.
Save quachngocxuan/0e25629705e0a5dfb192d258261acf99 to your computer and use it in GitHub Desktop.
Learn enough to be dangerous - Bash

Learn enough to be dangerous - Bash

echo

echo "HELLO"

input

read name
echo "Welcome $name"

expression evaluation

read x
read y
echo $(($x+$y))

bc command to evaluate expression

read exp
printf "%.3f" $(echo $exp | bc -l)

if-else

read X
read Y
if (($X > $Y))
then
    echo "X is greater than Y"
elif (($X < $Y))
then
    echo "X is less than Y"
else
    echo "X is equal to Y"
fi
read a
read b
read c

if [[ $a -eq $b && $b -eq $c ]]; then
    echo "EQUILATERAL";
elif [[ $a -eq $b || $a -eq $c || $b -eq $c ]]; then
    echo "ISOSCELES";
else
    echo "SCALENE";
fi

Loop

#!/bin/bash
for i in {1..99};
do
    if (($i % 2 == 1))
    then
        echo $i
    fi
done
read n
sum=0
for (( i=1 ; i<=$n ; i++ ))
do
  read a
  let "sum+=$a"
done
printf "%.3f" $(echo $sum/$n | bc -l)

Grep

Return whole line if pattern is matched

read char; echo -e "YES\nNO\n" | grep -i $char
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment