1、字符編碼轉(zhuǎn)換 iconv命令
https://blog.csdn.net/weixin_42425141/article/details/80940069
2、ftp 上傳下載腳本(親測成功) ---簡版
https://blog.csdn.net/yuchunhai321/article/details/82351089
3、 字符串截取,Shell中的${}、##和%%使用范例
file=/dir1/dir2/dir3/my.file.txt
可以用${ }分別替換得到不同的值:
{file##/}:刪掉最后一個(gè) / 及其左邊的字符串:my.file.txt
{file##.}:刪掉最后一個(gè) . 及其左邊的字符串:txt
{file%%/}:刪掉第一個(gè) / 及其右邊的字符串:(空值)
{file%%.}:刪掉第一個(gè) . 及其右邊的字符串:/dir1/dir2/dir3/my
4、ls -l | awk '/^d/ {print $NF}'
打印出當(dāng)前目錄下所包含的文件夾。
ls -l /home/etluser | awk '/^d/ {print $NF}'
可以添加路徑
5、## linux shell腳本如果不存在創(chuàng)建文件夾
在有的shell(比如linux中的bash)中sleep還支持睡眠(分,小時(shí))
sleep 1 睡眠1秒
sleep 1s 睡眠1秒
sleep 1m 睡眠1分
sleep 1h 睡眠1小時(shí)
7、# [shell腳本實(shí)現(xiàn)取當(dāng)前時(shí)間]
2018.09.30
!bin/bash
time4=$(date "+%Y.%m.%d")
echo $time4
前一天時(shí)間
(date -d last-day +%Y-%m-%d)或者
$(date -d "1 days ago" +%Y-%m-%d)
整數(shù)比較
-eq 等于,如:if [ "b" ]
9、# Linux查看文件編碼格式及文件編碼轉(zhuǎn)換
10、sed
- Sed簡介
sed 是一種在線編輯器,它一次處理一行內(nèi)容。處理時(shí),把當(dāng)前處理的行存儲在臨時(shí)緩沖區(qū)中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有 改變,除非你使用重定向存儲輸出。Sed主要用來自動(dòng)編輯一個(gè)或多個(gè)文件;簡化對文件的反復(fù)操作;編寫轉(zhuǎn)換程序等。以下介紹的是Gnu版本的Sed 3.02。 - 定址
可以通過定址來定位你所希望編輯的行,該地址用數(shù)字構(gòu)成,用逗號分隔的兩個(gè)行數(shù)表示以這兩行為起止的行的范圍(包括行數(shù)表示的那兩行)。如1,3表示1,2,3行,美元符號($)表示最后一行。范圍可以通過數(shù)據(jù),正則表達(dá)式或者二者結(jié)合的方式確定 。
https://www.cnblogs.com/ctaixw/p/5860221.html
11、文件轉(zhuǎn)碼
https://www.cnblogs.com/hnrainll/archive/2011/07/22/2113557.html
https://blog.csdn.net/eroswang/article/details/3847304
12、# linux 搜索文件內(nèi)容并輸出命令 grep、-i、-v ^#
grep /bin/grep
grep -iv [指定字條串] [文件]
在文件中搜索字符串匹配的行并輸出
-i 不區(qū)分大小寫 -v 排除指定字符串
grep -i java /etc/profile
grep -v ^# /etc/profile
不顯示以#開頭的行
13、sed 's/[0-9]//g'是什么意思
去除文本中的所有數(shù)字
[0-9]匹配一個(gè)數(shù)字,
s/A/B/ 將A替換為B
g表示全局替換
14、awk默認(rèn)分隔符是空格(輸出第3列)
awk -F '分隔符' '{print $3 }' 文件名
awk命令之指定多個(gè)字符串或特殊符號為分隔符
https://www.imzcy.cn/2040.html
15、深入理解sed中的-n選項(xiàng)和-p選項(xiàng)的用法
https://blog.csdn.net/qq_44714603/article/details/88222931
16、# sed -i命令詳解
-i :直接修改讀取的文件內(nèi)容,而不是輸出到終端。
17、shell-if表達(dá)式(-f,-d,-s,-r,-w,-x,-eq,-ne,-ge,-gt,-le,-lt )
https://blog.csdn.net/liyyzz33/article/details/84836255
文件表達(dá)式
if [ -f file ] 如果文件存在
if [ -d … ] 如果目錄存在
if [ -s file ] 如果文件存在且非空
if [ -r file ] 如果文件存在且可讀
if [ -w file ] 如果文件存在且可寫
if [ -x file ] 如果文件存在且可執(zhí)行
-h filename 如果文件是軟鏈接,則為真
18、# 【Shell】按行讀取文件內(nèi)容
!/bin/bash
while read line
do
echo $line
done < filename(待讀取的文件)
19、## shell腳本字符串截取的8種方法
21、### [cat <<EOF與cat <<-EOF的區(qū)別]https://blog.csdn.net/apache0554/article/details/45508631?utm_source=blogxgwz5
22、## [shell中的cat和文件分界符(<<EOF)
https://www.cnblogs.com/jinjiangongzuoshi/p/3679098.html
http://www.itdecent.cn/p/df07d8498fa5
23、在linux下shell腳本中if中用到or怎么寫
https://blog.csdn.net/tumblerman/article/details/90385340
-o = or , -a = and
24、# shell腳本----if(數(shù)字條件,字符串條件,字符串為空)
25、查看當(dāng)前文件夾下有多少個(gè)文件
ls -l |wc -l
26、# shell腳本實(shí)現(xiàn)取當(dāng)前時(shí)間
20180930155515
!bin/bash
time2=time2
27、Shell exit命令:退出當(dāng)前進(jìn)程
一般情況下,退出狀態(tài)為 0 表示成功,退出狀態(tài)為非 0 表示執(zhí)行失?。ǔ鲥e(cuò))了。
exit 退出狀態(tài)只能是一個(gè)介于 0~255 之間的整數(shù),其中只有 0 表示成功,其它值都表示失敗。
28、# Linux shell if [ -n ] 正確使用方法
if [ ! -n "$1" ];then
echo "Missing parameter."
exit 1
else
.....
fi
29、shell邏輯運(yùn)算的執(zhí)行規(guī)則:
-a、-o、!等同于&&、||、!----------->與、或、非
30、Linux shell中&,&&,|,||的用法
https://www.cnblogs.com/unknown404/p/10355705.html
cmd1 操作符 cmd2 操作符 cmd3 ......
&:除了最后一個(gè)cmd,前面的cmd均已后臺方式靜默執(zhí)行,執(zhí)行結(jié)果顯示在終端上,個(gè)別的cmd錯(cuò)誤不影響整個(gè)命令的執(zhí)行,全部的cmd同時(shí)執(zhí)行
&&:從左到右順序執(zhí)行cmd,個(gè)別cmd錯(cuò)誤不產(chǎn)生影響
|:各個(gè)cmd同時(shí)在前臺被執(zhí)行,但是除最后的cmd之外,其余的執(zhí)行結(jié)果不會被顯示在終端上
||:從左到右順序執(zhí)行cmd,只有左側(cè)的cmd執(zhí)行出錯(cuò),右邊的cmd才會被執(zhí)行,同時(shí)一旦有cmd被成功執(zhí)行,整個(gè)命令就會結(jié)束,返回終端
31、shell 腳本中&和&&的區(qū)別
a & b 表示a和b同時(shí)運(yùn)行
a && b表示 a運(yùn)行成功后再運(yùn)行b
32、bash shell 清空文件的方法
https://blog.csdn.net/a506681571/article/details/77941982
cat /dev/null > /var/logs/msg.log
或
echo "" > /var/logs/msg.log
33、# vim中跳到第一行和最后一行
gg跳到第一行
shift+g跳到文件最后一行
34、tar打包及打包并壓縮
https://blog.51cto.com/13503302/2066430
https://blog.csdn.net/weixin_41649786/article/details/81268819
35、“tar: Removing leading `/’ from member names”的錯(cuò)誤
https://blog.csdn.net/liuxiao723846/article/details/53104509
https://blog.csdn.net/qq_43413788/article/details/104545008
壓縮:tar -zcPf /backup/WEB_DATA
解壓:tar -xzvPf /a/b/x.gz
36、Linux shell腳本自動(dòng)輸入sudo密碼
echo "admin" | sudo -S xxx
37、字符串切分
echo ${var:0:5}
38、shell腳本運(yùn)行hive -f "XXX.sql" 或者impala-shell保存全部運(yùn)行日志到指定文件
hive -f LOG_DIR/aaa.log
39、# 判斷輸入shell腳本的日期格式是否正確
if echo 1 +%Y%m%d > /dev/null 2>&1
then :;
else
echo "輸入的日期格式不正確,應(yīng)為yyyy-mm-dd";
exit 1
fi
40、Shell中獲取字符串長度的七種方法
https://blog.csdn.net/Jerry_1126/article/details/51835119
利用${#str}來獲取字符串的長度
41、Linux Shell日期比較,數(shù)值比較和字符串比較及相關(guān)
https://blog.csdn.net/helloxiaozhe/article/details/81280095
42、shell腳本中判斷上一個(gè)命令是否執(zhí)行成功
if [ $? -ne 0 ]; then
echo "failed"
else
echo "succeed"
fi
43、if [ -z "sss" ]; then
[ -z STRING ] “STRING” 的長度為零則為真。 字符串為空即NULL時(shí)為真。
44、Linux常用命令簡述--dirname與basename
https://blog.51cto.com/2937761/2090137
[root@liang ~]# dirname /etc/httpd/
/etc
[root@liang ~]# dirname /etc/
/
[root@liang ~]# dirname /etc/httpd/conf/httpd.conf
/etc/httpd/conf
45、# Shell test命令(Shell [])詳解,附帶所有選項(xiàng)及說明
test 是 Shell 內(nèi)置命令,用來檢測某個(gè)條件是否成立。test 通常和 if 語句一起使用,并且大部分 if 語句都依賴 test。
test 命令有很多選項(xiàng),可以進(jìn)行數(shù)值、字符串和文件三個(gè)方面的檢測。
Shell test 命令的用法為:
test expression
當(dāng) test 判斷 expression 成立時(shí),退出狀態(tài)為 0,否則為非 0 值。
test 命令也可以簡寫為[],它的用法為:
[ expression ]
注意[]和expression之間的空格,這兩個(gè)空格是必須的,否則會導(dǎo)致語法錯(cuò)誤。[]的寫法更加簡潔,比 test 使用頻率高。
46、## shell腳本中小數(shù)點(diǎn)的問題
在shell腳本中看到這一行 . ./network-functions ,如果只有一個(gè)小數(shù)點(diǎn)我是理解的,但在第二個(gè)小數(shù)據(jù)點(diǎn)的前面多了一個(gè)空格,然后又出現(xiàn)一個(gè)小數(shù)點(diǎn),求學(xué)習(xí)。謝謝。
格式是這樣的 . ./network-functions 小數(shù)點(diǎn) 空格 小數(shù)點(diǎn) 路徑
第一點(diǎn)相當(dāng)于source 引用,第二個(gè)./xxx 表示當(dāng)前目錄下xxx文件,兩個(gè)點(diǎn) ../xx 表示父目錄
47、Linux Vim 撤銷和恢復(fù)撤銷快捷鍵
u(小寫) undo 的第 1 個(gè)字母,功能是撤銷最近一次對文本做的修改操作。
Ctrl+R Redo 的第 1 個(gè)字母,功能是恢復(fù)最近一次所做的撤銷操作。
U(大寫) 第一次會撤銷對一行文本(光標(biāo)所在行)做過的全部操作,第二次使用該命令會恢復(fù)對該行文本做過的所有操作。
48、command-v,判斷一個(gè)命令是否存在
http://blog.sina.com.cn/s/blog_d9809c3d0102xet2.html
command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
49、Shell中冒號的幾個(gè)作用
https://blog.csdn.net/pahys/article/details/54897856
50、# shell腳本對代碼執(zhí)行時(shí)間的計(jì)時(shí)
start_time=(date +%s)
cost_time=end_time-
((
(($cost_time%60))s"
51、linux shell之?dāng)?shù)字加減(https://blog.csdn.net/CodeHouse/article/details/78336165)
NUM=10
let NUM++
echo {NUM2}
52、Perl字符串比較和整數(shù)比較的區(qū)別詳解
https://www.jb51.net/article/122833.htm
字符串比較操作符
操作符 描述
lt 小于
gt 大于
eq 等于
le 小于等于
ge 大于等于
ne 不等于
對比記憶:Shell if 條件判斷(https://blog.csdn.net/zhan570556752/article/details/80399154)
字符串判斷
[ STRING1 = STRING2 ] 如果兩個(gè)字符串相同則為真 ;
[ STRING1 != STRING2 ] 如果字符串不相同則為真 ;
53、shell 正則
https://www.jb51.net/article/94354.htm
54、## Shell遍歷文件,對每行進(jìn)行正則匹配
pattern="HeartBeat"
tail -n 5 /home/test/log/log_20150205.log | while read line
do
if [[ pattern ]]
then
echo "Match"
else
echo "No match"
fi
echo $line
done
return_value=$(mysql -u用 -p密碼 -h主機(jī) -e "select count(*) from A;")
56、字符串切分
(https://blog.csdn.net/u010003835/article/details/80750003)
string="hello,shell,split,test"
array=({array[@]}
do
echo $var
done
57、獲取數(shù)組元素
n=${nums[2]}
58、Shell數(shù)組追加元素的技巧
https://blog.csdn.net/weixin_44901564/article/details/104552834
輸出數(shù)組全部元素:echo "{#arr[]}
追加元素:arr[${#arr[]}]=b
59、# shell中echo輸出換行的方法
[~]#echo "Hello world.\nHello sea"
Hello world.\nHello sea
[~]#echo -e "Hello world.\nHello sea"
Hello world.
Hello sea
60、按空格切分字符串
str="a b c d e"
arr=($str)
arr[0] is a, arr[1] is b, etc. arr is now an array
61、數(shù)組 – 在shell腳本中排序
https://www.jb51.cc/bash/383870.html
arr=( x11 y12 x21 y22 x31 y32)
arr2=( {arr[@]}"
do
echo "echo "${arr2[@]}"
62、sed -i 就是直接對文本文件進(jìn)行操作的。
(https://www.cnblogs.com/hxing/p/12008354.html)
sed -i 's/原字符串/新字符串/' /home/1.txt
sed -i 's/原字符串/新字符串/g' /home/1.txt
63、Linux shell中2>&1的含義解釋
https://blog.csdn.net/zhaominpro/article/details/82630528
64、 grep -i "test" test.txt 搜索出含有“test” 字符串(-i:不區(qū)分大?。?br> https://www.cnblogs.com/lemon-feng/p/11249461.html
65、awk '{print $2}' 這個(gè)命令
https://blog.csdn.net/qq_32649581/article/details/85285600
$2:表示第二個(gè)字段
print $2 : 打印第二個(gè)字段
awk '{print fileName : 一行一行的讀取指定的文件, 以空格作為分隔符,打印第二個(gè)字段
66、[^0-9]正則的意思
https://zhidao.baidu.com/question/142118234263291525.html
^是匹配字符串bai開始的意思,【0-9】意du思就是只要是zhi范圍0-9里的東dao西都要,或者說只要不是數(shù)字都不要~
67、深入理解sed中的-n選項(xiàng)和-p選項(xiàng)的用法
https://blog.csdn.net/qq_44714603/article/details/88222931
68、linux 字符串拼接,Shell 字符串拼接的實(shí)現(xiàn)示例(https://blog.csdn.net/weixin_28784019/article/details/116634028)
part01="part01"
part02="part02"
str03={part02}
echo ${str03}
69、shell編程中for循環(huán)常見三種用法(1-100求和示例)(https://blog.csdn.net/weixin_46329748/article/details/117513841)
!/bin/bash
for ((i = 100; i >= 0; i--)); do
sleep 3s
echo "(date -d "-""
target_date"
/home/aaron/anaconda3/envs/polish/bin/python /home/aaron/polish_main/main_process.py "$target_date"
done
70、執(zhí)行shell腳本出現(xiàn)“l(fā)et:not found”錯(cuò)誤以及解決辦法(https://blog.csdn.net/wangruifengrr/article/details/122556049)
https://blog.csdn.net/weixin_47957229/article/details/121544960
71、Linux腳本輸出當(dāng)前時(shí)間(https://blog.csdn.net/weixin_44540934/article/details/126935768)
echo $(date +%F)
72、輸出當(dāng)前日期前N天的日期(https://blog.csdn.net/weixin_39756445/article/details/116601729)
for ((i = 100; i >= 0; i--)); do
sleep 3s
echo "(date -d "-""
target_date"
/home/aaron/anaconda3/envs/polish/bin/python /home/aaron/polish_main/main_process.py "$target_date"
done
date-d "+1 day" +%Y%m%d //顯示前一天的日期
date-d "-1 day" +%Y%m%d //顯示后一天的日期
date-d "-1 month" +%Y%m%d //顯示上一月的日期
date-d "+1 month" +%Y%m%d //顯示下一月的日期
date-d "-1 year" +%Y%m%d //顯示前一年的日期
date-d "+1 year" +%Y%m%d //顯示下一年的日期
73、字符串轉(zhuǎn)日期,指定日期減一天(https://blog.csdn.net/xiaoping_1988/article/details/78915411)
https://blog.csdn.net/Linuxprobe18/article/details/95449496
!/bin/bash
for ((i = 120; i >= 0; i--)); do
sleep 3s
echo "$i"
dat=date -d "2023-01-01" +%F
target_date=date -d"$i day ago 2022-11-13" +%Y-%m-%d
target_date=
i"" day" +%Y-%m-%d) # 字符串拼接
echo "target_date"
done
74、