建站(二)

Linux命令

Posted by cj on September 24, 2018

整理一下学习Laravel过程中用到的Linux命令

lsb_release

Linux Standard Base.

我的机器上执行lsb_release -d将输出Description: Ubuntu 16.04.5 LTS,执行lsb_release -c -s将输出xenial,可以用来判断当前是否为指定的Linux发行版。

DISTRO=$(lsb_release -c -s)
[[ ${DISTRO} -ne "xenial" ]] && { echo "仅支持 Ubuntu 16.04 系统"; exit 1; }

$?

上一条命令的返回值。

可以用echo $?查看,也可以用在bash里判断上一条命令的返回值是否为指定数值。

lsb_release -d | grep 'Ubuntu' >& /dev/null
[[ $? -ne 0 ]] && { echo "仅支持 Ubuntu系统"; exit 1; }

id

Print real and effective user and group IDs.

常用id -u获取当前用户ID,并以是否为0判断是否为root用户。

[ $(id -u) != "0" ] && {
    echo "当前账户并非 root,请用 root 账户执行安装脚本(使用命令:sudo -H -s 切换为 root)"
}

set -e

Change the value of shell attributes and positional parameters, or display the names and values of shell variables. -e Exit immediately if a command exits with a non-zero status.

DIR=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )

在当前脚本中设置一个DIR的环境变量,代表当前脚本的文件夹路径。

Bash maintains a number of variables including BASH_SOURCE which is an array of source file pathnames.

${} acts as a kind of quoting for variables.

$() acts as a kind of quoting for commands but they’re run in their own context.

dirname gives you the path portion of the provided argument.

cd changes the current directory.

pwd gives the current path.

&& is a logical and but is used in this instance for its side effect of running commands one after another.

In summary, that command gets the script’s source file pathname, strips it to just the path portion, cds to that path, then uses pwd to return the (effectively) full path of the script. This is assigned to DIR. After all of that, the context is unwound so you end up back in the directory you started at but with an environment variable DIR containing the script’s path.

批量、递归地给当前目录、子目录下所有sh文件增加可执行权限

find /directory/of/interest/ -type f -iname "*.sh" -exec chmod +x {} \;

  1. -type: File type to look for, here “file”
  2. -iname: Ignore case in the name
  3. "*.sh": Globbing, telling the find command to search for files with “.sh” extension
  4. -exec chmod +x {}: This tells the find command to carry out a chmod command on each found file. Making each executable
  5. \;: Indicating end of command

修复 apt-get update 报错

问题:

Err:8 https://dl.yarnpkg.com/debian stable InRelease
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 4F77679369475BAA

修复:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 4F77679369475BAA

生成定长随机密码

添加于2019年2月18日16:22:09

示例:

head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13 ; echo ''

生成13位的,范围为[A-Za-z0-9]的字符串并输出到控制台。

真特么好用啊!

tr -dc 解释:

Translate, squeeze, and/or delete characters from standard input, writing to standard output.

-c, -C, --complement
         use the complement of SET1

-d, --delete
         delete characters in SET1, do not translate

重点是 complement 的解释,有集合中的 补集 含义。

In set theory, a complement of a set A refers to things not in (that is, things outside of) A.

Complement of set 01 means all characters except 0 and 1. Thus, the -d option will remove all characters that are neither 0 nor 1. 集合{0,1}的补集表示除0,1外的任意字符。因此,选项 -d 将会移除所有非{0,1}的字符。

针对上面的示例命令,tr -dc A-Za-z0-9 会将输入的字符串移除所有[A-Za-z0-9]之外的字符串,保留的内容再经过 | head -c 13 管道过滤一下长度,就生成了13位的、仅包含大小写字母和数字的字符串。Nice!

修改时区

添加于2019-2-24 23:49:37

# run as root
# 备份
mv /etc/localtime /etc/localtime.bak
# 使用上海时区
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 同步时间
apt-get install ntp ntpdate
ntpdate 0.us.pool.ntp.org
# 同步到硬件时钟
hwclock --systohc

Reference