shell学习:
https://www.runoob.com/linux/linux-shell.html
shell练习:
https://github.com/fengyuhetao/shell
windows下面使用awk、sed、grep、gnuplot
http://blog.sciencenet.cn/home.php?mod=space&uid=858128&do=blog&quickforward=1&id=994394
awk学习:
https://www.runoob.com/linux/linux-comm-awk.html
linux系统变量:
echo "PWD:"$PWD #当前用户的工作目录
echo "path:"$PATH #搜索执行码的路径
echo "Logname:"$LOGNAME #用户的登录名
echo "SHELL:"$SHELL #当前所使用的shell
echo "HOME:"$HOME #用户主目录
myUrl="https://www.runoob.com"
echo "test:$myUrl"
ech ${#myUrl} #输出字符串长度
echo "输出子串:${myUrl:1:4}" # 输出子字符串
echo `expr index "$myUrl" io` #查找子字符串i或o位置
array_name=(value0 value1 value2 value3)
echo ${array_name[@]} #使用 @ 符号可以获取数组中的所有元素
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg
#%-10s 指一个宽度为10个字符(-表示左对齐,没有则表示右对齐)
#printf转义参考:https://www.runoob.com/linux/linux-shell-printf.html
a=10
b=20
if [ $a == $b ]
then
echo "a 等于 b"
elif [ $a -gt $b ]
then
echo "a 大于 b"
elif [ $a -lt $b ]
then
echo "a 小于 b"
else
echo "没有符合的条件"
fi
#关系运算 -eq等于,-ne不等于,-gt 大于,-lt 小于,-ge大于等于,-le 小于等于
for str in 'This is a string'
do
echo "测试:$str";
done
str='root'
if [ ${str} == 'root' ]; then
echo "${str}字符串与root相等"
fi
unset myUrl #删除变量
echo $myUrl
var1=10
var2=20
var3=`expr $var2 / $var1`
echo "The result is $var3"
function factorial {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 -1 ]
local result=`factorial $temp`
echo $[ $result * $1 ]
fi
}
read -p "Please input a value: " value
result=`factorial $value`
echo "The factorial of $value is: $result"
#累加计算******************
function addarray {
local sum=0
local newarray
newarray=(`echo "$@"`)
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo "The original array is : ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=`addarray $arg1`
echo "The result is $result"
#全局变量
function func1 {
temp=$[ $value + 5 ]
result=$[ $temp * 2 ]
}
temlp=4
value=6
func1
echo "The result is $result"
if [ $temp -gt $value ]
then
echo "Temp is larger"
else
echo "temp is smaller"
fi
#返回数组***************
function arraydblr {
local origarry
local newarray
local elements
local i
origarry=(`echo "$@"`)
newarray=(`echo "$@"`)
elements=$[ $# - 1 ]
for (( i=0; i<=$elements; i++ ))
{
newarray[$i]=$[ ${origarry[$i]} * 2 ]
}
echo ${newarray[*]}
}
myarray=(1 2 3 4 5)
echo "The original array is : ${myarray[*]}"
arg1=`echo ${myarray[*]}`
result=(`arraydblr $arg1`)
echo "The new array is : ${result[*]}"
#函数传递数组*************
function testit {
echo "The parameters are : $@"
#函数只会读取数组变量的第一个值
thisarray=$1
echo "The received array is ${thisarray[*]}"
local newarray
newarray=(`echo "$@"`)
echo "The new array value is : ${newarray[*]}"
}
myarray=(1 2 3 4 5)
echo "The original array is : ${myarray[*]}"
#将数组变量当成一个函数参数,函数只会去函数变量第一个值
#testit $myarray
testit ${myarray[*]}
#函数中使用参数**********
function addem {
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
echo -n "Adding 10 and 15:"
value=`addem 10 15`
echo $value
echo -n "Let's try adding just one number: "
value=`addem 10`
echo $value
echo -n "Now trying adding no number: "
value=`addem`
echo $value
echo -n "Finally, try adding three or more numbers: "
value=`addem 10 15 20`
echo $value
#默认推出码*******************
# testing the exit status of a function
func1() {
echo "Trying to display a non-existent file"
ls -l badfile
}
#由于最后一条命令未执行成功,返回的状态码非0
echo "testing the function"
func1
echo "The exit status is : $?"
func2() {
ls -l badfile
echo "Another test to display a non-existent file"
}
#由于最后一条命令echo执行成功,返回的状态码为0
echo "Another test"
func2
echo "The exit status is : $?"
#使用msgbox部件 **********
dialog --title text --msgbox "This is a test" 10 20
#菜单************
function menu {
clear
echo
echo -e "\t\tSys Admin Menu\n"
echo -e "\t1. Display disk space"
echo -e "\t2. Display logged on users"
echo -e "\t3. Display memory usage"
echo -e "\t0. Exit program\n\n"
echo -en "\t\tEnter option:"
read -n 1 option
}
function diskspace {
clear
df -k
}
function whoseon {
clear
who
}
function menusage {
clear
cat /proc/meminfo
}
while [ 1 ]
do
menu
case $option in
0)
break;;
1)
diskspace;;
2)
whoseon;;
3)
menusage;;
*)
clear
echo "Sorry, wrong selection";;
esac
echo -en "\n\n\t\tHit any key to continue"
read -n 1 line
done
clear
#选择菜单***********
function diskspace {
clear
df -k
}
function whoseon {
clear
who
}
function menusage {
clear
cat /proc/meminfo
}
PS3="Enter option:"
select option in "Display disk space" "Display logged on users" "Display memory usage" "Exit program"
do
case $option in
"Exit program")
break;;
"Display disk space")
diskspace;;
"Display logged on users")
whoseon;;
"Display memory usage")
menusage;;
*)
clear
echo "Sorry, wrong selection";;
esac
done
clear