Nuts & Bolts of Bash - joyocaowei/joyocaowei.github.io GitHub Wiki

Use the Unofficial Bash Strict Mode

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

The set -e option instructs bash to immediately exit if any command has a non-zero exit status.
The set -u affects variables.
The set -o pipefail setting prevents errors in a pipeline from being masked.

Source

Execute Shell Script Using . ./ (dot space dot slash)
While executing the shell script using "dot space dot slash", as shown below, it will execute the script in the current shell without forking a sub shell. For example, . ./setup.sh or . $HOME/setup.sh

Subshell

(cd ~ && other commands)

A subshell is a child process launched by a shell (or shell script).

Arrays

bash-3.2$ array=(one two three)
bash-3.2$ echo ${array[@]}
one two three
bash-3.2$ echo ${array[2]}
three
bash-3.2$ echo ${#array[@]}
3
bash-3.2$ array[3]=four
bash-3.2$ echo ${array[*]}
one two three four

SFTP

# 使用端口进行连接
sftp -P remote_port remote_user@remote_host

# 从远程服务器拉取文件
get /path/remotefile
get -r remoteDirectory

# 上传本地文件到服务器
put local_file

mkdir folderName  # 如果远程服务器上不存在目录,需要先创建
put -r folderName

# 查看远程服务器目录内容
ls

# 查看本地目录内容
lls

# 执行本地shell命令
![command]

others

查看系统版本

Unix: cat /etc/release | cat /etc/redhat-release | uname -a
Linux: lsb_release -a | cat /etc/issue

getent get entries from administrative database, for example getent password
ps -e -o pid,ppid,comm more infomation use man ps
ps | grep $$ | awk '{print $4}' 查看当前使用的shell
netstat -anp 查看被打开的端口
curl -s --head ${url} 查看网页的header

查看jar包的详细内容

${JAVA_HOME}/bin/jar vtf name.jar -v 在标准输出中生成详细输出
-t 列出归档目录
-f 制定归档文件名

Linux下查看本机IP地址

/sbin/ifconfig (under solaris you may need to type ifconfig -a)
ip addr show (主要看eth0)