前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >快速学习-Shell脚本入门

快速学习-Shell脚本入门

作者头像
cwl_java
发布2020-03-25 17:18:55
4470
发布2020-03-25 17:18:55
举报
文章被收录于专栏:cwl_Javacwl_Javacwl_Java

第3章 Shell脚本入门

1.脚本格式 脚本以#!/bin/bash开头(指定解析器) 2.第一个Shell脚本:helloworld (1)需求:创建一个Shell脚本,输出helloworld (2)案例实操:

[atguigu@hadoop101 datas]$ touch helloworld.sh
[atguigu@hadoop101 datas]$ vi helloworld.sh

在helloworld.sh中输入如下内容

#!/bin/bash
echo "helloworld"

(3)脚本的常用执行方式 第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)

sh+脚本的相对路径
[atguigu@hadoop101 datas]$ sh helloworld.sh 
Helloworld
	sh+脚本的绝对路径
[atguigu@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh 
helloworld
	bash+脚本的相对路径
[atguigu@hadoop101 datas]$ bash helloworld.sh 
Helloworld
	bash+脚本的绝对路径
[atguigu@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh 
Helloworld

第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x) (a)首先要赋予helloworld.sh 脚本的+x权限

[atguigu@hadoop101 datas]$ chmod 777 helloworld.sh

(b)执行脚本 相对路径

[atguigu@hadoop101 datas]$ ./helloworld.sh 
Helloworld

绝对路径

[atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh 
Helloworld

注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。 3.第二个Shell脚本:多命令处理 (1)需求: 在/home/atguigu/目录下创建一个banzhang.txt,在banzhang.txt文件中增加“I love cls”。 (2)案例实操:

[atguigu@hadoop101 datas]$ touch batch.sh
[atguigu@hadoop101 datas]$ vi batch.sh

在batch.sh中输入如下内容

#!/bin/bash


cd /home/atguigu
touch cls.txt
echo "I love cls" >>cls.txt
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第3章 Shell脚本入门
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档