開發與維運

學習報告2

物聯網時代,Linux人才不可或缺物聯網、大數據、雲計算等主流行業大多數都是依靠Linux操作系統主流的互聯網應用都基於Linux平臺:網站、數據庫、網絡遊戲、即時通訊主流的服務器操作系統都採用Linux主流的軟件開發環境都是在Linux操作系統上:Java,嵌入式C/C++、Python、PHP

第二期我們學習了Linux基礎命令。

0 查找find

1 別名alias

2 變量的設置

3 常用的系統變量

4 通配符及組合按鍵

5 指令之間的分隔符(;&||)

6 輸出重定向(>,>>,1>,2>)

7 管道

通過本問還能瞭解到

env,set,export

unset,unalias

$?,$RANDOM

last

cut,wc,uniq,sort

0 查找

通過這個格式,可以查找到文件內容為{}裡面的數據

find ~ -type f -exec grep -n abs '{}' ';' -print

1 別名(alias)

一、設置方法

alias是一個比較好用的指令,用於對指令進行重新命名,方便記憶和使用

例如:我們要打開系統的網絡配置文件就需要輸入較長的指令(雖然vi指令不長,但是文件路徑有點深並不適合不好記憶)

[root@xiaoftest ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

我們給這串指令起一個別名’eth0',是不是很方便呢?

[root@xiaoftest ~]# alias eth0='vi /etc/sysconfig/network-scripts/ifcfg-eth0 '

這樣下一次訪問只需要輸入eth0即可達到一樣的效果

二、查看

直接輸入alias即可查看用戶目前已經配置可以用的別名

[root@xiaoftest ~]# alias

alias cp='cp -i'

alias eth0='vi /etc/sysconfig/network-scripts/ifcfg-eth0 '   //這是我們剛才定義的別名

alias l.='ls -d .* --color=tty'

alias ll='ls -l --color=tty'

alias ls='ls --color=tty'

alias mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@xiaoftest ~]#

三、取消設置別名

unalias

2 變量與變量的設置

一、變量的使用

個人認為變量其實是與別名相似,變量常常用來保存比較深的路徑,其實也可以用來保存指令的

變量名='需要定義的值'

[root@xiaoftest /]# test='ls -al'

[root@xiaoftest /]# $test

total

drwxr-xr-x   root root   Jan   : .

drwxr-xr-x   root root   Jan   : ..

-rw-r--r--    root root      Jan   : .autofsck

另外變量還可以向後追加

[root@xiaoftest /]# name='sunfan'
[root@xiaoftest /]# echo $name
sunfan
[root@xiaoftest /]# name="$name"test   //先引用$name後再追加test
[root@xiaoftest /]# echo $name
sunfantest            //追加test成功
[root@xiaoftest /]#

先定義變量name的值是sunfan ,然後通過對變量的引用($name)再附加test賦值給name

name的值就變成了sunfantest. (注意 1變量的賦值一定在等號前後不能有空格 2引用變量的時候一定要用雙引號)

在引用變量後面添加值後再賦值給這個變量的方法常常用於對環境變量的配置,

如果我要在環境變量裡寫入JAVA_HOME="XXXXXX", 這樣就會很方便--->PATH="$PATH":XXXXXX

二、查看系統變量

env 顯示目前系統中主要的變量

set 顯示系統中全部的變量內容

export 如果不接變量的話,則打印所有的變量同set

如果接變量的話,則將用戶變量添加至環境變量中去

[root@xiaoftest ~]# name='test' //不使用export

[root@xiaoftest ~]# env |grep name //環境變量中去查詢不到name

[root@xiaoftest ~]# echo $name

test

[root@xiaoftest ~]# export name="testtest" //使用export賦值

[root@xiaoftest ~]# echo $name

testtest

[root@xiaoftest ~]# env |grep name  //環境變量中能夠查詢到name

name=testtest //結果

三、取消設置變量

unset

3 常用的系統變量

? 變量(常用)

echo $? 用於輸出上一個指令執行是否錯誤,說明沒有錯誤則顯示0,如果是錯誤的話,就顯示錯誤碼

[root@xiaoftest /]# ls

bin   dev  home    lib         media  mnt  proc  sbin     sfcd  sys  usr

boot  etc  initrd  lost+found  misc   opt  root  selinux  srv   tmp  var

[root@xiaoftest /]# echo $?

0       //上一句指令執行成功

[root@xiaoftest /]# lls

-bash: lls: command not found

[root@xiaoftest /]# echo $?

127   //上一句指令執行失敗,錯誤碼127

[root@xiaoftest /]#

RANDOM

[root@xiaoftest ~]# echo $RANDOM

 4 通配符及組合按鍵

5 指令之間的分隔符

command1;command2

指令1和指令2都會執行

[root@xiaoftest sfcd]# ls;ls

d

d

command1&&command2

指令1成功的情況下指令2才會執行

[root@xiaoftest sfcd]# ls&&ls

d

d

command1||command2

指令1失敗的情況下指令2才會執行

[root@xiaoftest sfcd]# lll||ls
-bash: lll: command not found
d

6 輸出重定向

>,>> 都是屏幕輸出到文件 ,>與>>的區別在於 >>是追加在原有文本內容的後面,而>則是覆蓋

1> 成功的信息輸入

2> 報錯的信息輸入

