首页 技术 正文
技术 2022年11月9日
0 收藏 558 点赞 4,932 浏览 4570 个字

shell字符串、shell数组、shell echo指令、shell test命令、shell if语句、shell case语句、shell for语句、shell while语句、shell break语句、shell 函数第一个Shell脚本

#!/bin/bash
# this is your first shell
echo "hello world"

#!” 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell。

# 表示注释。

echo命令用于向窗口输出文本,相当于c语言中的printf函数。

chmod +x ./filename.sh  #使脚本具有执行权限
./test.sh #执行脚本bash filename.sh 执行脚本

shell字符串

#shell.str.sh
#!/bin/bash
str="my name is gjianw217"
str2="what's you name"
echo $str $str2
echo ${#str}
echo ${str::}
echo `expr index "$str" is`

字符串可以用单引号,也可以用双引号,也可以不用引号。其中:

单引号字符串的限制:

  • 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
  • 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。

双引号的优点:

  • 双引号里可以有变量
  • 双引号里可以出现转义字符

shell数组

#shell-array.sh
#!/bin/basharray=(value1 value2 value3 value4 value4)
echo ${array[]}
echo ${array[@]}
echo ${#array[@]}
echo ${#array[]}
echo ${array[*]}

在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。定义数组的一般形式为:
    数组名=(值1 值2 … 值n)

读取数组元素值的一般格式是:
    ${数组名[下标]}

shell echo指令

#shell-echo.sh#!/bin/bash
name="ok"
echo "$name it is a test"
echo date
echo '$name\"'
echo "it is a test" > myfile
echo -e "ok!\c"
echo "it is a test"
echo -e "ok!\n"
echo "it is a test"echo $'\n'

echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串,也可以使用echo实现更复杂的输出格式控制。如果变量与其它字符相连的话,需要使用大括号({ })。若需要原样输出字符串(不进行转义),则必须使用单引号。

shell test命令

#shell-test.sh#!/bin/bash
num1=
num2=
if test $[num1] -eq $[num2]
then
echo the two number equal
else
echo the two number not equal
fiif test num1=num2
then
echo the two number equal
else
echo the two number no equal
ficd /bin
if test -e ./bash
then
echo 'the file already exits'
else
echo 'the file does not exits'
ficd /bin
if test -e ./notFile -o ./bash
then
echo 'the file exits'
else
echo 'the file not exits'
fiecho "hello world"

Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

参数说明-eq等于则为真-ne不等于则为真-gt大于则为真-ge大于等于则为真-lt小于则为真-le小于等于则为真

参数说明-eq等于则为真-ne不等于则为真-gt大于则为真-ge大于等于则为真-lt小于则为真-le小于等于则为真

数值测试表

参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真

字符串测试表

y

参数 说明
-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真

文件测试表

参数 说明
= 等于则为真
!= 不相等则为真
-z 字符串 字符串长度伪则为真
-n 字符串 字符串长度不伪则为真

shell if语句

#shell-if.sh#!/bin/bash
if test $[+] -eq $[+];then echo 'the two equal';else echo 'the two not equal';finum1=
num2=
if test $num1 -eq $num2
then
echo the number equal
else
echo the number not equal
fi

在sh/bash里,如果else分支没有语句执行,就不要写这个else。末尾的fi是if倒过来拼写。

shell case语句

#shell-case.sh#!/bin/bashecho 'Input a number between 1 to 4'
echo -e "Your number is :\c"read aNumcase $aNum in
) echo 'You select 1'
;;
) echo 'You select 2'
;;
) echo 'You select 3'
;;
) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac

shell case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。

case工作方式如上所示,取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。

取值将检测匹配的每一个模式,一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

shell for语句

#shell-for.sh#!/bin/bash
for loop in
do
echo "The value is :${loop}"
donefor str in "This is a string"
do
echo $str
done

当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。in列表是可选的,如果不用它,for循环使用命令行的位置参数。

shell while语句

#shell-while.sh#!/bin/bash
COUNTER=
while [ $COUNTER -lt ]
do
COUNTER=`expr $COUNTER + `
echo $COUNTER
doneecho 'type <CTRL-D> to terminate'
echo -n 'enter your most liked file:'
while read FILM
do
echo "yeah! great film teh $FILM"
done

while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。

shell break语句

#shell-break.sh#!/bin/bashwhile :
do
echo -n "input a number between 0 to 5:"
read aNum
case $aNum in
) echo "Game over"
break
;;
||||) echo "you select $aNum"
;;
*) echo "you not select the 0-5 nuber"
continue
;;
esac
done

在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,Shell使用两个命令来实现该功能:break和continue。其中:break命令允许跳出所有循环(终止执行后面的所有循环)。continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

shell 函数

#shell-fun.sh#!/bin/bash
demoFun(){
echo "This is your first shell function"
}
echo "Function begin..."
demoFun
echo "Function end"funWithReturn(){
echo "the function is to get the sum of two numbers.."
echo -n "Input first number:"
read aNum
echo -n "Input another number:"
read anotherNum
echo "the two numbers are $aNum and $anotherNum"
return $(($aNum+$anotherNum))
}funWithReturn
echo "the sum of two numbers is $?"funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !"
echo "The string of the parameters is $* !"
}
funWithParam

shell函数的命名可以是函数名或function 函数名,一般省略function的书写。在函数的定义中,简单的可分为三种,无返回值,有返回值和带参数的函数。函数返回值,可以显示增加return语句;如果不加,则将最后一条命令运行结果作为返回值(一般为0,如果执行失败则返回错误代码)。 return后跟数值(0-255)。在获取函数的参数时,一般使用$n,但当参数个数大于10时,需要使用${n}来获取参数

参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$#相同,但是使用时加引号,并在引号中返回每个参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

参考资料:

Linux Shell脚本教程:30分钟玩转Shell脚本编程

SHELL脚本编程的常识

Linux Shell脚本编程基础

4 Advanced Bash-Scripting Guide

5 The Grymoire – Home for Unix Wizards

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295