建站(二)Linux命令


layout: post
title: “建站(二)”
subtitle: “Linux命令”
date: “2018-9-24”
author: “cj”
tags:
web
Laravel
linux
bash

整理一下學習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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章