日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux.2- shell命令(部分)

發布時間:2023/12/16 linux 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux.2- shell命令(部分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. shell概述
shell是一個命令行解釋器,接收用戶操作指令,然后調用操作系統內核.
shell還是一個功能豐富的編程語言
2. shell解析器
cat /etc/shells
有 sh bash 等
3. 入門操作
寫shell腳本, 文件首行 #!/bin/bash 指定解析器
腳本的執行 sh + 絕對或相對路徑
如果賦予了腳本可執行權限,則可以直接使用相對路徑和絕對路徑執行腳本文件
4. 變量
系統變量直接 $JAVA_HOME
顯示當前shell變量: set
$# 顯示腳本輸入參數個數
$n n為數字,獲取腳本第n個參數,$0代表腳本名稱,10以上的參數用大括號 ${10}
$* 獲取所有參數,當做一個整體
$@ 獲取所有參數,分開對待
$? (小朋友你是否有很多問號???) 判斷上一個命令是否正確執行,0正確,非0錯誤
5. 加減乘除
(2+3)*4=20

-sh-4.2$ expr `expr 2 + 3` \* 4 20 -sh-4.2$ echo $[(2+3)*4] 20

6. 買大買小買定離手
[ 條件 ] : 判斷true 和false,條件前后有空格
字符串比較: =
整數比較: -lt (less than) 小于 -le (less equal) 小于等于
-eq( equal) 等于 -gt (greater than) 大于
-ge( greater equal) 大于等于 -ne (not equal) 不等
權限判斷: -r 讀 -w 寫 -x 執行
文件判斷: -f 文件 -d 目錄 -e 文件存在
7. 流程一條龍

if [ 條件 ];thenxxx fi if [ 條件 ]thenxxx fi case $變量 in"p1")xxx;;"p2")xxx;;*)xxx;; for(( 初始值;循環條件:變量控制 ))doxxxdonefor((變量 in v1 v2 v3))doxxxdone while [ 條件 ]doxxxdone

8. 增刪改查
8.1 cut

-sh-4.2$ cut --help Usage: cut OPTION... [FILE]... Print selected parts of lines from each FILE to standard output.Mandatory arguments to long options are mandatory for short options too.-b, --bytes=LIST select only these bytes-c, --characters=LIST select only these characters-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter-f, --fields=LIST select only these fields; also print any linethat contains no delimiter character, unlessthe -s option is specified-n with -b: don't split multibyte characters--complement complement the set of selected bytes, charactersor fields-s, --only-delimited do not print lines not containing delimiters--output-delimiter=STRING use STRING as the output delimiterthe default is to use the input delimiter--help display this help and exit--version output version information and exitUse one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Selected input is written in the same order that it is read, and is written exactly once. Each range is one of:N N'th byte, character or field, counted from 1N- from N'th byte, character or field, to end of lineN-M from N'th to M'th (included) byte, character or field-M from first to M'th (included) byte, character or fieldWith no FILE, or when FILE is -, read standard input.GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'cut invocation'

cut [參數] 文件名
默認分隔符為制表符
-f 列號,提取第幾列
-d 分隔符,自定義分隔符分隔列

-sh-4.2$ echo $JAVA_HOME /usr/lib/jvm/java-1.8.0 -sh-4.2$ echo $JAVA_HOME|cut -d '/' -f 1-sh-4.2$ echo $JAVA_HOME|cut -d '/' -f 2 usr -sh-4.2$ echo $JAVA_HOME|cut -d '/' -f 3 lib

8.2 sed
sed 一次處理一行數據,處理時把當前處理的行存儲在臨時緩沖區,sed 開始處理緩沖區中的內容,處理完了輸出

