Bash script 学习笔记

1 篇文章 0 订阅
1 篇文章 0 订阅

工作三年半, 零零碎碎用过很多也写过很多脚本。不是科班出身所以bash基础不是很好 每次写都要东查西查的。是时候写一个给自己的cheat sheet了。

以下所有脚本都试用与MAC 大部分适用于centos。 如有错误跪谢指出。

系统相关:

1 找出占用 27017 的进程,并杀掉

这里的-t表示只输出pid number
xargs 是什么玩意? http://man.linuxde.net/xargs
awk是什么玩意? (随手一查AWK 这么大学问)https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html
lsof -ti:27017 | xargs kill
同样我们可以用另一种更加强大的方法 ps aux

(错误)ps aux | grep "redis-server" | awk '{print $2}'  | xargs kill 

上面这个是错的 因为始终会返回grep的进程 虽然did the job 但会返回无法找到这个进程 (权限问题?)以下命令works

  ps aux | grep "6379" | grep -rv "S+" |awk '{print $3}' | xargs kill

2 如何开放指定命令sudo权限,让其他用户不用输入sudo密码

sudoers
https://blog.csdn.net/Field_Yang/article/details/51547804

命令行相关

1 如何在bash 脚本中输入sudo的密码

问题 每次需要sudo权限的命令 扔到bash中都会出现问题,怎么自动输入密码
?

echo "123" | sudo  $PATH_TO_ES/bin/elasticsearch -d

结果不太consistent
Introduce -S arg

  -S          The -S (stdin) option causes sudo to read the password from
                   the standard input instead of the terminal device.  The
                   password must be followed by a newline character.

搞定

echo "123" | sudo -S $PATH_TO_ES/bin/elasticsearch -d
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值