#luacheck格式
[liyanhong@localhost lua]$ luacheck redis.lua
Checking redis.lua 1 error
redis.lua:12:24: expected '=' near 'red'
Total: 0 warnings / 1 error in 1 file
#git commit会处罚hooks的pre-commit文件运行
#重写了pre-commit文件脚本,会调用luacheck检查*.lua代码是否合格
#!/usr/bin/env bash
echo "I am pre-commit"
lua_files=$(git status -s | gawk '{if($1=="A"||$1=="M") {print $2}}')
echo "the file is $lua_files"
if [[ "$lua_files" != "" ]]
then
result=$(luacheck my.lua | tail -1 | gawk '{print $5}')
if [[ $result != "0" ]]
then
echo "some abort"
exec < /dev/tty#此处需要/dev/tty输入,直接read -p不行
read -p "Abort commit(y/n)?"
if [[ "$REPLY" == "Y" || "$REPLY" == "y" ]]
then
echo "Abort commit"
exit 1
fi
fi
fi
/dev/tty文件:
下面我们再来看/dev/tty的用途。当程序打开此文件是,Linux会自动将它重定向到一个终端窗口,因此该文件对于读取人工输入时特别有用。见如下Shell代码:
/> vi test_dev_tty.sh
#!/bin/bash
printf "Enter new password: " #提示输入
stty -echo #关闭自动打印输入字符的功能
read password < /dev/tty #读取密码
printf "\nEnter again: " #换行后提示再输入一次
read password2 < /dev/tty #再读取一次以确认
printf "\n" #换行
stty echo #记着打开自动打印输入字符的功能
echo "Password = " $password #输出读入变量
echo "Password2 = " $password2
echo "All Done"
总结:
1.shell脚本中if [[ ]] 做判别式
2.read -p 不行时 考虑 exec < /dev/tty重定向输入
3.read 不加后缀时,回复是放默认在$REPLY中
4.gawk 中可以'{if() {}}'
5.shell 中赋值是 var = $()