-sh-4.2$ sed --help Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...-n, --quiet, --silentsuppress automatic printing of pattern space-e script, --expression=scriptadd the script to the commands to be executed-f script-file, --file=script-fileadd the contents of script-file to the commands to be executed--follow-symlinksfollow symlinks when processing in place-i[SUFFIX], --in-place[=SUFFIX]edit files in place (makes backup if SUFFIX supplied)-c, --copyuse copy instead of rename when shuffling files in -i mode-b, --binarydoes nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (open files in binary mode (CR+LFs are not treated specially))-l N, --line-length=Nspecify the desired line-wrap length for the `l' command--posixdisable all GNU extensions.-r, --regexp-extendeduse extended regular expressions in the script.-s, --separateconsider files as separate rather than as a single continuouslong stream.-u, --unbufferedload minimal amounts of data from the input files and flushthe output buffers more often-z, --null-dataseparate lines by NUL characters--helpdisplay this help and exit--versionoutput version information and exit

-e 在指令模式上進行編輯
a 新增,a后面可以接一行字符串在下一行出現
d 刪除
s 查找替換

-sh-4.2$ cat test.txt aaaa bbb cccd dsd fsdf fsdf fsdf fsdfs -sh-4.2$ sed 'a 6666' test.txt aaaa bbb 6666 cccd dsd 6666 fsdf fsdf fsdf fsdfs 6666 -sh-4.2$ sed '1a 6666' test.txt aaaa bbb 6666 cccd dsd fsdf fsdf fsdf fsdfs -sh-4.2$ sed '/aa/d' test.txt cccd dsd fsdf fsdf fsdf fsdfs -sh-4.2$ sed 's/aa/666/' test.txt 666aa bbb cccd dsd fsdf fsdf fsdf fsdfs -sh-4.2$ sed 's/aa/666/g' test.txt 666666 bbb cccd dsd fsdf fsdf fsdf fsdfs

8.3 awk
把文件逐行讀入,空格Wie默認分隔符進行切片,切開部分再進行分析處理
awk [參數] ‘pattern{action}’ filename
-F 指定輸入文件分隔符
-v 賦值一個用戶定義變量

-sh-4.2$ cat test.txt aaaa bbb cccd dsd ff fd fsf fqq 666 777 888 -sh-4.2$ awk -F' ' '/^f/{print $2}' test.txt fd-sh-4.2$ cat test.txt aaaa bbb cccd dsd ff fd fsf fqq 666 777 888 -sh-4.2$ awk -F' ' 'BEGIN{print "xxx,ddd"} {print} END{print "end 9999"}' test.txt xxx,ddd aaaa bbb cccd dsd ff fd fsf fqq 666 777 888 end 9999-sh-4.2$ cat test.txt 111 aaaa bbb 222 cccd dsd 333 ff fd fsf fqq 666 777 888 -sh-4.2$ awk -F' ' -v i=10 '/[0-9]*/{print $1*i}' test.txt 1110 2220 3330 6660 7770

awk內置變量: FILENAME 文件名 NR 已讀的記錄數 NF 瀏覽記錄的域個數(切割后,列的個數)

-sh-4.2$ cat test.txt 111 aaaa bbb 222 cccd dsd 333 ff fd fsf fqq 666 777 888 -sh-4.2$ awk -F ' ' '{print "filename:" FILENAME " readnum:" NR " columns:" NF}' test.txt filename:test.txt readnum:1 columns:3 filename:test.txt readnum:2 columns:3 filename:test.txt readnum:3 columns:5 filename:test.txt readnum:4 columns:1 filename:test.txt readnum:5 columns:2

8.4 sort
sort 參數
-n 按照數值的大小排序
-r 以相反的順序來排序
-t 設置排序時所用的分隔字符
-k 指定需要排序的列

-sh-4.2$ cat test.txt 2 3 3 1 5 4 1 5 3 0 4 2 -sh-4.2$ sort -n test.txt 1 5 2 3 3 0 3 1 4 2 5 4 -sh-4.2$ sort -nr test.txt 5 4 4 2 3 1 3 0 2 3 1 5 -sh-4.2$ sort -nk 2 test.txt 3 0 3 1 4 2 2 3 5 4 1 5 -sh-4.2$ sort -nrk 2 test.txt 1 5 5 4 2 3 4 2 3 1 3 0

總結

以上是生活随笔為你收集整理的Linux.2- shell命令(部分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。