博主经常在工作中使用shell脚本,有些流程比较单一但是由很有规律的操作,往往就是脚本使用的最佳应用场景。
虽然在当下python脚本已经非常的普及了,初入行的朋友可能一想到“脚本”,就只想到“python脚本”。但对于我这个“行业老饼”,shell脚本却还是有一席之地的,毕竟在我们入行那会,还真没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文件输出一行
这是一个真实的应用场景,我就经常遇到,我一般是这样操作:
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$
关键脚本命令:find . -name *.o | xargs echo -n
三、查看当前目录下所有C文件的大小
这个也应经常用的,见案例操作:
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
关键脚本命令: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$
关键脚本命令:cat 1.txt 2.txt 3.txt > all.txt
以上几个常用的脚本操作,你都学会了吗?欢迎讨论。
-
Shell
+关注
关注
1文章
362浏览量
23250 -
脚本
+关注
关注
1文章
385浏览量
14802
发布评论请先 登录
相关推荐
评论