shell脚本编写笔记

简介

shelll脚本就是将在控制台运行的内容,放在shell脚本里集中运行。可以将平常需要经常重复输入的操作放在shell脚本里批量自动运行。
本文主要介绍如何编写简单的shell脚本,给shell脚本传参,shell脚本获取当前运行时间,通过shell执行hql语句。

编写一个简单的shell脚本

新建一个脚本文件:

gedit hello.sh 

输入以下内容:

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

给脚本添加可执行权限:

chmod +x hello.sh

运行脚本:

./hello.sh

若不给脚本添加可执行权限,也可通过如下命令直接运行:

sh hello.sh

参考资料:菜鸟教程

shell脚本传参

新建一个 copy.sh脚本,将本地主机上/home/hadoop/hello.sh文件拷贝到某个远程主机,此远程主机的ip和用户名通过传参输入。

    #!/bin/bash
    
    #定义2个变量:$1,$2
    echo "主机ip:$1"
    echo "用户名:$2“
    #远程拷贝:scp 源文件 用户名@目的地ip:目的地文件夹,例如:
    scp  /home/hadoop/hello.sh  $2@$1:/home/lxxinn

运行脚本时,第一个参数会传给变量$1,第二个参数会传给变量$2。参数序号从1开始。运行命令:

sh copy.sh  192.168.0.X  user

即可运行copy.sh脚本。

获取当前日期时间

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

time1=$(date)
echo $time1

time2=$(date "+%Y%m%d")
echo $time2

time3=$(date -d "3 day ago" +%Y%m%d)
echo $time3

获取明天日期时间

time2=$(date -d 'tomorrow' "+%Y%m%d")
echo $time2

或:

time2=$(date -d '+1 days' "+%Y%m%d")
echo $time2

在这里插入图片描述

获取昨天日期

time2=$(date -d '-1 days' "+%Y%m%d")
echo $time2

在这里插入图片描述

可在菜鸟教程的在线编辑器运行上述脚本查看效果。
参考:shell脚本实现取当前时间

shell执行hql语句

通过beeline工具在控制台登录hive时,是做如下操作:

cd ..
cd e3base/hive-1.1.0-cdh5.14.0-e3base3.0.0/bin
./beeline -u jdbc:hive2://host-1**-**-***-**:1***/aiuser -n user -p 123456

通过shell脚本打开hive,并给hql脚本传参,然后执行。
hql语句如下:

use 数据库名;	

#删除分区表某个分区的数据,参数由shell脚本传入
alter table cpu_partition_limin_copy drop partition (partition_date ='${hiveconf:far_date}');

#从一个分区表往另一个分区表添加某个分区的数据
insert into table cpu_partition_limin_copy partition (partition_date = '${hiveconf:near_date}')
	select ip,value from cpu_partition_limin 
			where (partition_date = '${hiveconf:near_date}');

shell脚本如下:

#!/bin/bash      #用bash语言解释本文件内容
echo "hello"
while true
do
	time1=$(date "+%H%M%S")     #获取当前时间时分秒,%H%M%S格式
	if [ $time1 -eq 015010 ]      # -eq:等于,当时分秒等于1点50分10秒时,执行如下操作
	then
		time2=$(date  -d "9 day ago" +"%Y%m%d")    #获取当前日期的9天前日期
		echo $time2
		time3=$(date -d "11 day ago" +%Y%m%d)
		echo $time3
		cd ..
		cd e3base/hive-1.1.0-cdh5.14.0-e3base3.0.0/bin
		./beeline -u jdbc:hive2://host-1**-**-***-**:1***/aiuser -n user -p 123456 -hiveconf far_date=$time3 -hiveconf near_date=$time2 -f /aiuser/limin_ai/update.hql
	fi
done

此脚本的主要作用是,定时执行如下操作:进入hive后,通过-hiveconf给hql脚本传参,执行hql脚本,删除11天前的数据,添加9天前的数据。
可以看到,cd…这句开始往下3句,和控制台执行的过程一样。

场景实例
在linux中运行spark时,需要重复输入如下spark命令,比较繁琐,还容易出错。

nohup spark-submit --master spark://master-0.master-svc:17001,master-1.master-svc:17001 --executor-memory 6G --total-executor-cores 10 net.py  > ./log/log.txt 2>&1 &

解决方法:
1、新建一个test.sh文件,将上述命令放到文件中,保存。
2、运行脚本:sh test.sh

shell拼接字符串

#!/bin/bash

name="Shell"
url="http://c.biancheng.net/shell/"

str1=$name$url  #中间不能有空格
str2="$name $url"  #如果被双引号包围,那么中间可以有空格
str3=$name": "$url  #中间可以出现别的字符串
str4="$name: $url"  #这样写也可以
str5="${name}Script: ${url}index.html"  #这个时候需要给变量名加上大括号

echo $str1
echo $str2
echo $str3
echo $str4
echo $str5

nowdate=$(date "+%Y%m%d")
echo $nowdate
source="/data_source_"$nowdate".txt"
echo $source

参考:Shell字符串拼接(连接、合并)

常见错误:

写了一段shell脚本后,运行时总是报错:“syntax error: unexpected end of file”
在这里插入图片描述
报错原因:
我的这个脚本是在windows系统中写的,文件格式是dos,需要改成unix的:
:set ff 查看文件格式
在这里插入图片描述
:set ff=unix 设置文件格式为unix:
在这里插入图片描述
shell脚本报错:“syntax error: unexpected end of file” 原因和解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值