博主經(jīng)常在工作中使用shell腳本,有些流程比較單一但是由很有規(guī)律的操作,往往就是腳本使用的最佳應(yīng)用場景。
雖然在當下python腳本已經(jīng)非常的普及了,初入行的朋友可能一想到“腳本”,就只想到“python腳本”。但對于我這個“行業(yè)老餅”,shell腳本卻還是有一席之地的,畢竟在我們?nèi)胄心菚€真沒python什么事!
本文呢,主要是給大家介紹幾個我在工作中常用的,但又非常有用且操作高效的腳本,感興趣的可以往下一看。
一、搜索想要的C文件并拷貝到一個指定的目錄
這個案例很有用,我直接上操作指引:
recan@ubuntu:~/win_share_workspace/test_share$ find . -name *.c
./test_gcc/pass_array2_test.c
./test_gcc/test.c
./test_jd_paper_test/test.c
./test_python_gcc_map/main.c
./test_python_gcc_map/libf.c
./test_python_gcc_map/libd.c
./test_python_gcc_map/libe.c
./test_python_gcc_map/libb.c
./test_python_gcc_map/liba.c
./master_mode_sdk/app/bl2028n_linkkitapp/app_entry.c
./master_mode_sdk/app/tg7100c_linkkitapp/app_entry.c
./master_mode_sdk/app/sv6266_linkkitapp/app_entry.c
./jx_sdk_config/jx_sdk_config.c
./jx_sdk_config/cJSON.c
./jx_sdk_config/main.c
./test_coredump/test_cordump.c
./tcp_echo/tcp_server.c
./tcp_echo/tcp_client.c
./test_aligned_macro/main.c
./test_gcc_test/test.c
./test_for_loop/test.c
./test_bin2array/bin2array.c
./test_cli_for_linux/src/cli_dumpsys.c
./test_cli_for_linux/src/jx_os_cli.c
./test_cli_for_linux/src/cli_default_command.c
./test_cli_for_linux/src/cli.c
./test_cli_for_linux/src/cli_adapt.c
./test_cli_for_linux/tcp_server.c
./test_cli_for_linux/adapter_for_linux/main.c
./test_cli_for_linux/adapter_for_linux/uart.c
./test_cli_for_linux/adapter_for_linux/osal.c
./test_hexdump/test_hexdump.c
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ find . -name *.c | xargs -i cp -rf {} /tmp/test_share/
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ ls /tmp/test_share/
app_entry.c cJSON.c cli.c cli_dumpsys.c jx_sdk_config.c libb.c libe.c main.c pass_array2_test.c tcp_server.c test_cordump.c uart.c
bin2array.c cli_adapt.c cli_default_command.c jx_os_cli.c liba.c libd.c libf.c osal.c tcp_client.c test.c test_hexdump.c
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
主要腳本命令:find . -name *.c | xargs -i cp -rf {} /tmp/test_share/
二、編譯鏈接的時候把所有的object文件輸出一行
這是一個真實的應(yīng)用場景,我就經(jīng)常遇到,我一般是這樣操作:
recan@ubuntu:~/win_share_workspace/test_share$ find . -name *.o
./test_makefile_paper/bar.o
./test_gcc_test/test.o
./test_for_loop/test.o
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ find . -name *.o | xargs echo -n
./test_makefile_paper/bar.o ./test_gcc_test/test.o ./test_for_loop/test.o
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
關(guān)鍵腳本命令:find . -name *.o | xargs echo -n
三、查看當前目錄下所有C文件的大小
這個也應(yīng)經(jīng)常用的,見案例操作:
recan@ubuntu:~/win_share_workspace/test_share$ find . -name *.c | xargs ls -alh
-rwxr-xr-x 1 recan system 64K Jul 19 2021 ./jx_sdk_config/cJSON.c
-rwxr-xr-x 1 recan system 6.6K Jul 22 2021 ./jx_sdk_config/jx_sdk_config.c
-rwxr-xr-x 1 recan system 472 Jul 21 2021 ./jx_sdk_config/main.c
-rwxr-xr-x 1 recan recan 0 Jul 12 2021 ./master_mode_sdk/app/bl2028n_linkkitapp/app_entry.c
-rwxr-xr-x 1 recan recan 0 Jul 12 2021 ./master_mode_sdk/app/sv6266_linkkitapp/app_entry.c
-rwxr-xr-x 1 recan recan 0 Jul 12 2021 ./master_mode_sdk/app/tg7100c_linkkitapp/app_entry.c
-rwxr-xr-x 1 recan recan 5.3K Jan 15 2022 ./tcp_echo/tcp_client.c
-rwxr-xr-x 1 recan recan 6.9K Jan 15 2022 ./tcp_echo/tcp_server.c
-rw-r--r-- 1 recan recan 1.1K Aug 4 2021 ./test_aligned_macro/main.c
-rwx------ 1 recan system 1.9K Aug 24 2021 ./test_bin2array/bin2array.c
-rwx------ 1 recan system 1.3K Jan 16 2022 ./test_cli_for_linux/adapter_for_linux/main.c
-rwx------ 1 recan system 857 Jan 15 2022 ./test_cli_for_linux/adapter_for_linux/osal.c
-rwx------ 1 recan system 1.4K Jan 15 2022 ./test_cli_for_linux/adapter_for_linux/uart.c
-rwx------ 1 recan system 2.1K Jan 15 2022 ./test_cli_for_linux/src/cli_adapt.c
-rwx------ 1 recan system 27K Jan 15 2022 ./test_cli_for_linux/src/cli.c
-rwx------ 1 recan system 8.0K Nov 9 2021 ./test_cli_for_linux/src/cli_default_command.c
-rwx------ 1 recan system 1.3K Nov 9 2021 ./test_cli_for_linux/src/cli_dumpsys.c
-rwx------ 1 recan system 1.6K Dec 13 2021 ./test_cli_for_linux/src/jx_os_cli.c
-rwxr-xr-x 1 recan system 9.1K Jan 16 2022 ./test_cli_for_linux/tcp_server.c
-rwx------ 1 recan system 626 Aug 18 2021 ./test_coredump/test_cordump.c
-rw-r--r-- 1 recan system 768 Jan 27 2022 ./test_for_loop/test.c
-rw-r--r-- 1 recan system 817 Sep 7 2021 ./test_gcc/pass_array2_test.c
-rwxr-xr-x 1 recan recan 233 Jul 6 2021 ./test_gcc/test.c
-rw-r--r-- 1 recan recan 87 Jun 11 2021 ./test_gcc_test/test.c
-rwx------ 1 recan system 1.8K Sep 8 2021 ./test_hexdump/test_hexdump.c
-rwx------ 1 recan system 1.7K Aug 31 2021 ./test_jd_paper_test/test.c
-rw-r--r-- 1 recan system 225 Aug 31 2021 ./test_python_gcc_map/liba.c
-rw-r--r-- 1 recan system 225 Aug 31 2021 ./test_python_gcc_map/libb.c
-rw-r--r-- 1 recan system 225 Aug 31 2021 ./test_python_gcc_map/libd.c
-rw-r--r-- 1 recan system 225 Aug 31 2021 ./test_python_gcc_map/libe.c
-rw-r--r-- 1 recan system 225 Aug 31 2021 ./test_python_gcc_map/libf.c
-rw-r--r-- 1 recan system 492 Aug 31 2021 ./test_python_gcc_map/main.c
關(guān)鍵腳本命令:find . -name *.c | xargs ls -alh
四、把幾個文本文件按順序拼接起來
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ echo 111111 > 1.txt
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ echo 222222 > 2.txt
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ echo 333333 > 3.txt
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ cat 1.txt 2.txt 3.txt > all.txt
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$
recan@ubuntu:~/win_share_workspace/test_share$ cat all.txt
111111
222222
333333
recan@ubuntu:~/win_share_workspace/test_share$
關(guān)鍵腳本命令:cat 1.txt 2.txt 3.txt > all.txt
以上幾個常用的腳本操作,你都學(xué)會了嗎?歡迎討論。
-
Shell
+關(guān)注
關(guān)注
1文章
363瀏覽量
23257 -
腳本
+關(guān)注
關(guān)注
1文章
387瀏覽量
14811
發(fā)布評論請先 登錄
相關(guān)推薦
評論