1. 程式人生 > 其它 >shell root切換oracle_第一章:shell

shell root切換oracle_第一章:shell

技術標籤:shell root切換oracleshell 列印日期shell 拷貝 一個目錄下的所有檔案 到 另個目錄下shell 檔案路徑有空格

第一章:shell

下圖是 一個常見的命令列:

897248f8d782de173a85692f88be0975.png
命令列
  • ~ 表示當前位於 home 目錄下。
  • $ 符號表示您現在的身份不是 root 使用者。

shell 是命令列直譯器,用於解釋命令。例如輸入 date 會輸出當前日期。

787fad17da42f867179e12a29942973d.png
date

shell 採用空格來分割命令進行解析。

例如 echo 命令,以空格分割。

[email protected]:~$echoHello
Hello

如果空格作為命令的一部分,需要採用轉義符號 \ 進行轉義。

如果執行的命令不是 shell 中內建的關鍵字,shell 會去諮詢環境變數 $PATH 。

例如配置的 Java 環境變數之前輸入 Java 命令無效,配置環境變數後就可以解釋了。

在 shell 中導航

在 Linux/macOS 中路徑以斜槓 / 分割,根目錄是 / 代表系統所有檔案都在該路徑之下。

在 windows 中路徑以反斜槓 \ 分割。分為多個盤,例如 (C/D/E/F)等,每個盤都有一個根目錄。

例如:C:\

絕對路徑表示以根目錄起始的路徑。

相對路徑則是相對於當前工作目錄的路徑。絕對路徑之外的都是相對路徑。

例如:使用 pwd 命令可以檢視當前的工作目錄。

[email protected]
:~$pwd
/home/weijiew

可以用 cd 命令來切換目錄。其中 .. 表示退回到上級目錄。

[email protected]:/home$cd..
[email protected]:/$

一個點 . 表示當前工作目錄。

[email protected]:/$cd./home
[email protected]:/home$

波浪線(~)表示當前使用者的主目錄,反斜槓(/)表示根目錄,所有檔案的起始,二者是不同的。

如果是 root 使用者,cd ~ 會切換到 /root 目錄下。

[email protected]:/#pwd
/
[email protected]
:/#cd~
[email protected]:~#pwd
/root

如果不是 root 使用者,會切換的到當前使用者的主目錄下。也就是 /home/{username} 目錄中。

例如我的使用者名稱是 weijiew ,那麼 cd ~ 將會進入到 /home/weijiew 中。

[email protected]:/$pwd
/
[email protected]:/$cd~
[email protected]:~$pwd
/home/weijiew

撤銷目錄切換。當切換到其他目錄後如果想回退到切換前的目錄中,可以採用 cd - 來實現。

[email protected]:~$pwd
/home/weijiew
[email protected]:~$cd/
[email protected]:/$cd-
/home/weijiew
[email protected]:~$pwd
/home/weijiew

ls 命令用於檢視當前目錄下的檔案。

[email protected]:/home$ls
weijiew

ls 命令對應很多引數,如果忘記引數含義可以採用 ls --help 進行檢視。

例如 ls -l 可以更詳細的列印檔案或檔案資訊。

[email protected]:~$ls-l/home
total0
drwxr-xr-x1weijiewweijiew4096Aug2414:47weijiew

第三行中開頭的 d 表示 /home 是一個目錄。

接下來的 9 個字元每三個字元一組。

假如用括號分割的話:d(rwx)(r-x)(r-x) 。

這三組分別代表了「檔案所有者」「使用者組」「其他所有人」對這個資料夾所擁有的許可權。

  • r (read) 代表可讀。
  • w (write) 代表可寫也就是具有修改權。
  • x 代表可執行。
  • - 則代表該使用者不具備相應的許可權。

注意,/bin目錄下的程式在最後一組,即表示所有人的使用者組中,均包含x許可權,也就是說任何人都可以執行這些程式。

  • mv 重新命名或移動檔案。
  • cp 拷貝檔案。
  • mkdir 新建資料夾。

如果忘記命令的相關內容,可以使用 man 來檢視該命令的使用者手冊。

例如 man ls ,注意按 q 可以退出檢視介面。

man 可以獲得比 help 更多的資訊。

在程式間建立連線

資訊的流動稱為「流」,程式中存在兩個流。

流入程式的稱為「輸入流」,流出的則稱為「輸出流」

