本文共 4277 字,大约阅读时间需要 14 分钟。
#!/bin/bashecho "Change a new page in 1996:Pepsi."echo "The moon is getting round,and things will get better."echo "Date: 2021/4/10 10:45"echo "Address: Baskerball Court"
#!/bin/bash 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell
编辑好后 需要给脚本赋予权限才能执行
#!/bin/bashthings="Change a new page in 1996:Pepsi."sentence="The moon is getting round,and things will get better."Address="Address: Baskerball Court"echo $thingsecho $sentenceecho "${Address} Date: 2021-4-10 10:45"readonly colacola="Yuwei Wang"echo $cola # 只读变量 运行时候会提示 运行的时候 你们看不见 我自己看见就好啦!
${}花括号是分隔符号 可用可不用
readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变 运行会报错:./first.sh:行10: cola:只读变量 unset 可以删除变量 这里我不想用 不想用!
#!/bin/basharray_name=(1 2 3 4 5 6 7)echo " 0: ${array_name[0]}"echo " all: ${array_name[*]}"length=${#array_name[*]}echo "length: $length"array_num=('chenyuhang')lengthn=${#array_num[0]} # 10echo $lengthn
- 数组用空格分隔
- 获取数组长度${array_name[*]}
- 获取数组中单个元素的长度${#array_num[0]}
echo "shell:"echo "filename:$0"; echo "parameter1:$1"; # cc66echo "parameter2:$2"; # basketballecho "parameter3:$3"; # 3timesecho "parameter4:$4"; # cherishecho "parameter5:$5"; # colaecho "parameter numbers:$#";echo "ID: $$";# "@" 等价于 "1" "2" "3"echo "$@";for i in "$@";do echo $i;done# 以一个单字符串显示所有向脚本传递的参数 "*" 等价于 "1 2 3"echo "$*";for i in "$*";do echo $i;done
- bash 不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用
- 乘号(*)前边必须加反斜杠()才能实现乘法运算
- 必须处理好格式 空格!
#!/bin/basha=10b=20val=`expr $a + $b`echo "a + b : $val"val=`expr $a - $b`echo "a - b : $val"val=`expr $a \* $b`echo "a * b : $val"val=`expr $b / $a`echo "b / a : $val"val=`expr $b % $a`echo "b % a : $val"if [ $a == $b ]then echo "a==b"fiif [ $a != $b ]then echo "a!=b"fi
#!/bin/bashfile="./first.sh"if [ -r $file ]then echo "文件可读"else echo "文件不可读"fiif [ -w $file ]then echo "文件可写"else echo "文件不可写"fiif [ -x $file ]then echo "文件可执行"else echo "文件不可执行"fiif [ -f $file ]then echo "文件为普通文件"else echo "文件为特殊文件"fiif [ -d $file ]then echo "文件是个目录"else echo "文件不是个目录"fiif [ -s $file ]then echo "文件不为空"else echo "文件为空"fiif [ -e $file ]then echo "文件存在"else echo "文件不存在"fi
- >是写入文件 重新写入 以前有的内容清空
- >>是追加写入 在文件文末写入
#!/bin/bashecho "shell:"echo "filename:$0"; echo "parameter1:$1" > test.txt; # cc66echo "parameter2:$2" >> test.txt; # basketballecho "parameter3:$3" >> test.txt; # 3timesecho "parameter4:$4" >> test.txt; # cherishecho "parameter5:$5" >> test.txt; # colaecho "It's right to cherish all beautiful thinsg" >> test.txt;echo "date:2021-4-10 22:45" >> test.txt;echo `date` #显示命令执行的结果
#!/bin/bash# ifa=0825b=0617echo "if:"if [ $a != $b ]then echo " I love three things in the world, the sun, the moon and you. The sun for the day. The moon for the night and you forever."fi# 写成一行(适用于终端命令提示符): if [ $a != $b ];then echo " I love you three thousand";fi;# forecho -e "\nfor:";for item in 1 2 3 4 5do echo " The value is:$item "doneecho -e "\n";for str in I love you three thousanddo echo $str;done# while echo -e "\nwhile:";int=1;while($int <= 5 )do echo $int $int = `expr $int + 1`done
#!/bin/bashecho 'exit:';echo -n "Please input your website:";while read websitedo echo "YES! $website is a good website!"done
#!/bin/bashecho "Please input a number from 1 to 4:";read num;case $num in 1) echo "your num is 1" ;; 2) echo "your num is 2" ;; 3) echo "your num is 3" ;; 4) echo "your num is 4" ;;esac
每个模式要用)结束 其间所有命令开始执行直至 ;
#!/bin/bashloveboy(){ echo "Good things come to those who wait.some of us just have to wait longer than others.";echo "We should be patient!";while :do echo -n "Please input a num from 1 to 3:"; read num case $num in 1) echo "如果我爱你,而你也正巧爱我。你头发乱了时候,我会笑笑地替你拨一拨,然后,手还留恋地在你发上多待几秒" ;; 2) echo "如果我爱你,而你不巧地不爱我。你头发乱了,我只会轻轻地告诉你,你头发乱了喔" ;; 3) echo "怎么会有你这种傻子呢,被拒绝,被删还是在默默关注我呢。在我最难过的日子里,又突然出现在我的生活呢?" ;; *) echo "这次想和爱我的人在一起 看夕阳 看日落 看天上繁星点点 去山顶 去海边 去古城。" break ;; esacdone}echo "start function!";loveboy;echo "funciont over!";
!/bin/bashecho '好好努力 好好爱人' > 1.txt;echo `date` >> 1.txt;echo -e '\n';echo `who` >> 1.txt;echo -e '\n';head 1.txt
- 谢谢王雨薇小傻子,预谋良久的出现在我的生活,成为那束光! 不然的话 学习的shell的时间可能又要无限期的推迟了吧!
转载地址:http://zbwmf.baihongyu.com/