33篇 shell related articles

CentOS 7升级gcc版本

1、安装centos-release-scl

sudo yum install centos-release-scl

2、安装devtoolset,注意,如果想安装7.版本的,就改成devtoolset-7-gcc,以此类推

sudo yum install devtoolset-8-gcc*

3、激活对应的devtoolset,所以你可以一次安装多个版本的devtoolset,需要的时候用下面这条命令切换到对应的版本

scl enable devtoolset-8 bash

查看版本

gcc -v
More ~

tcpdump常用命令

监视指定主机的数据包

打印所有进入或离开sundown的数据包.

tcpdump host sundown

也可以指定ip,例如截获所有210.27.48.1 的主机收到的和发出的所有的数据包

tcpdump host 210.27.48.1 

打印helios 与 hot 或者与 ace 之间通信的数据包

tcpdump host helios and ( hot or ace )

截获主机210.27.48.1 和主机210.27.48.2 或210.27.48.3的通信

tcpdump host 210.27.48.1 and (210.27.48.2 or 210.27.48.3 ) 

打印ace与任何其他主机之间通信的IP 数据包, 但不包括与helios之间的数据包.

tcpdump ip host ace and not helios

如果想要获取主机210.27.48.1除了和主机210.27.48.2之外所有主机通信的ip包,使用命令:

tcpdump ip host 210.27.48.1 and ! 210.27.48.2

截获主机hostname发送的所有数据

tcpdump -i eth0 src host hostname

监视所有送到主机hostname的数据包

tcpdump -i eth0 dst host hostname
More ~

shell去除重复行

去除重复行

sort file.txt |uniq

查找非重复行

sort file.txt |uniq -u

查找重复行

sort file.txt |uniq -d

统计

sort file.txt| uniq -c
More ~

Delete lines with sed command

Sed Command to Delete Lines: Sed command can be used to delete or remove specific lines which matches a given pattern or in a particular position in a file. Here we will see how to delete lines using sed command with various examples.

More ~

How to Check if a File or Directory Exists in Bash

Many times when writing Shell scripts, you may find yourself in a situation where you need to perform an action based on whether a file exists or not.

In Bash, you can use the test command to check whether a file exists and determine the type of the file.

The test command takes one of the following syntax forms:

test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]
More ~

linux命令: awk

awk

文本和数据进行处理的编程语言

补充说明

awk 是一种编程语言,用于在 linux/unix 下对文本和数据进行处理。数据可以来自标准输入(stdin)、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是 linux/unix 下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk 有很多内建的功能,比如数组、函数等,这是它和 C 语言的相同之处,灵活性是 awk 最大的优势。

awk 命令格式和选项

语法形式

awk [options] 'script' var=value file(s)
awk [options] -f scriptfile var=value file(s)
More ~

shell脚本中的if 参数-a至-z

[-a file] 如果file存在则为真 

不过貌似有时候-a表示为and:条件与

[-b file] 如果file存在且是一个块特殊文件则为真 
[-c file] 如果file存在且是一个字特殊文件则为真 
[-d file] 如果file文件存在且是一个目录则为真 
-d前的!是逻辑非 
例如: 
if [ ! -d $lcd_path/$par_date ] 
表示后面的那个目录不存在,则执行后面的then操作 
[-e file] 如果file文件存在则为真 
[-f file] 如果file存在且是一个普通文件则为真 
[-g file] 如果file存在且已经设置了SGID则为真(SUID 是 Set User ID, SGID 是 Set Group ID的意思) 
[-h file] 如果file存在且是一个符号连接则为真 
[-k file] 如果file存在且已经设置粘制位则为真 
当一个目录被设置为"粘制位"(用chmod a+t),则该目录下的文件只能由 
一、超级管理员删除 
二、该目录的所有者删除 
三、该文件的所有者删除 
也就是说,即便该目录是任何人都可以写,但也只有文件的属主才可以删除文件。 
具体例子如下: 
#ls -dl /tmp 
drwxrwxrwt 4 root    root  ......... 
注意other位置的t,这便是粘连位。 
[-p file] 如果file存在且是一个名字管道(F如果O)则为真 
管道是linux里面进程间通信的一种方式,其他的还有像信号(signal)、信号量、消息队列、共享内存、套接字(socket)等。 
[-r file] 如果file存在且是可读的则为真 
[-s file] 如果file存在且大小不为0则为真 
[-t FD] 如果文件描述符FD打开且指向一个终端则为真 
[-u file] 如果file存在且设置了SUID(set userID)则为真 
[-w file] 如果file存在且是可写的则为真 
[-x file] 如果file存在且是可执行的则为真 
[-O file] 如果file存在且属有效用户ID则为真 
[-G file] 如果file存在且属有效用户组则为真 
[-L file] 如果file存在且是一个符号连接则为真 
[-N file] 如果file存在and has been mod如果ied since it was last read则为真 
[-S file] 如果file存在且是一个套接字则为真 
[file1 –nt file2] 如果file1 has been changed more recently than file2或者file1 exists and file2 does not则为真 
[file1 –ot file2] 如果file1比file2要老,或者file2存在且file1不存在则为真 
[file1 –ef file2] 如果file1和file2指向相同的设备和节点号则为真 
[-o optionname] 如果shell选项“optionname”开启则为真 
[-z string] “string”的长度为零则为真 
[-n string] or [string] “string”的长度为非零non-zero则为真 
[sting1==string2] 如果2个字符串相同。“=”may be used instead of “==”for strict posix compliance则为真 
[string1!=string2] 如果字符串不相等则为真 
[string1<string2] 如果“string1”sorts before“string2”lexicographically in the current locale则为真 
[arg1 OP arg2] “OP”is one of –eq,-ne,-lt,-le,-gt or –ge.These arithmetic binary oprators return true if “arg1”is equal to,not equal to,less than,less than or equal to,greater than,or greater than or equal to“agr2”,respectively.“arg1”and “agr2”are integers. 
More ~

shell date 格式化

date  --date="STRING"
date  --date="next Friday"
date  --date="2 days ago"
date  --date="yesterday"
date  --date="yesterday" +"%format"
# Get yesterday's date in dd-mm-yy format
date  --date="yesterday" +"%d-%m-%y"
date  --date="yesterday" +"%m-%d-%y" # US date format
date  --date="yesterday" +"%Y-%m-%d" # YYYY-mm-dd format
## store y'day date in a shell variable called yday and display it ##
yday=$(date  --date="yesterday" +"%Y-%m-%d")
echo "$yday"
  • You can use fortnight for 14 day.
  • week for 7 days.
  • hour for 60 minutes
  • minute for 60 seconds
  • second for one second
  • You can also use this / now / today keywords to stress the meaning.
date --date='fortnight'
date --date='5 fortnight'
date --date='fortnight ago'
date --date='5 fortnight ago'
date --date='2 hour'
date --date='2 hour ago'
date --date='20 minute'
date --date='20 minute ago'
date --date='this Friday'
## OR ##
date --date='next Friday'

Set System Date & Time?

date --set='+30 minutes'
date --set='1 day ago'
More ~