markdown Linux系统操作和bash脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Linux系统操作和bash脚本相关的知识,希望对你有一定的参考价值。

## centOS
1. check if software info
```
yum info xxx
```

## ubuntu
1. using gpg to encrypt your file
```
gpg --symmetric xxx.txt # encrypt xxx.txt
gpg --decrypt xxx.txt.gpg # decrypt your file
```

## bash scripts
### amazing webs
1. [IBM Developer](https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/index.html)

### bash-snippets 
1. rename folders
```
ls |cut -c 6- | xargs -i mv rect-{} {}
```
2. check if a variable is empty
see "https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/"
and "https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash"
```
if [ -z "$var" ]
then
      echo "\$var is empty"
else
      echo "\$var is NOT empty"
fi
```

```
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi
```

### grep, sed and awk
1. grep
```
# find filenames in current folder, not include tar
ls | grep -v tar
```

2. sed
get the name of all mp4 video files from current directory.
```
ls *.mp4 | sed 's/.mp4//g'
```

3. [replace a string for all files.](https://stackoverflow.com/questions/4804405/search-and-replace-in-vim-across-all-the-project-files)
```
find ./ -name "*.xml" | xargs sed -i 's/head/helmet/g' {}
# or https://stackoverflow.com/questions/11392478/how-to-replace-a-string-in-multiple-files-in-linux-command-line
find ./ -type f -exec sed -i 's/string1/string2/g' {} \;
# or do it this way
cd /path/to/your/folder
sed -i 's/foo/bar/g' *

```

以上是关于markdown Linux系统操作和bash脚本的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Bash 脚本中检测操作系统?

从 Bash 脚本中检测操作系统并通知用户

使 bash 脚本在 Linux 和 Solaris 上运行

Shell 脚本之规范和变量

Linux里面bash是啥?

Linux教程 - Bash脚本初学者完整指南