程式讀取資訊時會從輸入流中進行讀取,相反列印資訊時則是輸出到輸出流中。

例如重定向 > 可以將程式的輸入流和輸出流分別重定向到檔案中。

最簡單的重定向是 < file 和 > file。這兩個命令可以將程式的輸入輸出流分別重定向到檔案:

[email protected]:~$echohello>hello.txt
[email protected]:~$cathello.txt
hello
[email protected]:~$cathello2.txt
[email protected]:~$cathello2.txt
hello

>> 表示追加內容。

[email protected]:~$cathello.txt
hello
[email protected]:~$echohello>>hello.txt
[email protected]:~$cathello.txt
hello
hello

使用管道( pipes ),我們能夠更好的利用檔案重定向。|操作符允許我們將一個程式的輸出和另外一個程式的輸入連線起來。

管道線的用法例項。

ls -l / 表示檢視根目錄 (/) 下所有檔案的詳細資訊。

[email protected]:~$ls-l/
total580
lrwxrwxrwx1rootroot7Apr2314:40bin->usr/bin
drwxr-xr-x1rootroot4096Apr2314:49boot
drwxr-xr-x1rootroot4096Aug2415:37dev
drwxr-xr-x1rootroot4096Aug2415:34etc
drwxr-xr-x1rootroot4096May2921:45home
-rwxr-xr-x1rootroot591344Jan11970init
-rwxrwxrwx1rootroot11Aug2417:08last-modified.txt
lrwxrwxrwx1rootroot7Apr2314:40lib->usr/lib
lrwxrwxrwx1rootroot9Apr2314:40lib32->usr/lib32
lrwxrwxrwx1rootroot9Apr2314:40lib64->usr/lib64
lrwxrwxrwx1rootroot10Apr2314:40libx32->usr/libx32
drwxr-xr-x1rootroot4096Apr2314:40media
drwxr-xr-x1rootroot4096May2921:44mnt
drwxr-xr-x1rootroot4096Apr2314:40opt
dr-xr-xr-x9rootroot0Aug2415:34proc
drwx------1rootroot4096Aug2417:08root
drwxr-xr-x1rootroot4096Aug2415:34run
lrwxrwxrwx1rootroot8Apr2314:40sbin->usr/sbin
drwxr-xr-x1rootroot4096Apr1022:57snap
drwxr-xr-x1rootroot4096Apr2314:40srv
dr-xr-xr-x12rootroot0Aug2415:34sys
drwxrwxrwt1rootroot4096Aug2416:23tmp
drwxr-xr-x1rootroot4096Apr2314:41usr
drwxr-xr-x1rootroot4096Apr2314:43var

如果我只想檢視最後一個資料夾的相信資訊,可以採用管道線進行「過濾」

其中 tail 命令表示從尾部開始檢視檔案,引數 n 後面的數字表示具體要檢視幾行。

[email protected]:~$ls-l/|tail-n1
drwxr-xr-x1rootroot4096Apr2314:43var

也可以講資訊如果輸出流 (>) 寫入文字中。

weijie[email protected]:~$ls-l/|tail-n1>ls.txt
[email protected]:~$catls.txt
drwxr-xr-x1rootroot4096Apr2314:43var

一個功能全面又強大的工具

根使用者在類 Unix 系統中是非常強大的,擁有整個系統的所有許可權。

通過 su (super user 縮寫) 命令進行切換到根使用者,也稱為超級使用者或 root 使用者。

因為根使用者許可權比較高, 所以通常不建議處於根使用者的狀態避免誤操作。但是執行某些命令是許可權不夠,sudo 命令用於解決這個問題,也就是可以以 su 的身份來 do 一些事情。

普通使用者的命令列前面的符號是 $ 。例如:[email protected]:~$

root 使用者的命令列前面的符號則是 # 。例如:[email protected]:/home/weijiew#

有一件事情是您必須作為根使用者才能做的,那就是向sysfs 檔案寫入內容。系統被掛載在/sys下, sysfs 檔案則暴露了一些核心(kernel)引數。因此,您不需要藉助任何專用的工具,就可以輕鬆地在執行期間配置系統核心。注意 Windows or macOS沒有這個檔案

課後練習

  1. 在 /tmp 下新建一個名為 missing 的資料夾。
[email protected]:/tmp$cd/
[email protected]:/$cd/tmp
[email protected]:/tmp$mkdirmissing
  1. 用 man 檢視程式 touch 的使用手冊。

