275篇 Default中的文章

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 ~

是时间将CentOS8 升级为CentOS Stream了

CentOS8 的生命周期到2021年12月31日截止,CentOS7是到2024年6月30日。还有使用CentOS8的注意了,官方可能不再更新,需要将系统升级到CentOS Stream.

升级也很简单:

dnf swap centos-linux-repos centos-stream-repos
dnf distro-sync

附 CentOS Stream 下截地址:

http://mirrors.cqu.edu.cn/CentOS/8-stream/isos/x86_64/
http://mirrors.aliyun.com/centos/8-stream/isos/x86_64/
http://mirrors.neusoft.edu.cn/centos/8-stream/isos/x86_64/
http://mirrors.163.com/centos/8-stream/isos/x86_64/
http://mirrors.tuna.tsinghua.edu.cn/centos/8-stream/isos/x86_64/
http://mirrors.ustc.edu.cn/centos/8-stream/isos/x86_64/
http://ftp.sjtu.edu.cn/centos/8-stream/isos/x86_64/
http://mirrors.nju.edu.cn/centos/8-stream/isos/x86_64/
http://mirrors.bfsu.edu.cn/centos/8-stream/isos/x86_64/

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 ~

修改docker镜像

若有个名为nginx的容器:

docker run -it nginx bash

修改内容后退出,使用docker commit命令提交修改。

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

如 :

docker commit -a "John Click" -m "add extra plugins" 2ff7f15ca033 nginx:latest

2ff7f15ca033 为刚才修改的docker容器id。

More ~

go test 覆盖率报告

go test -coverprofile=coverage.out
go tool cover -html=coverage.out

对指定目录

go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out
More ~

ssl自签名证书

查看证书

openssl x509 -in cert.pem -noout -text

生成证书

openssl genrsa -out server.key 1024
3650 -key server.key -out server.crt -subj "/C=CN/ST=mykey/L=mykey/O=mykey/OU=mykey/CN=domain1/CN=domain2/CN=domain3"

按向导生成证书

openssl genrsa -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -in server.csr -out server.crt -signkey server.key -days 3650
More ~

yarn 更新依赖

通过 yarn outdated 可以看到一些待更新的,但是满足最低要求,执行yarn upgrade时并未更新。

可以执行yarn upgrade --latest来更新依赖到最新版本。

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 ~