shell
为注释
!执行脚本的 解释器 为bash
权限问题 remark
调试 bash -x
man test
变量 —>默认必须全局变量 局部变量必须给local
a=0; 弱类型语言 没有明确类型
命令替换符号· pwd
➜ shell_test a=pwd
➜ shell_test echo $a
/root/shell_test
➜ shell_test a=$a:a:
➜ shell_test echo $a
/root/shell_test::
这只是一个字符串拼接
local b=5 局部变量—必须给local
$0:获取当前执⾏shell脚本的⽂件名,包括路径。
$n: 获取当前执⾏脚本的第n个参数,n=1…9,如果n⼤于9,则需要将n使用⼤括号括起
$*:获取当前shell的所有参数,将所有命令⾏参数视为单个字符串,相当于”$1$2$3″;
$#:得到执⾏当前脚本的参数个数;个数 ./1.sh 1 2 3 7
$@:获取这个程序所有参数,并保留参数之间的任何空白,相当于”$1″ “$2” “$3”,这
是将参数传给其他程序的最好办法;
$? 判断是否成功执行 -非0 都是错误的
$$:取当前进程的PID
$!:上一个指令的PI
-
大于 -gt (greater than)
-
小于 -lt (less than)
-
大于或等于 -ge (greater than or equal)
-
小于或等于 -le (less than or equal)
-
不相等 -ne (not equal)
-
相等 -eq (equal)
输入输出 – READ
read [-options] [variable…]
example
->read age
->input 1444
->echo $age
➜ shell_test read name age
name 111
➜ shell_test echo $name $age
name 111
-a array #把输⼊赋值到数组 array 中,从索引号零开始。
-d delimiter
用字符串 delimiter 中的第⼀个字符指示输⼊结束,⽽不是⼀个换⾏符
使用 Readline 来处理输⼊。这使得与命令⾏相同的⽅式编辑输⼊
-n num
读取 num 个输⼊字符,⽽不是整⾏
-p prompt
为输⼊显示提示信息,使用字符串 prompt
example :
➜ shell_test read -p "Please input your and age" name
Raw mode. 不把反斜杠字符解释为转义字符
Silent mode.
➜ shell_test read -p -s "Please input your and age" name --遮掩 类似登录遮掩米面
-t seconds #超时
-u fd
使用⽂件描述符 fd 中的输⼊,⽽不是标准输⼊
centos 6 的环境下 必须强调 参数 使用读入参数必须要有 $符号 (ubuntu 似乎不需要)
1 #!/bin/bash
2 read n
3 if [ $n -gt 0 ];then
4 echo ">0"
5 else
6 echo "<0" 7 fi < code></0">
IF-ELSE
强调一下 if[[]] 必须加; 在 then 后面必须杰 fi
example
#!/bin/bash
2 read n
3 if [[ $n -gt 0 ]];then
4 echo ">0"
5 else
6 echo "<0" 7 fi < code></0">
判断是否为空
1 #!/bin/bash
2 read n
3 if [[ ${n}x == x ]]; then 4 echo "n is null"
5 else
6 echo "n = $n"
7 fi
标准长度判断
#!/bin/bash 2 read n
3 if [[ ! -n ${n} ]]; then
4 echo "n is null"
5 else
6 echo "n = $n"
7 fi
#!/bin/bash
2 read n
3 if [[ $n -gt 0 ]];then
4 echo "$n >12"
5 elif [[ $n -eq 12 ]] ;then 6 echo "$ n<12" 7 8 9 else echo "xxx" fi< code></12">
用于主要用于菜单
function __test__(){
echo "1xxxx"
echo "2xxxx"
echo "3xxxx"
}
__test__
read a
case $a in
1 )
echo "xxx"
;;
2)
echo "yyy"
;;
3 )
echo "xxxxx"
;;
esac
Example
1 #! /bin/bash
2 for each in ls
;do
3 echo ${each}
4 done
类似于foreach 语句
标准循环
9 for (( i = 1 ; i < 10 ; i++ )); do
10 ls ${i}.* 11 done
echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
25 for i in seq 0 100
; do
26 if [[ $[i % 2] -eq 1 ]] ; then
27 echo $i
28 fi
29 done
2 number =0
3 while [[ ${number} -lt 100 ]]; do
4 echo ${number}
5 number =$[${number}+1] 6 let number+=1
7 done
continue
会跳过当前循环中的代码,强迫开始下一次循环。
for (( i = 1 ; i < 10 ; i++ )); do
20 ls ${i}.*
21 if [[ $i -eq 3 ]]; then
22 continue
23 fi
24
25 if [[$i -eq 6 ]]; then
26 continue
27 fi
28 done 23 done
数组Arrary
declare 声明方式
declare a test
输出数组内容 //需要大括号转义
${【【】[*]}
${arry[@]}
确定数组元素个数 总数
${#arry[@]}
找到数组的下标
${!arry[@]}
数组长度
echo ${#test}
数据追加
arry+(abc);
sort 排序
#! /bin/bash
while [[ 1 ]];do
echo "inpuut the number: ";
read n
for (( i=2; i< ${n}; i++));do
flag=1
if [[ $[${n}%${i} ] -eq 0 ]];then
echo " is not prime"
flag=0
break
fi
done
if [[ ! ${flag} -eq 0 ]];then
echo "is prime"
fi
done
变量,参数展开
${parameter:-word} 如果变量未定义,则表达式的值为word。 --如果指令的某个参数出现了异常,通过该指令补充指令
${parameter:=word} 如果变量未定义,则设置变量的值为word,返回表达式的值 也是word。 与之同上
${parameter:?word} 用于捕捉由于变量未定义而导致的错误并退出程序,
rm ${c:?"not found"}/*
zsh: c: "not found"
echo $?
1 返回1
rm ${c:+"not found"}/* 如果变量已经定义,返回word,也就是真。 判断是否存在
提示一下 [!]是查history 记录的id
${!prefix*} 如 abc=13 abd =12 echo ${!ab*} 匹配全部ab开头的变量
example : find ${pate :-/error} -name ".c" -type f | xargs rm -f 报错
example : find ${pate :? error} -name ".c" -type f | xargs rm -f 报错
!号 模糊匹配 !cl a=abc echo ${! ab*}
变量如果为空 :/ 补 出错退出 :?error
保证指令出错的健壮性
${#parameter} 输出字符串的长度 echo ${#a} a=123
${parameter:offset} 从第offset字符开始截取 a=123 echo ${a:1}
${parameter:offset:length} 从offset字符开始截取,取length长度 ➜ a=123 ~ echo ${a:1:1} 2
${parameter#pattern} 从头删除最短匹配 模糊匹配
${parameter##pattern} 从头删除最长匹配 模糊匹配
ü${parameter//pattern/string} 全部匹配被替换
ü${parameter/#pattern/string} 字符串开头的替换
ü${parameter/%pattern/string} 字符串结尾的替换
ü${parameter,,} ${parameter^^} 全部转换为小写、大写
ü${parameter,} ${parameter^} 首字母转换为小写、大写
权限问题 remark
➜ shell_test sudo ./1.sh
sudo: ./1.sh: command not found
升级权限
➜ shell_test chmod +x 1.sh
./1.sh 执行成功
Original: https://www.cnblogs.com/yijieyufu/p/16423192.html
Author: Aquiet
Title: Linux-shell
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/581804/
转载文章受原作者版权保护。转载请注明原作者出处!