执行 ./xxx.sh 后发生了什么

本文深入解析了Shell脚本的执行流程,包括别名、内建命令和PATH搜索机制,阐述了如何通过type命令判断命令类型,以及shell脚本的三种调用方式和执行特性。

问题描述

有下面一段 shell 脚本

ubuntu>echo xxx.sh
#!/bin/bash 
echo 'hello world'
ubuntu>chmod a+x xxx.sh 
ubnutu>./xxx.sh 

当输入 ./xxx.sh` 回车后,发生了什么?

问题分析

这里涉及到 bash 脚本的执行过程。其实我们在命令行中数据只是一个一个的字符串,这些字符串会交给 bash 程序处理。

  1. 先检查 alias(别名)配置中是否有对应的命令别名。如果命令别名存在,则执行,否则执行 2 .
  2. 查询内建命令,如果内建命令中存在,则执行,否则执行 3 .
  3. 在 PATH 路径中查找命令,如果 PATH 路径中找到了,则执行,否则保存,-bash:某某命令: command not found

扩展

  • 如何查看命令的类型——也就是如何判断一个命令是 alias 别名还是内建命令还是PATH 里面配置的命令。
  • 大家都知道,在 Linux 中式没有文件后缀名字的,那么 Linux 如何区分文件类型的呢?记住there is shell , there is way ,这就是 file 命令。

从 man type 中可以看到命令可以分问四类:

  • alias
  • keyword
  • function
  • builtin

请看:

[centos]$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls

-a 选项会从 alias shell-builtin PATH 中全部找出来。

file 会按照 systemfile test/magic test/language test 的顺序检测文件类型。
所以我们在写脚本的时候最好在最前面加上:

#!/bin/bash 

shell 脚本的三种调用方式

调用 shell 脚本有三种方式:

  • ./xxx.sh
  • bash xxx.sh or sh xxx.sh
  • . xxx.sh or source xxx.sh
shell 脚本的执行方式shell 文件是否需要执行权限是否启动新的进程
./xxx.sh
. xxx.sh or source xxx.sh
bash xxx.sh or sh xxx.sh
[10/12] Linking CXX executable xiaozhi.elf FAILED: xiaozhi.elf C:\Windows\system32\cmd.exe /C "cd . && D:\Espressif\tools\xtensa-esp-elf\esp-14.2.0_20241119\xtensa-esp-elf\bin\xtensa-esp32s3-elf-g++.exe -mlongcalls -fno-builtin-memcpy -fno-builtin-memset -fno-builtin-bzero -fno-builtin-stpcpy -fno-builtin-strncpy -Wl,--cref -Wl,--defsym=IDF_TARGET_ESP32S3=0 -Wl,--Map=D:/wxq/e/ge/proj/ai/xiaozhi-esp32/build/xiaozhi.map -Wl,--no-warn-rwx-segments -Wl,--orphan-handling=warn -fno-rtti -fno-lto -Wl,--gc-sections -Wl,--warn-common -T esp32s3.peripherals.ld -T esp32s3.rom.ld -T esp32s3.rom.api.ld -T esp32s3.rom.bt_funcs.ld -T esp32s3.rom.libgcc.ld -T esp32s3.rom.wdt.ld -T esp32s3.rom.version.ld -T esp32s3.rom.newlib.ld -T memory.ld -T sections.ld @CMakeFiles\xiaozhi.elf.rsp -o xiaozhi.elf && cd ." D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/main/libmain.a(wifi_board.cc.obj):(.literal._ZN9WifiBoard12GetBoardJsonB5cxx11Ev+0x8): undefined reference to `WifiStation::ssid_[abi:cxx11]' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation36softap_get_current_connection_numberEv+0x0): undefined reference to `WifiStation::gl_sta_list' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x10): undefined reference to `WifiStation::ble_is_connected' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x34): undefined reference to `WifiStation::gl_sta_connected' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x38): undefined reference to `WifiStation::gl_sta_bssid' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x3c): undefined reference to `WifiStation::gl_sta_ssid' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x40): undefined reference to `WifiStation::gl_sta_ssid_len' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x44): undefined reference to `WifiStation::gl_sta_got_ip' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x48): undefined reference to `WifiStation::gl_sta_is_connecting' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x4c): undefined reference to `WifiStation::gl_sta_conn_info' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x58): undefined reference to `WifiStation::sta_config' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x5c): undefined reference to `WifiStation::sta_config' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x68): undefined reference to `WifiStation::sta_config' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x70): undefined reference to `WifiStation::ap_config' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation22example_event_callbackE20esp_blufi_cb_event_tP20esp_blufi_cb_param_t+0x78): undefined reference to `WifiStation::ap_config' D:/Espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/bin/ld.exe: esp-idf/78__esp-wifi-connect/lib78__esp-wifi-connect.a(wifi_station.cc.obj):(.literal._ZN11WifiStation14IpEventHandlerEPvPKclS0_+0xc): undefined reference to `WifiStation::password_[abi:cxx11]'
06-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值