下面給三個例子

成功的信息輸入到a 失敗的信息輸入到b

[root@xiaoftest sfcd]# lll >>a >>b

[root@xiaoftest sfcd]# lll >>a >>b

[root@xiaoftest sfcd]# lll >>a >>b

[root@xiaoftest sfcd]# cat a b

-bash: lll: command not found

-bash: lll: command not found

-bash: lll: command not found

[root@xiaoftest sfcd]#

成功和失敗的信息都輸入到c(注意同文件的失敗信息輸出是>&1)

[root@xiaoftest sfcd]# lll 1>>c 2>&1 //報錯

[root@xiaoftest sfcd]# lll 1>>c 2>&1 //報錯

[root@xiaoftest sfcd]# lll 1>>c 2>&1 //報錯

[root@xiaoftest sfcd]# ll 1>>c 2>&1 //成功

[root@xiaoftest sfcd]# cat c

-bash: lll: command not found

-bash: lll: command not found

-bash: lll: command not found

total 12

-rw-r--r--  1 root root  0 Jan  1 20:09 a

-rw-r--r--  1 root root 90 Jan  1 20:09 b

-rw-r--r--  1 root root 90 Jan  1 20:11 c

-rwxrwxrwx  1 root root  3 Jan  1 14:57 d

[root@xiaoftest sfcd]#

成功的信息輸入到a 失敗的丟到垃圾桶

[root@xiaoftest sfcd]# ll 1>>e 2>>/dev/null
[root@xiaoftest sfcd]# lll 1>>e 2>>/dev/null
[root@xiaoftest sfcd]# cat e
total 12
-rw-r--r-- 1 root root 0 Jan 1 20:09 a
-rw-r--r-- 1 root root 90 Jan 1 20:09 b
-rw-r--r-- 1 root root 267 Jan 1 20:11 c
-rwxrwxrwx 1 root root 3 Jan 1 14:57 d
-rw-r--r-- 1 root root 0 Jan 1 20:13 e

 7 管道

對命令的結果進行一步一步的過濾及篩選

例如command1|command2|command3...

command1的結果傳遞給command2,command1進行過濾後傳遞給command3...最後輸出

...悲劇 ,我本想是看grep reboot的結果命令少打了

[root@xiaoftest sfcd]# last |reboot //這第二個命令直接重啟了,悲劇。

重新來!

[root@xiaoftest ~]# last|grep reboot

reboot   system boot  2.6.-.EL      Wed Jan   :          (:)

reboot   system boot  2.6.-.EL      Wed Jan   :          (:)

reboot   system boot  2.6.-.EL      Wed Jan   :          (:)

reboot   system boot  2.6.-.EL      Wed Jan   :          (:)

 

在第一個命令結束後再根據第一個命令獲得了reboot的記錄

[root@xiaoftest ~]# last|grep reboot|wc -l

4

在第二個命令結束後再根據第二個命令獲得了reboot的具體次數統計

管道的常用指令

cut 用於篩選文檔

[root @test /root ]# cut -d "分隔字符" [-cf] fields

參數說明:

-d  :後面接的是用來分隔的字符,默認是『空格符』

-c  :後面接的是『第幾個字符』

-f  :後面接的是第幾個區塊?

範例:

[root @test /root]# cat /etc/passwd | cut -d ":" -f

將 passwd 這個檔案裡面,每一行裡頭的 : 用來作為豎線,

而列出第一個區塊!也就是姓名所在啦!

[root @test /root]# last | cut -d " " -f1

以空格符為分隔,並列出第一個區間!

[root @test /root]# last | cut -c1-

將 last 之後的數據,每一行的 - 個字符取出來!

-c 的使用

[root@xiaoftest ~]# last|cut -c1-15 //輸出1-15個字符

root     pts/

reboot   system

root     pts/

reboot   system

-d -f的使用 -d是採用分隔符 -f用來讀取第N個區間的(注意:這裡是整體的劃分,如本用例就是左右的整體劃分)

[root@xiaoftest sfcd]# cat user

Mary/password

Peter/password

kittey/password

 

xiaoLi/password

xiaoming/password

 

test/password

test2/password

[root@xiaoftest sfcd]# cat user |cut -d "/" -f1 //第一區間

Mary

Peter

kittey

 

xiaoLi

xiaoming

 

test

test2

[root@xiaoftest sfcd]# cat user |cut -d "/" -f2 //第二區間

password

password

password

 

password

password

 

password

password

wc 用於統計

[root @test /root ]# wc [-lmw]

參數說明:

-l   :多少行

-m   :多少字符

-w   :多少字?

範例:

[root @test /root]# cat /etc/passwd | wc -l

這個檔案裡頭有多少行?

[root @test /root]# cat /etc/passwd | wc -w

這個檔案裡頭有多少字!?

uniq 用於去重

[root@xiaoftest sfcd]# last |cut -d " " -f 1 |uniq //使用了去重
root
reboot
root
reboot

 

[root@xiaoftest sfcd]# last |cut -d " " -f 1  //沒有使用去重
root
root
root
reboot
root
reboot

sort 排序(更加有利於去重與uniq配合使用較好)

[root@xiaoftest sfcd]# last |cut -d " " -f  |sort |uniq //先排序後去重

 

reboot

root

wtmp

[root@xiaoftest sfcd]#

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *