你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

Shell | Hello World

2021/12/30 16:13:45

文章目录

    • 输出语句
    • 变量
    • 数组

输出语句

使用 chmod 命令给文件hello.sh 添加可执行权限 x

[root@VM-0-9-centos data]# echo "hello world"
hello world
[root@VM-0-9-centos data]# vi hello.sh
[root@VM-0-9-centos data]# ls
C_language  hello.js  hello.sh  server.js
[root@VM-0-9-centos data]# ./hello.sh
-bash: ./hello.sh: 权限不够
[root@VM-0-9-centos data]# ll
总用量 16
drwxr-xr-x 2 root root 4096 1230 14:18 C_language
-rw-r--r-- 1 root root   29 1230 14:38 hello.js
-rw-r--r-- 1 root root   19 1230 15:30 hello.sh
-rw-r--r-- 1 root root  410 1230 14:42 server.js
# 使用 chmod 给文件hello.sh 添加可执行权限 x 
[root@VM-0-9-centos data]# chmod +x ./hello.sh
[root@VM-0-9-centos data]# ll
总用量 16
drwxr-xr-x 2 root root 4096 1230 14:18 C_language
-rw-r--r-- 1 root root   29 1230 14:38 hello.js
-rwxr-xr-x 1 root root   19 1230 15:30 hello.sh
-rw-r--r-- 1 root root  410 1230 14:42 server.js
[root@VM-0-9-centos data]# ./hello.sh
hello world

变量

[root@VM-0-9-centos data]# vi var.sh
[root@VM-0-9-centos data]# ll
总用量 20
drwxr-xr-x 2 root root 4096 1230 14:18 C_language
-rw-r--r-- 1 root root   29 1230 14:38 hello.js
-rwxr-xr-x 1 root root   19 1230 15:30 hello.sh
-rw-r--r-- 1 root root  410 1230 14:42 server.js
-rw-r--r-- 1 root root   33 1230 15:37 var.sh
[root@VM-0-9-centos data]# chmod +x ./var.sh 
[root@VM-0-9-centos data]# ./var.sh 
Hello World!!!
[root@VM-0-9-centos data]# cat var.sh 
var="Hello World!!!"
echo ${var}
[root@VM-0-9-centos data]# 

数组

[root@VM-0-9-centos data]# ./arr.sh 
hello
world
shell
---------
hello world shell
3
5
[root@VM-0-9-centos data]# cat arr.sh 
# 定义数组,以空格间隔
arr=("hello" "world" "shell")
echo ${arr[0]}
echo ${arr[1]}
# 获取数组某个特定元素
echo ${arr[2]}
echo "---------"
# 获取数组全部元素
echo ${arr[@]}
# 获取数组长度
echo ${#arr[@]}
# 获取数组单个元素长度
echo ${#arr[0]}