shell脚本基础语法

######1.使用vim编辑器创建一个test.sh shell脚本

[webuser@xx-xx-xx-xx ~]$ vim test.sh 

######2.在脚本第一行中输入如下脚本

#!/bin/bash
echo "Hello World !"

说明:#!是一个约定标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell;
######3.给脚本test.sh授予可执行权限

chmod +x ./test.sh

######4.执行脚本

[webuser@xx-xx-xx-xx ~]$ ./test.sh 
hello world

#####5.echo命令用于字符串输出

echo 'hello world'  ##单引号
echo "hello world"  ##双引号
echo hello world    ##省略双引号 
echo "\"hello world\"" ##转义符输出

输出结果是:

[webuser@xx-xx-xx-xx ~]$ ./test.sh 
hello world
hello world
hello world
"hello world"

######6.read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量

#!/bin/bash
read name
echo "$name is a good student"

输出结果是:

[webuser@xx-xx-xx-xx ~]$ ./test.sh 
lili
lili is a good student

######7.将打印结果输出到文件中

#!/bin/bash
readname
echo "$name is a good status" > myfile

######8.获取字符串的长度

usernam="lili"
echo ${#usernam} #输出4

######9.查看目录下的所有文件

for file in $(ls *)
do
echo $file
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值