Tag - shell

? shell ? ? python ?    2021-01-22 16:17:53    1614    0    0

Shell seq用法

seq --help

  1. Usage: seq [OPTION]... LAST
  2. or: seq [OPTION]... FIRST LAST
  3. or: seq [OPTION]... FIRST INCREMENT LAST
  4. Print numbers from FIRST to LAST, in steps of INCREMENT.
  5. Mandatory arguments to long options are mandatory for short options too.
  6. -f, --format=FORMAT use printf style floating-point FORMAT
  7. -s, --separator=STRING use STRING to separate numbers (default: \n)
  8. -w, --equal-width equalize width by padding with leading zeroes
  9. --help display this help and exit
  10. --version output version information and exit

注意事项

seq 选项[-s " "][-w] FIRST INCREMENT LAST
注意 选项紧跟seq, 位于序列数字FIRST INCREMENT LAST之前,FIRST INCREMENT LAST放在最后
比如

  1. seq -s " " -w 2 100 1000

常用命令

  • 生成一个1 到 10 的序列 ,不指定分隔符s时,默认是换行符。
  1. root@gua-vm1:/vob# seq 10
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  • 生成一个1 到 10 的序列 ,指定分隔符为空格 ,在seq 后面加上选项 -s " "
  1. root@gua-vm1:/vob# seq -s " " 10
  2. 1 2 3 4 5 6 7 8 9 10
  • 生成一个3 到 10 的序列 ,只指定头尾时,默认递增量为1
  1. r
? shell ?    2021-01-20 17:03:25    1276    0    0

打印全部列

  1. awk '{print $0}'

打印指定列

  1. awk '{print $2,$4}'

打印指定列并拼接字符串

  1. awk '{print $1,"hhah",$2}'
  2. awk '{print $1"hahah"$2}'

按指定分隔符取列

  1. awk -F: '{ print $1 }'

打印某一列及后面所有列的内容

  1. awk '{out=""; for(i=2;i<=NF;i++){out=out" "$i}; print out}'

打印最后一列,最后一列的前一列,NF表示最后一列的列号

  1. awk '{print $NF, $(NF-1)}'

打印指定一行,NR表示行号

  1. awk 'NR==6 {print $0}'

打印第六行,以 :分隔开的第一列

  1. awk -F ":" 'NR==6 {print $1}'

打印第4行以后的行

  1. git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR>4 {print $0}'
  2. 5 1f021028e22d92119fa5a0821273596e0c37be87-1f02102:1f02102
  3. 6 f36b0ad5a376edf485979b357af4ac1df87cc443-f36b0ad:f36b0ad
  4. 7 e3776955f8f3592051603d2299b93d3b55cbc52a-e377695:e377695
  5. 8 94c50a1fd810d5c1e62535fac8f6db3c01c4c476-94c50a1:94c50a1
  6. git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR<4 {print $0}'
  7. git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR<=4 {print $0}'

条件用法
打印包含 Merge branch 的行 用/strings/

  1. git log --pretty=format:"%
? shell ?    2020-04-03 10:07:53    2883    1    0

 

设置密码复杂度

http://www.linux-pam.org/Linux-PAM-html/sag-pam_cracklib.html

安装libpam-cracklib

apt-get install libpam-cracklib

 

修改文件 /etc/pam/common-password 中的配置,修改完保存即生效。

如果有加enforce_for_root​ ,则对root 有效。

默认对root无效。 当root 为操作修改的用户时,虽然会提示,但是仍能修改成功。

 

vi common-password

password        requisite                       pam_cracklib.so retry=3 minlen=8 difok=3 dcredit=-1 ocredit=-1 enforce_for_root​

以上例子为:可重试三次,密码最少8个字符,与旧密码至少3个字符不同,至少一个数字,至少一个符号,对root操作也有效。


 

http://www.linux-pam.org/Linux-PAM-html/sag-pam_cracklib.html

-----------------


difok=N

This argument will change the default of 5 for the number of character changes in the new password that differentiate it from the old password.

minlen=N

The minimum acceptable size for the new password (plus one if credits are not disabled which is the default). In addition to the number of characters in the new password, credit (of +1 in length) is given for each different kind of character (other, upper, lower and digit). The default for this parameter is 9 whic

? svn ? ? shell ?    2019-10-12 10:00:26    1743    1    0

svn 强制提交日志

分析:

检查时间:提交前

效果:日志符合要求成功提交,日志不符合要求给出提示,并中断提交。

 

适用钩子脚本:svn -> hooks -> pre-commit (commit 前触发执行的脚本),当svn 有commit 请求时,会触发执行pre-commit(shell脚本),当这个脚本以非0退出时,中断commit , 当脚本正常退出时,允许commit.

 

进入版本库配置目录hooks ,可见模板 pre-commit.tmpl ,复制为pre-commit 。

root@tclserver:/var/svn/svn_dir/Auto_script/hooks# ll
-rwxr-xr-x 1 root root 3350 Apr 6 2017 pre-commit*
-rwxr-xr-x 1 root root 3510 Feb 6 2017 pre-commit.tmpl*


 pre-commit.tmpl 原模板的内容分析

# PRE-COMMIT HOOK
#
# The pre-commit hook is invoked before a Subversion txn is
# committed.  Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named 'pre-commit' (for which
# this file is a template), with the following ordered arguments:
#
#   [1] REPOS-PATH   (the path to this repository)
#   [2] TXN-NAME     (the name of the txn about to be committed)
#   ........
REPOS="$1"
TXN="$2"
# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | \
 grep "[a-zA-Z0-9]" > /dev/null |
