echo $SHELL
ifconfig > test.log
ifconfig >> test.log
ifconfiff 2> test.log
ifconfiff 2>> test.log
命令 &>>文件
命令>>正确文件 2>>错误文件
wc -c < test.log
-c:统计字节数
-w:统计单词数
-l:统计行数
顺序执行
同java
同java
命令执行的结果会传递给命令2
ls -l /etc/ | more
将系统变量赋值
gg=$(date)
echo $gg
2020年 4月15日 星期三 16时28分54秒 CST
查看系统所有变量
unset a
环境变量在当前shell和这个shell的所有子shell中生效
export y=6
env
$1:代表第一个参数
$2:代表第二个参数
#!/bin/bash
sum=$((10 + 20))
echo $sum
sum1=$1
sum2=$2
sum3=$((sum1 + sum2))
echo $sum3
./sum.sh 32 48
30
80
#!/bin/bash
# 将输入作为整体,只循环一次
for i in "$*"
do
echo $i
done
# 将输入作为单个,一个一个循环
for j in "$@"
do
echo $j
done
# 打印参数个数
echo $#
./canshu.sh 11 22 33 44 55 66
返回上一个命令执行状态,0为正确,非0为错误
当前进程的进程号
后台运行的最后一个进程的进程号
#!/bin/bash
# -p:提示文本
# -t:停留时间
read -p "xingming:" -t 30 name
echo $name
# -s 用户输入信息不现实在命令行上
read -p "mima:" -s passwd
echo -e "\n"
echo $passwd
# -n 控制用户输入的长度
read -p "xingbie:" -n 1 xingbie
echo -e "\n"
echo $xingbie
echo $((aa + bb))
33
或
echo $(($aa + $bb))
33
[ $i -le 100 ]
#!/bin/bash
test=$(env | grep USER | cut -d "=" -f 2)
if [ "$test" == "xiehanchao" ]
then
echo "is yaoyan"
fi
#!/bin/bash
test=$(env | grep USER | cut -d "=" -f 2)
if [ "$test" == "xiehanchao" ]
then
echo "is yaoyan"
else
echo “no”
fi
if []
then
elif []
then
else
fi
exit 错误码
#!/bin/bash
read -p "请输入y or n:" type
case $type in
"y")
echo "shi yes"
;;
"n")
echo "shi no"
;;
*)
echo "请输入正确的"
esac
#!/bin/bash
cd /Users/xiehanchao/studyshell
ls *.zip > ziplog.log
for fileName in $(cat ziplog.log)
do
echo $fileName
unzip -n $fileName -d ./tt &> log.log
done
#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
s=$(($i+$s))
i=$(($i+1))
done
echo $s
ls | grep -v “需要取反的字符串”
$2代表截取的列数
awk '{printf $2 "\t" $4 "\n"}' student.txt
3s:第三行
02/09:将02替换成09
g:如果有多个替换多个
-n 按数字进行排序,默认是字符串
#!/bin/bash
s=$(ls *.sh | grep -v conutJava.sj)
count=0
for i in $s
do
t=$(cat $i | wc -l)
echo $t
count=$(($count+$t))
done
echo "总数"$count