2017년 8월 8일 화요일

Bash Shell Sample


  • Read File

while read line; do
    echo "${line}"
done < test.txt




  • Make list by separating character

for line in `echo "12;34" | tr ";" " "`
do
    echo "${line}"
done

12
34



  • Compare string type

# = : equal
if [ "$a" = "$b" ]; then
    echo "A is equal B"
else
    echo "A is not equal B"
fi

# == : equal
if [ "$a" == "$b" ]; then
    echo "A is equal B"
else
    echo "A is not equal B"
fi

# != : not equal
if [ "$a" != "$b" ]; then
    echo "A is not equal B"
else
    echo "A is equal B"
fi

# < : less
if [[ "$a" < "$b" ]]; then
    echo "A is less than B"
else
    echo "A is greater than B"
fi

if [ "$a" \< "$b" ]; then
    echo "A is less than B"
else
    echo "A is greater than B"
fi

# > : greater
if [[ "$a" > "$b" ]]; then
    echo "A is greater than B"
else
    echo "A is less than B"
fi

if [ "$a" \> "$b" ]; then
    echo "A is greater than B"
else
    echo "A is less than B"
fi


# -z : null
if [ -z "$a" ]; then
    echo "A is null"
else
    echo "A is not null"
fi

# -n : not null
if [ -z "$a" ]; then
    echo "A is not null"
else
    echo "A is null"
fi



  • Compare integer type

# -eq : equal
if [ "$a" -eq "$b" ]; then
    echo "A is equal B"
else
    echo "A is not equal B"
fi

# -ne : not equal
if [ "$a" -ne "$b" ]; then
    echo "A is not equal B"
else
    echo "A is equal B"
fi

# -gt : greater
if ["$a" -gt "$b" ]; then
    echo "A is greater than B"
else
    echo "A is less than B"
fi

# -ge : greater equal
if [ "$a" -ge "$b" ]; then
    echo "A is greater equal than B"
else
    echo "A is less than B"
fi

# -lt : less
if [ "$a" -lt "$b" ]; then
    echo "A is less than B"
else
    echo "A is greater than B"
fi

# -le : less equal
if [ "$a" -le "$b" ]; then
    echo "A is less equal than B"
else
    echo "A is greater than B"
fi

# > : greater
if (( "$a" > "$b" )); then
    echo "A is greater than B"
else
    echo "A is less than B"
fi

# >= : greater equal
if (( "$a" >= "$b" )); then
    echo "A is greater equal than B"
else
    echo "A is less than B"
fi

# < : less
if (( "$a" < "$b" )); then
    echo "A is less than B"
else
    echo "A is greater than B"
fi

# <= : less equal
if (( "$a" <= "$b" )); then
    echo "A is less equal than B"
else
    echo "A is greater than B"
fi



  • Compare float type

$> echo "76.0 < 9.5" | bc
0

$> echo "76.0 > 9.5" | bc
1

$> echo "76.0 >= 91.5" | bc
0

$> echo "76.0 <= 91.5" | bc
1

$> echo "76 > 91.5" | bc
0

$> echo "76 < 91.5" | bc
1

$> echo "76 > 91" | bc
0

$> echo "76 < 91" | bc
1

$> echo "76.0 > 91" | bc
0

$> echo "76.0 < 91" | bc
1


if [[ `echo "76.0 > 9.5" | bc` == 1 ]]; then
    echo "76.0 is greater than 9.5"
else
    echo "76.0 is less than 9.5"
fi

if [[ `echo "76.0 < 91.5" | bc` == 1 ]]; then
    echo "76.0 is less than 91.5"
else
    echo "76.0 is greater than 91.5"
fi



  • Find string in any file

$> grep -r "abcabc" ./*
./views/Picture/Picture.js:             dispatch(abcabcPath(path));



0 개의 댓글:

댓글 쓰기