Shell

不适合阅读的整理的一些个人常用的 Shell 指令

Posted by gavin on July 23, 2021

随便整理的一些自用的Shell指令

Linux

linux ps (process status): show current process status.

ps -a                                   show all processes
ps -ef | grep (KEYWORD OF PROCESS)      show chosen process
! /bin/bash

#       Output
echo "----------------OUTPUT TEST--------------"
printf "input somthing to print: \n"
read name
echo "$name it is a test"
name='fly'
echo ${name}
test_str="h,"$name"!"
echo $test_str          ###print test_str###
echo ${#test_str}       ###print length of test_str###
echo ${test_str:1:4}    ###print 1-4 position###
echo "\"test123test\""


#       Array
echo "----------------ARRAY TEST ---------------"
array_01=(v1 v2 v3)
array_02[0]=v4
array_02[1]=v5
array_02[2]=v6

# ${array_name[index]}    general format for element in array

array_val_01=${array_02[1]}
echo ${array_val_01}    ###print 2nd element in array_02###
echo ${array_01[@]}     ###print all elements in array_01###

array_length_01=${array_01[@]} ###length of array_01###


#       Passing parameters
echo "-----------------PASSING PARA TEST--------"
echo "passing case";
echo "file name: $0";
echo "first para: $1";
echo "second para: $2";


#       Expression
echo "----------------EXPRESSION TEST-----------"
a=10
b=20
val_expr_sum=`expr $a + $b`
val_expr_minor=`expr $a - $b`
val_expr_mul=`expr $a \* $b`
val_expr_div=`expr $b / $a`
val_expr_mod=`expr $b % $a`
echo "sum is: $val_expr_sum"
echo "minor is: $val_expr_minor"
echo "mul is: $val_expr_mul"
echo "div is: $val_expr_div"
echo "mod is: $val_expr_mod"


#       Test command
echo "---------------TEST TEST----------------"
# -eq   equal then true
# -ne   not equal then true
# -gt   greater then true
# -ge   greater or equal then true
# -lt   littler then true
# -le   littler or equal then true
c=10
d=20
if test $[c] -eq $[d]
then echo 'equal'
else echo 'not equal'
fi


#       Control
# for
echo "--------------CONTROL TEST--------------"
for loop in 1 2 4 5
do
        echo "val is: $loop"
done

# while
int=1
while(($int<=5))
do
        echo $int
        let "int++"
done

echo -n "input smth,press CTRL-D to quit: "
while read while_test_01
do
        echo "$while_test_01 got"
done


# case
echo "input int between 1-4"
read case_test_01
case $case_test_01 in
        1) echo "choose 1";;
        2) echo "choose 2";;
        3) echo "choose 3";;
        4) echo "choose 4";;
        *) echo "input error";;
esac


#       Shell function
echo "------------------FUNCTION TEST-----------------"
demoFun(){
        echo "Two nums sum"
        echo "input first: "
        read func_test_01
        echo "input second: "
        read func_test_02
        echo "num is $func_test_01 and $func_test_02"
        return $(($func_test_01+$func_test_02))
}
demoFun
echo "sum is $? !"