美文网首页
helloworld入门

helloworld入门

作者: 城堡下的晚祷 | 来源:发表于2019-03-25 22:55 被阅读0次

1.脚本格式

#!/bin/bash开头(指定解析器bash,此处#非注释)

2.hello world案例

(1)创建helloworld脚本

[root@localhost shelldata]# touch helloworld.sh
[root@localhost shelldata]# ll
总用量 0
-rw-r--r--. 1 root root 0 3月  26 06:11 helloworld.sh

(2)编辑helloworld脚本

 vim helloworld.sh

helloworld.sh内容:

#!/bin/bash
echo "hello world"

(3)执行helloworld脚本

[root@localhost shelldata]# sh helloworld.sh 
hello world
[root@localhost shelldata]# bash helloworld.sh 
hello world

注意,当以上面运行方式执行脚本,本质是bash解析器帮助执行,当以下面方式运行时本质是脚本自己执行

[root@localhost shelldata]# ./helloworld.sh
-bash: ./helloworld.sh: 权限不够

需要为其增加运行权限:

[root@localhost shelldata]# chmod u+x helloworld.sh 
[root@localhost shelldata]# ./helloworld.sh
hello world

3.又一个案例

编写脚本test.shell实现以下功能:
在shelldata目录下创建myshell.txt,并在其中写入“I love shell”

[root@localhost shelldata]# touch test.sh
[root@localhost shelldata]# chmod 777 test.sh 
[root@localhost shelldata]# ll
总用量 8
-rwxr--r--. 1 root root 31 3月  26 06:19 helloworld.sh
-rwxrwxrwx. 1 root root 55 3月  26 06:38 test.sh

[root@localhost shelldata]# vim test.sh

test.sh内容为:

#!/bin/bash
cd /home/yzl/shelldata
touch myshell.txt
echo "I love shell">>myshell.txt

执行脚本:

[root@localhost shelldata]# ./test.sh 
[root@localhost shelldata]# ll
总用量 8
-rwxr--r--. 1 root root 31 3月  26 06:19 helloworld.sh
-rw-r--r--. 1 root root  0 3月  26 06:40 myshell.txt
-rwxrwxrwx. 1 root root 73 3月  26 06:40 test.sh

[root@localhost shelldata]# vim myshell.txt 

此时myshell.txt内容为:

I love shell

相关文章

网友评论

      本文标题:helloworld入门

      本文链接:https://www.haomeiwen.com/subject/rwsivqtx.html