? shell ? ? gitlab ?    2019-09-18 10:38:35    652    0    0

JQ URL

https://stedolan.github.io/jq/download/
https://stedolan.github.io/jq/manual/#Invokingjq
这是一个开源工具,用于处理json 。

前言

"The API uses JSON to serialize data. You don’t need to specify .json at the end of an API URL."

在linux 下使用gitlab API 返回json 对象,经常使用python处理返回结果。 如果只是单纯地使用shell脚本中,常将其当作文本,并用sed ,grep,cut分解。但是因为它不是准确地分解对象中的组成,而是简单地通过关键字进行筛选和切割,所以经常会有遗漏或不够准确。

在linux 中安装jq , 可以在shell 中直接使用jq 对json 对象进行处理。

安装jq

下载 jq 源码包编译安装
  1. # configure ,make ,make install
  2. cd jq
  3. autoreconf -i
  4. ./configure --disable-maintainer-mode
  5. make
  6. sudo make install

下载二进制文件直接使用

  1. [root@huangbei ~]# chmod +x jq-linux64
  2. [root@huangbei ~]# mv jq-linux64 /usr/bin/jq
  3. [root@huangbei ~]# jq
  4. jq - commandline JSON processor [version 1.6]
  5. Usage: jq [options] <jq filter> [file...]
  6. jq [options] --args <jq filter> [strings...]
  7. jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
  8. jq is a tool for processing JSON inputs, applying the given
? shell ?    2019-09-10 15:18:37    398    0    0


包含行首尾的空格

  1. echo ${#Massage}

不包含行首尾的空格

  1. echo $Massage | awk '{print length($0)}'

示例

  1. [lcf@huangbei ~]$ Massage=" Usage: $0 \${RELEASE_NOTE_FULL_PATH} "
  2. [lcf@huangbei ~]$ echo ${#Massage}
  3. 39
  4. [lcf@huangbei ~]$ echo $Massage | awk '{print length($0)}'
  5. 37
  1. [lcf@huangbei ~]$ Massage="Usage: $0 \${RELEASE_NOTE_FULL_PATH}"
  2. [lcf@huangbei ~]$ echo ${#Massage}
  3. 37
? shell ?    2019-07-10 15:19:11    455    0    0

 

shell 里做 判断时[ 左边一直报错,语法无问题,有redhat 里执行没有问题,脚本放到ubuntu 时报错

[: test: unexpected operator​

 

解决:

ubuntu 的shell 默认指向dash , 需要改回bash

sudo dpkg-reconfigure dash

选择 no

? shell ?    2019-01-29 15:36:08    275    1    0
  1. sed -i "/key/c\modify key line " file
  2. #把file 中带有 key 的所有行替换为modify key line

eg.

  1. change_list=`cat temp3.txt `
  2. for i in ${change_list}
  3. do
  4. if [[ $exp ]]; then
  5. sed -i "/${i}/c\\${i} <-- change" temp2.txt
  6. fi
  7. done

在exit 0 前面插入一行:i

  1. sed -i '/^exit 0/i \/sbin\/start ttyS0' /etc/rc.local

在exit 0 后面插入一行:a

  1. sed -i '/^exit 0/a \/sbin\/start ttyS0' /etc/rc.local

处理同一个打印结果 中,取多行标题及关键字匹配行

  1. accurev show wspaces | head -n 5 ; accurev show wspaces | grep EPDG_R16

以上accurev show wspaces 执行了两次,在两次结果里各取结果 再拼接,效率较低

  1. sed -n '1,5p'
  2. sed -n '/xxxxx/p'
  3. sed -n '1,5p;/^EPDG_R16_xxxx/p'

使用sed 将两个条件用分号串成多个条件,对结果进行“或关系”匹配

  1. accurev show wspaces | sed -n '1,5p;/^EPDG_R16_xxxx/p'
  2. Stream #
  3. | Target X-Action
  4. | | X-Action
? shell ?    2019-01-25 15:42:49    764    0    0
paste 按列拼接将文件拼接
 
  1. paste text1.txt text2.txt
直接字符串拼接
 
  1. B="case_"${A}".test"
将文件中的一组字符全部加上一样的前缀和后缀
 
  1. cat temp.txt | while read line
  2. do
  3. echo MME_case${line}.test >>temp_MME.txt
  4. done
? shell ? ? linux ?    2019-01-25 15:30:51    691    0    0

规律用法记忆点tips
从左边开始匹配:#  ##
从右边开始匹配:%  %%
最大化匹配:叠号 ##  %%  (从左/右匹配,当匹配到多个关键符时,匹配到最后一个停下)
最小化匹配:单号 #   %   (从左/右匹配,当匹配到多个关键符时,匹配到第一个停下)
从左匹配,* 号放在匹配符左边: */  *.  *-
从右匹配,* 号放在匹配符右边: /*  .* 
var=/vob/lcf/test.txt
常用取文件名:剩下的是从左起最后一个/的右边部分 ${var##*/}
常用取后缀:剩下的是从左起最后一个.的右边部分 ${var##*.}
常用取路径:剩下的是从右起第一个.的左边部分 ${var%/*}

.......


1/2