?風123456789~-CSDN博客??
?最近文章閱讀排行榜
?
【爬蟲基礎】第一部分 網絡通訊 P1/3-CSDN博客
【爬蟲基礎】第一部分 網絡通訊-Socket套接字 P2/3-CSDN博客
【Linux專欄】find命令+同步 實驗-CSDN博客
【Linux運維】非root用戶的單向免密登錄_linux 單向免密-CSDN博客
【Linux運維】服務器信息查詢_linux 查詢機器所有信息腳本-CSDN博客
【Oracle專欄】客戶端expdp 實驗驗證-CSDN博客
【Oracle專欄】DBMS_CRYPTO 加密包、AES加解密_oracle aes解密-CSDN博客
1.背景?
? ? ? ? ? ?echo
是一個常用的輸出命令,它可以將指定的字符串或變量輸出到終端。
? ? ? 需求:在跑批腳本結束,通常需要打印當前時間到文件中,方便查看。
? ? ? 本文以Linux 終端輸出字符串 和 時間為例進行實驗,雖然簡單,整理一下。
2. 實驗
2.1 實驗:date
date? ? ? ?
說明:linux 輸入date,默認輸出當前的日期+星期+時間
date? +"格式"? 或 date “+格式”
說明:按制定格式輸出需要的日期及時間
[oracle@neptune ~]$ date
Fri Feb 21 14:16:11 CST 2025
[oracle@neptune ~]$ date "+%Y-%m-%d %H:%M:%S"
2025-02-21 14:16:19
[oracle@neptune ~]$ date +"%Y-%m-%d %H:%M:%S"
2025-02-21 14:16:29
[oracle@neptune ~]$ date "+%Y-%m-%d"
2025-02-21
[oracle@neptune ~]$ date "+%F"
2025-02-21
[oracle@neptune ~]$ date "+%F %T %A"
2025-02-21 14:21:23 Friday
結果截圖:?
2.2 實驗 echo 時間
(1)簡單輸出
輸出年月日:echo $(date +%F)輸出時分:echo $(date +%R)輸出時分秒:echo $(date +%T)輸出星期:echo $(date +%A)
結果截圖:?
(2)輸出多個格式
需要用雙引號包起來,否則報錯.
[oracle@neptune ~]$ echo $(date "+%F %T %A")
2025-02-21 14:26:52 Friday
[oracle@neptune ~]$
[oracle@neptune ~]$ echo $(date +%F %T %A)
date: extra operand ‘%T’
Try 'date --help' for more information.[oracle@neptune ~]$
結果截圖:
(3)組合格式輸出
[oracle@neptune ~]$ echo $(date +"%Y-%m-%d %H:%M:%S")
2025-02-21 14:31:07
[oracle@neptune ~]$ echo $(date +"%Y-%m-%d %H:%M:%S %A")
2025-02-21 14:31:16 Friday
[oracle@neptune ~]$ echo $(date +"%F %T %A")
2025-02-21 14:31:28 Friday
[oracle@neptune ~]$ echo `date`
Fri Feb 21 14:36:38 CST 2025
[oracle@neptune ~]$
[oracle@neptune ~]$ echo `date +%F`
2025-02-21
[oracle@neptune ~]$ echo `date +%F %T`
date: extra operand ‘%T’
Try 'date --help' for more information.[oracle@neptune ~]$ echo `date "+%F %T"`
2025-02-21 14:37:44
[oracle@neptune ~]$
結果截圖:
2.3 實驗 echo 字符串
echo "Hello, World"? ? ?或? echo Hello, World
說明:linux 輸出字符串,直接跟在echo之后即可,可加雙引號 或不加。
echo "Hello," "ZhangSan"
說明:linux 輸出字符串拼接。
first_name="John"
last_name="Doe"
echo "Full name: $first_name $last_name"
拼接多個變量和字符串,只需將它們按順序放在
echo
命令中。echo "Hello, $name"
說明:linux 輸出字符串,同時字符串中有變量。
注意:
?單引號?:其中的內容會被原樣輸出,不會解析變量和特殊字符。
?雙引號?:其中的內容會被解析,變量和特殊字符會被替換為相應的值。
[oracle@neptune ~]$ name=1
[oracle@neptune ~]$ echo "The name is $name ian"
The name is 1 ian
[oracle@neptune ~]$ echo 'The name is $name ian'
The name is $name ian
[oracle@neptune ~]$
(1)字符串無變量?
[oracle@neptune ~]$ echo ss
ss
[oracle@neptune ~]$ echo "ss"
ss
[oracle@neptune ~]$ echo "ss" "你好"
ss 你好
[oracle@neptune ~]$ echo "ss""你好"
ss你好
[oracle@neptune ~]$ echo "ss""你好"",歡迎"
ss你好,歡迎
[oracle@neptune ~]$ echo ss 你好,歡迎
ss 你好,歡迎
[oracle@neptune ~]$ echo ss 你好,歡迎!
ss 你好,歡迎!
結果截圖:
(2)字符串有變量
[oracle@neptune ~]$ name=Zhangsan
[oracle@neptune ~]$ echo name
name
[oracle@neptune ~]$ echo `name`
-bash: name: command not found[oracle@neptune ~]$ echo $name
Zhangsan
[oracle@neptune ~]$ echo "歡迎:"$name
歡迎:Zhangsan
[oracle@neptune ~]$ echo "歡迎:$name"
歡迎:Zhangsan
[oracle@neptune ~]$
?結果截圖:
(3)拼接的幾個變量
echo 后用 $變量 取,多個變量可以用雙引號包起來?。
當變量名與后續字符容易混淆時,可以使用?${}來明確變量的邊界。
[oracle@neptune ~]$ name="Alice"
[oracle@neptune ~]$ greeting="Hello, "
[oracle@neptune ~]$ echo $greeting$name
Hello, Alice
[oracle@neptune ~]$ echo "$greeting$name"
Hello, Alice
[oracle@neptune ~]$ echo "$greeting $name"
Hello, Alice
[oracle@neptune ~]$ echo "$greeting 歡迎 $name"
Hello, 歡迎 Alice
[oracle@neptune ~]$ echo $greeting 歡迎 $name
Hello, 歡迎 Alice
[oracle@neptune ~]$
結果截圖:?
當變量名與后續字符容易混淆時,可以使用
${}
來明確變量的邊界。
[oracle@neptune ~]$ name="Alice"
[oracle@neptune ~]$ echo "The name is ${name}ian"
The name is Aliceian
[oracle@neptune ~]$ echo "The name is $name ian"
The name is Alice ian
[oracle@neptune ~]$ echo "The name is $nameian"
The name is
[oracle@neptune ~]$
?結果截圖:
2.4 實驗 echo 字符串+時間
使用 echo 字符串? $(date "格式") ,可以用雙引號 包起來
[oracle@neptune ~]$ echo "跑批結束!"
跑批結束!
[oracle@neptune ~]$ echo "跑批結束!結束時間為:" `date`
跑批結束!結束時間為: Fri Feb 21 14:58:00 CST 2025
[oracle@neptune ~]$[oracle@neptune ~]$ echo "跑批結束!結束時間為:" $(date "+%F %T %A")
跑批結束!結束時間為: 2025-02-21 15:04:12 Friday[oracle@neptune ~]$ echo "跑批結束!結束時間為: $(date "+%F %T %A")"
跑批結束!結束時間為: 2025-02-21 15:05:08 Friday
[oracle@neptune ~]$
結果截圖:
?3. 在腳本中 echo
#!/bin/bash
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "當前系統時間是:$current_time"
4.相關知識點
4.1 echo -n? ?
表示:不換行。默認是換行的
4.2 echo -e
表示:前面有反斜線的字符將作為轉義字符,但是需要要有單引號 或者 雙引號?包含。
[oracle@neptune ~]$ echo \n\n 1234
nn 1234
[oracle@neptune ~]$ echo -e \n\n 1234
nn 1234
[oracle@neptune ~]$ echo -e '\n\n 1234'1234
[oracle@neptune ~]$ echo -e '\n\n' 12341234
[oracle@neptune ~]$
[oracle@neptune ~]$ echo -e "\\\\" 1234
\ 1234
[oracle@neptune ~]$ echo -e "\\ \\" 1234
\ \ 1234
[oracle@neptune ~]$
結果截圖:
4.3設置文本顏色的轉義符
\e[<格式代碼>m?? ? 或者? ??\033[<格式代碼>m
說明:基本上是夾在 "\e["(轉義開方括號)和 "m" 之間數字值。指定一個以上的格式代碼(數字),則用分號將他們分開。
\e[A;B;Cm? ? ? ? ? ? 或者? ?\033[A;B;Cm??
說明:A 為格式控制,編號0~9;
? ? ? ? ? B?為字體顏色,編號30~37;
? ? ? ? ? C?為背景色,編號40~47。
? ? ? ? ? 可通過 \e[0m? 或 \033[0m 關閉顏色輸出;?\e[A;B;Cm 內容 \e[0m
\e? 即 \033,? 結尾是 \e[0m 清理格式
1) 格式代碼:A? 0-9?
2) 字體顏色 B 30-27? | 背景顏色 C? 40-47
?echo -e '\e[4;31;43m 我是紅色字體 黃色背景 下劃線!\e[0m'
[oracle@neptune ~]$ echo -e '\e[4;31;43m 我是紅色字體 黃色背景 下劃線!\e[0m'我是紅色字體 黃色背景 下劃線!
[oracle@neptune ~]$ echo 1
1
截圖:
?
如果在結尾不關格式,則為以下結果:
本節實驗結束:ok??
項目管理--相關知識???
項目管理-項目績效域1/2-CSDN博客
項目管理-項目績效域1/2_八大績效域和十大管理有什么聯系-CSDN博客
項目管理-項目績效域2/2_績效域 團不策劃-CSDN博客
高項-案例分析萬能答案(作業分享)-CSDN博客
項目管理-計算題公式【復習】_項目管理進度計算題公式:樂觀-CSDN博客
項目管理-配置管理與變更-CSDN博客
項目管理-項目管理科學基礎-CSDN博客
項目管理-高級項目管理-CSDN博客
項目管理-相關知識(組織通用治理、組織通用管理、法律法規與標準規范)-CSDN博客
Oracle其他文檔,希望互相學習,共同進步
Oracle-找回誤刪的表數據(LogMiner 挖掘日志)_oracle日志挖掘恢復數據-CSDN博客
oracle 跟蹤文件--審計日志_oracle審計日志-CSDN博客
ORA-12899報錯,遇到數據表某字段長度奇怪現象:“Oracle字符型,長度50”但length查卻沒有50_varchar(50) oracle 超出截斷-CSDN博客
EXP-00091: Exporting questionable statistics.解決方案-CSDN博客
Oracle 更換監聽端口-CSDN博客