1.端口
-
lsof(list open files)
-
lsof -i 端口號:(查看端口是否被占用)
[root@centos-master ~]# lsof -i :22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1097 root 5u IPv4 22183 0t0 TCP *:ssh (LISTEN)
-
-
-
lsof -i 列出當前系統打開文件的工具
[root@centos-master ~]# lsof -i COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME chronyd 825 chrony 6u IPv4 18564 0t0 UDP VM-24-7-centos:323 chronyd 825 chrony 7u IPv6 18565 0t0 UDP VM-24-7-centos:323 NetworkMa 1027 root 25u IPv4 21550 0t0 UDP centos-master:bootpc->_gateway:bootps sshd 1097 root 5u IPv4 22183 0t0 TCP *:ssh (LISTEN) kubelet 1102 root 16u IPv4 29804 0t0 TCP VM-24-7-centos:46279 (LISTEN) kubelet 1102 root 24u IPv4 79483 0t0 TCP centos-master:51190->centos-master:sun-sr-https (SYN_SENT) kubelet 1102 root 25u IPv4 80041 0t0 TCP centos-master:51210->centos-master:sun-sr-https (SYN_SENT) kubelet 1102 root 31u IPv4 80042 0t0 TCP centos-master:51214->centos-master:sun-sr-https (SYN_SENT) kubelet 1102 root 32u IPv6 28518 0t0 TCP *:10250 (LISTEN) kubelet 1102 root 33u IPv4 79415 0t0 TCP centos-master:51168->centos-master:sun-sr-https (SYN_SENT) kubelet 1102 root 36u IPv4 29826 0t0 TCP VM-24-7-centos:10248 (LISTEN) kubelet 1102 root 37u IPv4 78696 0t0 TCP centos-master:51180->centos-master:sun-sr-https (SYN_SENT) nginx 1134 root 8u IPv4 23267 0t0 TCP *:http (LISTEN) nginx 1135 nobody 8u IPv4 23267 0t0 TCP *:http (LISTEN) secu-tcs- 1178 root 9u IPv4 52553 0t0 TCP centos-master:48034->180.97.118.234:nsesrvr (ESTABLISHED) tat_agent 1270 root 17u IPv4 36221 0t0 TCP centos-master:46896->169.254.0.138:d-s-n (ESTABLISHED) kube-cont 2677 root 7u IPv4 32361 0t0 TCP VM-24-7-centos:10257 (LISTEN) kube-cont 2677 root 12u IPv4 80043 0t0 TCP centos-master:51218->centos-master:sun-sr-https (SYN_SENT) kube-sche 2723 root 7u IPv4 33194 0t0 TCP VM-24-7-centos:10259 (LISTEN) kube-sche 2723 root 8u IPv4 79270 0t0 TCP centos-master:51164->centos-master:sun-sr-https (SYN_SENT) kube-sche 2723 root 9u IPv4 79271 0t0 TCP centos-master:51166->centos-master:sun-sr-https (SYN_SENT) kube-sche 2723 root 10u IPv4 78660 0t0 TCP centos-master:51174->centos-master:sun-sr-https (SYN_SENT) kube-sche 2723 root 11u IPv4 78767 0t0 TCP centos-master:51192->centos-master:sun-sr-https (SYN_SENT) kube-sche 2723 root 12u IPv4 79761 0t0 TCP centos-master:51212->centos-master:sun-sr-https (SYN_SENT) kube-sche 2723 root 13u IPv4 79762 0t0 TCP centos-master:51216->centos-master:sun-sr-https (SYN_SENT) kube-sche 2723 root 14u IPv4 80079 0t0 TCP centos-master:51226->centos-master:sun-sr-https (SYN_SENT)
-
-
netstat
-
netstat -tunlp:顯示TCP,udp的端口和進程等相關情況
[root@centos-master ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1097/sshd tcp 0 0 127.0.0.1:46279 0.0.0.0:* LISTEN 1102/kubelet tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 4882/python3 tcp 0 0 127.0.0.1:10248 0.0.0.0:* LISTEN 1102/kubelet tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1134/nginx: master tcp 0 0 127.0.0.1:10257 0.0.0.0:* LISTEN 2677/kube-controlle tcp 0 0 127.0.0.1:10259 0.0.0.0:* LISTEN 2723/kube-scheduler tcp6 0 0 :::10250 :::* LISTEN 1102/kubelet udp 0 0 127.0.0.1:323 0.0.0.0:* 825/chronyd udp6 0 0 ::1:323 :::* 825/chronyd
-
參數
-t (tcp) #僅顯示tcp相關選項 -u (udp) #僅顯示udp相關選項 -n #拒絕顯示別名,能顯示數字的全部轉化為數字 -l #僅列出在Listen(監聽)的服務狀態 -p #顯示建⽴相關鏈接的程序名
-
netstat -tunlp | grep 端口號 :查看端口是否被占用
[wq@centos-master ~]# netstat -tunlp | grep 22 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - [wq@centos-master ~]# netstat -tunlp | grep 80 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
-
2.進程
Linux ps (英文全拼:process status)命令用於顯示當前進程的狀態,類似於 windows 的任務管理器。
ps 的參數非常多, 在此僅列出幾個常用的參數並大略介紹含義
-
ps -A 列出所有的進程
[wq@centos-master ~]# ps -A PID TTY TIME CMD 1 ? 00:00:02 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 rcu_gp 4 ? 00:00:00 rcu_par_gp 6 ? 00:00:00 kworker/0:0H-events_highpri 9 ? 00:00:00 mm_percpu_wq 10 ? 00:00:00 ksoftirqd/0 11 ? 00:00:01 rcu_sched 12 ? 00:00:00 migration/0 13 ? 00:00:00 watchdog/0 14 ? 00:00:00 cpuhp/0 15 ? 00:00:00 cpuhp/1 16 ? 00:00:00 watchdog/1 17 ? 00:00:00 migration/1 18 ? 00:00:00 ksoftirqd/1 20 ? 00:00:00 kworker/1:0H-events_highpri 23 ? 00:00:00 kdevtmpfs
-
ps -w 顯示加寬可以顯示較多的資訊
[wq@centos-master ~]# ps -w PID TTY TIME CMD 24612 pts/13 00:00:00 bash 24889 pts/13 00:00:00 ps
-
ps -au 顯示較詳細的資訊
[wq@centos-master ~]# ps -au USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1292 0.0 0.0 19340 2192 ttyS0 Ss+ 10:01 0:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,960 root 1293 0.0 0.0 16772 1808 tty1 Ss+ 10:01 0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux root 3780 0.0 0.1 28716 5544 pts/1 Ss+ 10:03 0:00 -bash root 4222 0.0 0.1 28584 5272 pts/7 Ss 10:03 0:00 -bash zl 4264 0.0 0.1 26328 5208 pts/8 Ss 10:03 0:00 -bash root 4351 0.0 0.1 28584 5376 pts/9 Ss+ 10:04 0:00 -bash root 5098 0.0 0.1 28584 5160 pts/10 Ss+ 10:05 0:00 -bash root 6354 0.0 0.1 28584 5252 pts/0 Ss+ 10:08 0:00 -bash zl 7119 0.1 0.1 69408 5100 pts/8 S+ 10:10 0:03 top wuya 20055 0.0 0.1 29608 5764 pts/3 Ss+ 10:43 0:00 -bash jc 20137 0.0 0.1 26328 5036 pts/4 Ss 10:43 0:00 -bash jc 20188 0.1 0.1 69408 5128 pts/4 S+ 10:43 0:00 top root 20200 0.0 0.1 138616 6244 pts/7 S 10:43 0:00 su cl cl 20201 0.0 0.1 26352 5148 pts/7 S 10:43 0:00 bash rq 20287 0.0 0.1 26460 5168 pts/5 Ss 10:43 0:00 -bash rq 20334 0.1 0.1 69408 4940 pts/5 S+ 10:43 0:00 top cl 20341 0.1 0.1 69408 5140 pts/7 S+ 10:43 0:00 top yl 20459 0.0 0.1 26460 5000 pts/2 Ss 10:44 0:00 -bash lm 20558 0.0 0.1 26328 5096 pts/6 Ss 10:44 0:00 -bash yl 20584 0.1 0.1 69408 5012 pts/2 S+ 10:44 0:00 top lm 20631 0.1 0.1 69408 4924 pts/6 S+ 10:44 0:00 top wq 20703 0.0 0.1 26460 5276 pts/12 Ss+ 10:44 0:00 -bash xl 22070 0.0 0.1 26460 5124 pts/11 Ss 10:47 0:00 -bash xl 22253 0.1 0.1 69408 5016 pts/11 S+ 10:48 0:00 top wq 24612 0.0 0.1 26460 5272 pts/13 Ss 10:54 0:00 -bash wq 25117 0.0 0.1 62704 4380 pts/13 R+ 10:55 0:00 ps -au
-
ps -elf 顯示詳細的資訊,包括PPID
[wq@centos-master ~]# ps -elf F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD 4 S root 1 0 0 80 0 - 43463 - 10:01 ? 00:00:02 /usr/lib/systemd/systemd --switched-root 1 S root 2 0 0 80 0 - 0 - 10:01 ? 00:00:00 [kthreadd] 1 I root 3 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [rcu_gp] 1 I root 4 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [rcu_par_gp] 1 I root 6 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [kworker/0:0H-events_highpri] 1 I root 9 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [mm_percpu_wq] 1 S root 10 2 0 80 0 - 0 - 10:01 ? 00:00:00 [ksoftirqd/0] 1 I root 11 2 0 80 0 - 0 - 10:01 ? 00:00:01 [rcu_sched] 1 S root 12 2 0 -40 - - 0 - 10:01 ? 00:00:00 [migration/0] 5 S root 13 2 0 -40 - - 0 - 10:01 ? 00:00:00 [watchdog/0] 1 S root 14 2 0 80 0 - 0 - 10:01 ? 00:00:00 [cpuhp/0] 1 S root 15 2 0 80 0 - 0 - 10:01 ? 00:00:00 [cpuhp/1] 5 S root 16 2 0 -40 - - 0 - 10:01 ? 00:00:00 [watchdog/1] 1 S root 17 2 0 -40 - - 0 - 10:01 ? 00:00:00 [migration/1] 1 S root 18 2 0 80 0 - 0 - 10:01 ? 00:00:00 [ksoftirqd/1] 1 I root 20 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [kworker/1:0H-events_highpri] 5 S root 23 2 0 80 0 - 0 - 10:01 ? 00:00:00 [kdevtmpfs] 1 I root 24 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [netns] 1 S root 25 2 0 80 0 - 0 - 10:01 ? 00:00:00 [rcu_tasks_trace] 1 S root 26 2 0 80 0 - 0 - 10:01 ? 00:00:00 [rcu_tasks_rude_] 1 S root 27 2 0 80 0 - 0 - 10:01 ? 00:00:00 [kauditd] 1 S root 28 2 0 80 0 - 0 - 10:01 ? 00:00:00 [khungtaskd] 1 S root 29 2 0 80 0 - 0 - 10:01 ? 00:00:00 [oom_reaper] 1 I root 30 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [writeback] 1 S root 31 2 0 80 0 - 0 - 10:01 ? 00:00:00 [kcompactd0] 1 S root 32 2 0 85 5 - 0 - 10:01 ? 00:00:00 [ksmd] 1 S root 33 2 0 99 19 - 0 - 10:01 ? 00:00:00 [khugepaged] 1 I root 34 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [crypto] 1 I root 35 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [kintegrityd] 1 I root 36 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [kblockd] 1 I root 37 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [blkcg_punt_bio] 1 I root 38 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [tpm_dev_wq] 1 I root 39 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [md] 1 I root 40 2 0 60 -20 - 0 - 10:01 ? 00:00:00 [edac-poller]
-
ps -aux 顯示所有包含其他使用者的行程
[wq@centos-master ~]# ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 173852 10772 ? Ss 10:01 0:02 /usr/lib/systemd/systemd --switched-root --system --droot 2 0.0 0.0 0 0 ? S 10:01 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 10:01 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 10:01 0:00 [rcu_par_gp] root 6 0.0 0.0 0 0 ? I< 10:01 0:00 [kworker/0:0H-events_highpri] root 9 0.0 0.0 0 0 ? I< 10:01 0:00 [mm_percpu_wq] root 10 0.0 0.0 0 0 ? S 10:01 0:00 [ksoftirqd/0] root 11 0.0 0.0 0 0 ? I 10:01 0:01 [rcu_sched] root 12 0.0 0.0 0 0 ? S 10:01 0:00 [migration/0] root 13 0.0 0.0 0 0 ? S 10:01 0:00 [watchdog/0] root 14 0.0 0.0 0 0 ? S 10:01 0:00 [cpuhp/0] root 15 0.0 0.0 0 0 ? S 10:01 0:00 [cpuhp/1] root 16 0.0 0.0 0 0 ? S 10:01 0:00 [watchdog/1] root 17 0.0 0.0 0 0 ? S 10:01 0:00 [migration/1] root 18 0.0 0.0 0 0 ? S 10:01 0:00 [ksoftirqd/1] root 20 0.0 0.0 0 0 ? I< 10:01 0:00 [kworker/1:0H-events_highpri] root 23 0.0 0.0 0 0 ? S 10:01 0:00 [kdevtmpfs] root 24 0.0 0.0 0 0 ? I< 10:01 0:00 [netns] root 25 0.0 0.0 0 0 ? S 10:01 0:00 [rcu_tasks_trace] root 26 0.0 0.0 0 0 ? S 10:01 0:00 [rcu_tasks_rude_] root 27 0.0 0.0 0 0 ? S 10:01 0:00 [kauditd] root 28 0.0 0.0 0 0 ? S 10:01 0:00 [khungtaskd] root 29 0.0 0.0 0 0 ? S 10:01 0:00 [oom_reaper] root 30 0.0 0.0 0 0 ? I< 10:01 0:00 [writeback] root 31 0.0 0.0 0 0 ? S 10:01 0:00 [kcompactd0] root 32 0.0 0.0 0 0 ? SN 10:01 0:00 [ksmd]
-
ps au(x) 輸出格式
USER #進程擁有者 PID #進程ID %CPU #占用的CPU使用率 %MEM #占用的內存使用率 VSZ #占用的虛擬記憶體大小 RSS #占用的記憶體大小 TTY #終端的次要裝置號碼 (minor device number of tty) STAT #該進程的狀態 [D: 無法中斷的休眠狀態 (通常 IO 的進程),R: 正在執行中,S: 靜止狀態,T: 暫停執行, Z: 不存在但暫時無法消除,W: 沒有足夠的記憶體分頁可分配,<: 高優先序的行程,N: 低優先序的行程 L: 有記憶體分頁分配並鎖在記憶體內 (實時系統或捱A I/O)]
START #進程開始時間 TIME #執行時間 COMMAND #所執行的指令 -
顯示指定用戶信息:ps -u 用戶名
[wq@centos-master ~]# ps -u wq PID TTY TIME CMD 20693 ? 00:00:00 systemd 20696 ? 00:00:00 (sd-pam) 20702 ? 00:00:00 sshd 20703 pts/12 00:00:00 bash 24611 ? 00:00:00 sshd 24612 pts/13 00:00:00 bash 43230 ? 00:00:00 sshd 43231 pts/2 00:00:00 bash 43799 pts/2 00:00:00 ps
3.文件查找
-
find
Linux find 命令用來在指定目錄下查找文件。任何位於參數之前的字符串都將被視為欲查找的目錄名。
如果使用該命令時,不設置任何參數,則 find 命令將在當前目錄下查找子目錄與文件。並且將查找到的子目錄和文件全部進行顯示。-
按照名稱查找:find path(路徑) -name 文件名
[root@centos-master ~]# find / -name python3 /etc/alternatives/python3 /var/lib/alternatives/python3 /var/lib/docker/overlay2/de7c794a997536eae02d9ed41d09c344b0770ce39902d2e20ab7cd3929f82cbb/diff/etc/python3 /var/lib/docker/overlay2/de7c794a997536eae02d9ed41d09c344b0770ce39902d2e20ab7cd3929f82cbb/diff/usr/bin/python3 /var/lib/docker/overlay2/de7c794a997536eae02d9ed41d09c344b0770ce39902d2e20ab7cd3929f82cbb/diff/usr/share/lintian/overrides/python3 /var/lib/docker/overlay2/de7c794a997536eae02d9ed41d09c344b0770ce39902d2e20ab7cd3929f82cbb/diff/usr/share/python3 /var/lib/docker/overlay2/de7c794a997536eae02d9ed41d09c344b0770ce39902d2e20ab7cd3929f82cbb/diff/usr/share/doc/python3 /var/lib/docker/overlay2/de7c794a997536eae02d9ed41d09c344b0770ce39902d2e20ab7cd3929f82cbb/diff/usr/lib/python3 /usr/local/qcloud/python/bin/python3 /usr/bin/python3 /usr/share/bash-completion/completions/python3
[root@centos-master ~]# find / -name nginx /usr/local/nginx /usr/local/nginx/sbin/nginx
-
模糊查找:*代表所有,?代表一個字符串,/d表示整形數字,/D表示字符串
[root@centos-master ~]# find / -name ngi* /sys/fs/cgroup/blkio/system.slice/nginx.service /sys/fs/cgroup/pids/system.slice/nginx.service /sys/fs/cgroup/cpu,cpuacct/system.slice/nginx.service /sys/fs/cgroup/memory/system.slice/nginx.service /sys/fs/cgroup/devices/system.slice/nginx.service /sys/fs/cgroup/systemd/system.slice/nginx.service /etc/systemd/system/multi-user.target.wants/nginx.service /usr/local/nginx /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old /usr/local/nginx/logs/nginx.pid /usr/local/nginx/conf/nginx.conf.default /usr/local/nginx/conf/nginx.conf.bak /usr/local/nginx/conf/nginx.conf /usr/lib/python3.6/site-packages/sos/report/plugins/nginx.py /usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/nginx.cpython-36.pyc /usr/lib/python3.6/site-packages/sos/report/plugins/__pycache__/nginx.cpython-36.opt-1.pyc
-
find path -size +大小 :根據文件大小查找
查找根目錄下大於100M的文件
[root@centos-master ~]# find / -size +102400 /root/mq/otp_src_23.0.tar.gz /proc/kcore find: ‘/proc/195343/task/195343/fd/6’: No such file or directory find: ‘/proc/195343/task/195343/fdinfo/6’: No such file or directory find: ‘/proc/195343/fd/5’: No such file or directory find: ‘/proc/195343/fdinfo/5’: No such file or directory /boot/initramfs-0-rescue-18d21b10f101401ea5d08fa347024367.img /boot/initramfs-0-rescue-940f597f0c554e1887f4976a627c7def.img /boot/initramfs-0-rescue-cfa8a069bf8747439d6b6ffa995517d2.img /boot/initramfs-0-rescue-33790f3e0323419f9a055840e9d10b13.img /boot/initramfs-0-rescue-3aa41ff1dee349f9bfbe2ebec1913390.img
-
-
whereis:查找程序位置
Linux whereis命令用於查找文件。 該指令會在特定目錄中查找符合條件的文件。這些文件應屬於原始代碼、二進制文件,或是幫助文件。 該指令只能用於查找二進制文件、源代碼文件和man手冊頁,一般文件的定位需使用locate命令。
-b 只查找二進制文件。 -B<目錄> 只在設置的目錄下查找二進制文件。 -f 不顯示文件名前的路徑名稱。 -m 只查找說明文件。 -M<目錄> 只在設置的目錄下查找說明文件。 -s 只查找原始代碼文件。 -S<目錄> 只在設置的目錄下查找原始代碼文件。 -u 查找不包含指定類型的文件。
[root@centos-master ~]# whereis nginx nginx: /usr/local/nginx
[root@centos-master ~]# whereis python3 python3: /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python3 /usr/lib/python3.6 /usr/lib64/python3.6 /usr/local/lib/python3.6 /usr/include/python3.6m /usr/share/man/man1/python3.1.gz -
which:查找環境變量文件
Linux which命令用於查找文件。 which指令會在環境變量$PATH設置的目錄里查找符合條件的文件。
[root@centos-master ~]# which mv alias mv='mv -i' /usr/bin/mv [root@centos-master ~]# which ls alias ls='ls --color=auto' /usr/bin/ls
-
參數
-n<文件名長度> 指定文件名長度,指定的長度必須大於或等於所有文件中最長的文件名。 -p<文件名長度> 與-n參數相同,但此處的<文件名長度>包括了文件的路徑。 -w 指定輸出時欄位的寬度。 -V 顯示版本信息。
-
4.輸出
-
echo:shell腳本的輸出指令
[root@centos-master ~]# echo "hello world" hello world [root@centos-master ~]# echo "hello Linux" hello Linux
-
printf
printf 命令模仿 C 程序庫(library)里的 printf() 程序。 printf 由 POSIX 標准所定義,因此使用 printf 的腳本比使用 echo 移植性好。 printf 使用引用文本或空格分隔的參數,外面可以在 printf 中使用格式化字符串,還可以制定字符串的寬度、左右對齊方式等。
默認的 printf 不會像 echo 自動添加換行符,我們可以手動添加 \n。-
shell腳本的輸出
shell腳本
#!/bin/bash # author:特昂糖 printf "%-10s %-8s %-4s\n" 姓名 性別 體重kg printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 printf "%-10s %-8s %-4.2f\n" 楊過 男 48.6543 printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876
運行腳本輸出:
[root@centos-master ~]# ./asd.sh 姓名 性別 體重kg 郭靖 男 66.12 楊過 男 48.65 郭芙 女 47.99
-
格式替代符
%s %c %d %f 都是格式替代符,%s 輸出一個字符串,%d 整型輸出,%c 輸出一個字符,%f 輸出實數,以小數形式輸出。 %-10s 指一個寬度為 10 個字符(- 表示左對齊,沒有則表示右對齊),任何字符都會被顯示在 10 個字符寬的字符內,如果不足則自動以空格填充,超過也會將內容全部顯示出來。 %-4.2f 指格式化為小數,其中 .2 指保留2位小數。
shell腳本
#!/bin/bash # author:特昂糖 # format-string為雙引號 printf "%d %s\n" 1 "abc" # 單引號與雙引號效果一樣 printf '%d %s\n' 1 "abc" # 沒有引號也可以輸出 printf %s abcdef # 格式只指定了一個參數,但多出的參數仍然會按照該格式輸出,format-string 被重用 printf %s abc def printf "%s\n" abc def printf "%s %s %s\n" a b c d e f g h i j # 如果沒有 arguments,那么 %s 用NULL代替,%d 用 0 代替 printf "%s and %d \n"
[root@centos-master ~]# ./index.sh 1 abc 1 abc abcdefabcdefabc def a b c d e f g h i j and 0
-
printf 的轉義序列
序列 說明 \a 警告字符,通常為ASCII的BEL字符 \b 后退 \c 抑制(不顯示)輸出結果中任何結尾的換行字符(只在%b格式指示符控制下的參數字符串中有效),而且,任何留在參數里的字符、任何接下來的參數以及任何留在格式字符串中的字符,都被忽略 \f 換頁(formfeed) \n 換行 \r 回車(Carriage return) \t 水平制表符 \v 垂直制表符 \\ 一個字面上的反斜杠字符 \ddd 表示1到3位數八進制值的字符。僅在格式字符串中有效 \0ddd 表示1到3位的八進制值字符 -
終端的輸出
[root@centos-master ~]# printf "我叫:%s,\n我的年齡:%d,\n我的薪資:%f \n" 特昂糖 21 999.99 我叫:特昂糖, 我的年齡:21, 我的薪資:999.990000
-