[email protected]:/tmp$ man touch

  1. 用 touch 在 missing 資料夾中新建一個叫 semester 的檔案。
[email protected]:/tmp$cdmissing
[email protected]:/tmp/missing$touchsemester
  1. 將以下內容一行一行地寫入 semester 檔案:
#!/bin/sh
curl--head--silenthttps://missing.csail.mit.edu

第一行可能有點棘手, # 在Bash中表示註釋,而 ! 即使被雙引號(")包裹也具有特殊的含義。單引號(')則不一樣,此處利用這一點解決輸入問題。更多資訊請參考 Bash quoting手冊[1]

雙引號對於 "$" "`" "" (啟用歷史擴充套件時) "!" 進行翻譯。參考[2]

單引號會保留字元中的所有資訊,

Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

[email protected]:/tmp/missing$echo'#!/bin/sh'>semester
[email protected]:/tmp/missing$catsemester
#!/bin/sh
[email protected]:/tmp/missing$echo'curl--head--silenthttps://missing.csail.mit.edu'>>semester
[email protected]:/tmp/missing$catsemester
#!/bin/sh
curl--head--silenthttps://missing.csail.mit.edu
  1. 嘗試執行這個檔案。例如,將該指令碼的路徑(./semester)輸入到您的shell中並回車。如果程式無法執行,請使用 ls命令來獲取資訊並理解其不能執行的原因。

無法執行,ls 檢視後發現該檔案不具備執行許可權。

[email protected]:/tmp/missing$./semester
-bash:./semester:Permissiondenied
[email protected]:/tmp/missing$ls-lsemester
-rw-rw-rw-1weijiewweijiew61Aug2416:45semester
  1. 檢視 chmod 的手冊(例如,使用man chmod命令)

[email protected]:/tmp/missing$ man chmod 瀏覽後按 q 退出。

  1. 使用 chmod 命令改變許可權,使 ./semester 能夠成功執行,不要使用sh semester來執行該程式。您的 shell是如何知曉這個檔案需要使用sh來解析呢?更多資訊請參考:[shebang](https://en.wikipedia.org/wiki/Shebang_(Unix "shebang"))
[email protected]:/tmp/missing$chmod+xsemester
[email protected]:/tmp/missing$ls-lsemester
-rwxrwxrwx1weijiewweijiew61Aug2416:45semester
[email protected]:/tmp/missing$./semester
  1. 使用 | 和 > ,將 semester 檔案輸出的最後更改日期資訊,寫入根目錄下的 last-modified.txt 的文 件中

最後更改時間:

[email protected]:/tmp/missing$stat-c%Ysemester
1598259525

先切換到根目錄下,在根目錄下建立並修改檔案許可權。

需要切換到 su 使用者下才能在根目錄下建立檔案。

777 代表三個使用者型別都具有可讀可寫可執行的許可權。

[email protected]:/tmp/missing$cd/
[email protected]:/$su
[email protected]:/#touchlast-modified.txt
[email protected]:/#ls-llast-modified.txt
-rw-r--r--1rootroot0Aug2417:04last-modified.txt
[email protected]:/#chmod777last-modified.txt
[email protected]:/#ls-llast-modified.txt
[email protected]:/#stat-c%Y/tmp/missing/semester>/last-modified.txt
-rwxrwxrwx1rootroot0Aug2417:04last-modified.txt
[email protected]:/#catlast-modified.txt
1598259525
  1. 寫一段命令來從 /sys 中獲取筆記本的電量資訊,或者桌上型電腦CPU的溫度。注意:macOS並沒有sysfs, 所以mac使用者可以跳過這一題。

路徑的 /sys/class/power_supply 目錄結構如下,筆記本的硬體資訊位於此處。

.
├──ac
│├──online
│└──type
├──battery
│├──capacity
│├──charge_counter
│├──current_now
│├──health
│├──present
│├──status
│├──technology
│├──temp
│├──type
│└──voltage_now
└──usb
├──online
└──type
[email protected]:/#cat/sys/class/power_supply/battery/capacity
96
[email protected]:/#cat/sys/class/power_supply/battery/health
Good
[email protected]:/#cat/sys/class/power_supply/battery/status
Notcharging
[email protected]:/#cat/sys/class/power_supply/battery/type
Battery

參考資料

[1]

Bash quoting手冊: https://www.gnu.org/software/bash/manual/html_node/Quoting.html

[2]

參考: https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html#Double-Quotes