Bash的知识点总结_0


1.在bash中如何清除某个变量周围的空白符号(起到类似于Python中的strip()函数的作用)?

http://search.aol.com/aol/search?q=shell+strip+space

方法一:tr命令
tr -d ' '
echo " test test test " | tr -d ' '
方法二:sed命令
# sed 's/ //g'
# echo " test test test " | sed 's/ *$//'
# echo " test test test " | sed 's/^ *//'
# echo " test test test " | sed -e 's/^ *//' -e 's/ *$//'
方法三:

字符串截取

# rtrim=${trimmed%% }
# ltrim=${trimmed## }

字符串替换

# echo ${s//[[:blank:]]/}
方法四:xargs命令
# echo " lol " | xargs

差不多就这些了,其它的也多多少少是一些变形……

2.获取文件的目录名/文件名
#!/bin/bash

# 1.获取脚本所在目录的名称
base_dir=$(dirname $0)/..
pwd
cd $base_dir
pwd

# 2.获取脚本的文件名
file_abs_name=$0
base_name=${file_abs_name##/*/}
base_name_no_ext=${base_name%.*}
3.grep命令的一些tips
   -e PATTERN, --regexp=PATTERN
	  Use  PATTERN  as	the  pattern.	This  can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-).  (-e is specified by POSIX.)

   -f FILE, --file=FILE
	  Obtain patterns from FILE, one per line.	The empty file contains zero patterns, and therefore matches nothing.  (-f is specified by POSIX.)

   -i, --ignore-case
	  Ignore case distinctions in both the PATTERN and the input files.  (-i is specified by POSIX.)

   -v, --invert-match
	  Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)

   -c, --count
	  Suppress normal output; instead print a count of matching lines for each input file.  With the -v, --invert-match  option  (see  below),	count  non-matching lines.  (-c is specified by POSIX.)

   -l, --files-with-matches
	  Suppress	normal output; instead print the name of each input file from which output would normally have been printed.  The scanning will stop on the first match.  (-l is specified by POSIX.)

   -q, --quiet, --silent
	  Quiet; do not write anything to standard output.	Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.  (-q is specified by POSIX.)

   -n, --line-number
	  Prefix each line of output with the 1-based line number within its input file.  (-n is specified by POSIX.)

Context Line Control
   -A NUM, --after-context=NUM
	  Print NUM lines of trailing context after matching lines.  Places a line containing a group separator (--)  between  contiguous  groups  of  matches. With the -o or --only-matching option, this has no effect and a warning is given.

   -B NUM, --before-context=NUM
	  Print  NUM  lines  of  leading  context before matching lines.  Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

   -C NUM, -NUM, --context=NUM
	  Print NUM lines of output context.  Places a line containing a  group  separator	(--)  between  contiguous  groups  of  matches.   With	the  -o  or --only-matching option, this has no effect and a warning is given.

 

# cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:14551283   76129    0    0    0     0          0         0 14551283   76129    0    0    0     0       0          0
  eth0:18007778  145326    0    0    0     0          0         0   502374    3647    0    0    0     0       0          0
  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0

Method.2
# grep -A1 'lo:' /proc/net/dev | grep -v 'lo:' | awk -F':' '{print $1}'
  eth0

# grep -A1 'lo:' /proc/net/dev | grep -v 'lo:' | awk -F':' '{print $1}' | sed 's/[[:space:]]//g'
eth0

Method.3
In [5]: print os.popen("grep -A1 'lo:' /proc/net/dev | grep -v 'lo:' | awk -F':' '{print $1}' | sed 's/[[:space:]]//g'").read()
eth0
In [6]: print os.popen("grep -A1 'lo:' /proc/net/dev | grep -v 'lo:' | awk -F':' '{print $1}'").read().strip()
eth0
4.shell中的条件测试
5.使用shell脚本进行自动化测试
6.查看Linux的内核版本命令
$ uname -a

$ lsb_release -a

$ cat /etc/issue

$ cat /proc/version

$ getconf LONG_BIT

$ file /bin/ls

$ arch
7.导出manual为txt文件
# man grep | col -b >grep_manual.txt

 

, ,

《“Bash的知识点总结_0”》 有 1 条评论

  1. 查看CentOS系统的内核/版本信息
    `
    $ uname -r
    $ rpm –query centos-release
    $ cat /etc/centos-release
    $ cat /proc/version
    $ cat /etc/issue

    # 不是所有的机器都安装了 lsb_release
    $ lsb_release -a
    # 安装 lsb_release
    $ sudo yum provides */lsb_release
    $ sudo yum install redhat-lsb

    # CentOS 7
    $ hostnamectl
    `

    https://linuxconfig.org/how-to-check-centos-version
    http://www.binarytides.com/command-check-centos-version/
    https://www.unixmen.com/linux-troubleshooting-fix-lsb_release-command-found-centos/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注