shell -> read builtin

本文详细介绍了如何在sh脚本中实现用户交互,并处理用户可能输入的多个回车操作,确保脚本流程的稳定性和用户友好性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做了一个sh脚本,里头会有一些和用户交互的地方,需要用户回答一些问题,以执行下一步(通过read读取用户输入,有时候用户输入回车,代表使用缺省答 案)
问题是在脚本执行过程中,用户可能会有意无意的输入很多个我不想要的回车.

 

function prompt_answer(){
    prompt="${1}"
    variable="${2}"
    while read  -t1 dummy_vars ; do
        :
    done   

    read -p "${prompt}" -r "${variable}"
}

# 从当前用户主目录查找目录的函数 # 参数1: 要查找的目录名 define find_in_home $(shell \ found_dir=$$(find "$$HOME" -type d -name "$(1)" -print -quit 2>/dev/null); \ if [ -n "$$found_dir" ]; then \ echo "$$found_dir"; \ else \ echo "DIRECTORY_NOT_FOUND"; \ fi \ ) endef # 使用示例(查找用户主目录下的 Documents 目录) TARGET_DIR := lite-user-runtime-env ENV_DIR := $(call find_in_home,$(TARGET_DIR)) ifeq ($(ENV_DIR),DIRECTORY_NOT_FOUND) $(error Directory "$(TARGET_DIR)" not found!!) else $(info ENV_DIR = $(ENV_DIR)) endif # Kconfig 配置系统相关 KCONFIG_CONFIG ?= $(CURDIR)/.config KCONFIG_AUTOHEADER ?= $(CURDIR)/autoconf.h KCONFIG_AUTOCONF ?= $(CURDIR)/autoconf.h # 检查 Kconfig 工具是否存在 ifeq ($(shell command -v kconfig-mconf 2>/dev/null),) $(error "kconfig-frontends not found, please install it first (sudo apt install kconfig-frontends)") endif KCONFIG_TOOL := $(shell command -v kconfig-conf 2>/dev/null) ifndef KCONFIG_TOOL $(error "kconfig-conf not found! Please install kconfig-frontends (sudo apt install kconfig-frontends)") endif CROSS = $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi- CREAT_TOOL = $(ENV_DIR)/bin/create-sp-img CC = gcc INC = -I . -I ./include -I ../posix/include INC += -I $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include #cflag = -g ${cflags} -Wall -fPIC -fno-asynchronous-unwind-tables -fno-builtin-function -mcpu=cortex-m3 -mthumb -nostdinc #inc += -I $(SPBASEDIR)/vsoc/$(os_type)/include/ -I . #inc += -I $(SPBASEDIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include # CFLAG = -g -Wall -fPIC -fno-asynchronous-unwind-tables -mcpu=cortex-a5 -mtune=generic-armv7-a -mthumb -mfloat-abi=soft -Os # CFLAG = -g -Os -Wall -fPIC -fno-asynchronous-unwind-tables -mcpu=cortex-a5 -mtune=generic-armv7-a -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard #-ffreestanding 独立环境 CFLAG += -include $(KCONFIG_AUTOHEADER) CFLAG += -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections CFLAG += -fPIE -Wall -fno-builtin-function -Werror=implicit -flto #lflag = -g -pie -T n58.ld.lds -Os --no-warn-rwx-segments --start-group $(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/thumb/v7-m/nofp/libgcc.a ../posix/lib/libposix.a --end-group #lflag = ../src/raw.o ../src/dns_server.o ../src/exit.o ../src/memset.o ../src/network.o ../src/strncasecmp.o ../src/timerfd.o ../src/vfs_device.o ../src/vfs.o lflag += -g -pie -T n58.ld.lds -Os -Wl,--no-warn-rwx-segments -flto -mcpu=cortex-m3 -mthumb -nostartfiles -nostdlib lflag += -Wl,--start-group,../posix/lib/libposix.a,$(ENV_DIR)/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/thumb/v7-m/nofp/libgcc.a,--end-group lflag += -Wl,-Bsymbolic,-gc-sections LD = gcc objs = main.o websocket_client.o websocket_client_posix.o uart_audio.o crc32_new.o hdlc_byte.o var_buffer_fifo.o tiny_json.o gpio.o led.o pwrkey.o adc.o rtc.o target = test image = $(target).spimg # Kconfig 配置目标:打开图形化配置界面 menuconfig: Kconfig @echo "Starting Kconfig menuconfig..." kconfig-mconf $< --output $(KCONFIG_CONFIG) @# 配置后立即生成头文件 $(MAKE) generate_header generate_header: $(KCONFIG_CONFIG) @echo "Generating autoconf.h from .config..." $(KCONFIG_TOOL) --silentoldconfig $(CURDIR)/Kconfig $(KCONFIG_CONFIG) --header=$(KCONFIG_AUTOHEADER) @test -f $(KCONFIG_AUTOHEADER) || (echo "Error: Failed to generate autoconf.h"; exit 1) @echo "Successfully generated $(KCONFIG_AUTOHEADER)" $(KCONFIG_AUTOHEADER): $(KCONFIG_CONFIG) @if [ ! -f "$(KCONFIG_CONFIG)" ]; then \ $(MAKE) menuconfig; \ else \ $(MAKE) generate_header; \ fi .c.o: $(CROSS)$(CC) -c $< $(INC) $(CFLAG) all:$(KCONFIG_AUTOHEADER) $(target) $(image) @echo "Build completed successfully" $(target):$(objs) $(CROSS)$(LD) -o $@ $^ $(lflag) $(image):$(target) $(CREAT_TOOL) $(target) host armm3 > /dev/null clean: -rm -fv $(target) -rm -fv *.o -rm -fv *.spimg spvsoc@spvsoc-vm:~/4G_8006/vsoc_posix_env$ make clean ;make make clean -C src make[1]: 进入目录“/home/spvsoc/4G_8006/vsoc_posix_env/src” ENV_DIR = /home/spvsoc/lite-user-runtime-env rm -f *.o libposix.a rm -f .depend make[1]: 离开目录“/home/spvsoc/4G_8006/vsoc_posix_env/src” make clean -C test make[1]: 进入目录“/home/spvsoc/4G_8006/vsoc_posix_env/test” ENV_DIR = /home/spvsoc/lite-user-runtime-env rm -fv test rm -fv *.o rm -fv *.spimg make[1]: 离开目录“/home/spvsoc/4G_8006/vsoc_posix_env/test” rm posix -rf make install -C src make[1]: 进入目录“/home/spvsoc/4G_8006/vsoc_posix_env/src” ENV_DIR = /home/spvsoc/lite-user-runtime-env /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c dns_server.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c exit.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c memset.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c network.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c raw.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c strncasecmp.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c timerfd.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c vfs.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc -g -Wall -Os -fno-asynchronous-unwind-tables -mcpu=cortex-m3 -mthumb -nostdinc -nostdlib -ffreestanding -ffunction-sections -fdata-sections -fPIE -fno-builtin-function -Werror=implicit -flto -I ./ -I include/ -I /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/lib/gcc/arm-none-eabi/13.2.1/include -c vfs_device.c /home/spvsoc/lite-user-runtime-env/toolschain/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc-ar rcs libposix.a dns_server.o exit.o memset.o network.o raw.o strncasecmp.o timerfd.o vfs.o vfs_device.o rm ../posix -rf mkdir ../posix cp include ../posix -rf mkdir ../posix/lib cp libposix.a ../posix/lib make[1]: 离开目录“/home/spvsoc/4G_8006/vsoc_posix_env/src” make -C test make[1]: 进入目录“/home/spvsoc/4G_8006/vsoc_posix_env/test” ENV_DIR = /home/spvsoc/lite-user-runtime-env Starting Kconfig menuconfig... kconfig-mconf Kconfig --output /home/spvsoc/4G_8006/vsoc_posix_env/test/.config *** End of the configuration. *** Execute 'make' to start the build or try 'make help'. make generate_header make[2]: 进入目录“/home/spvsoc/4G_8006/vsoc_posix_env/test” ENV_DIR = /home/spvsoc/lite-user-runtime-env Generating autoconf.h from .config... /usr/bin/kconfig-conf --silentoldconfig /home/spvsoc/4G_8006/vsoc_posix_env/test/Kconfig /home/spvsoc/4G_8006/vsoc_posix_env/test/.config --header=/home/spvsoc/4G_8006/vsoc_posix_env/test/autoconf.h /usr/bin/kconfig-conf: 未识别的选项 "--header=/home/spvsoc/4G_8006/vsoc_posix_env/test/autoconf.h" Usage: /usr/bin/kconfig-conf [-s] [option] <kconfig-file> [option] is _one_ of the following: --listnewconfig List new options --oldaskconfig Start a new configuration using a line-oriented program --oldconfig Update a configuration using a provided .config as base --silentoldconfig Same as oldconfig, but quietly, additionally update deps --olddefconfig Same as silentoldconfig but sets new symbols to their default value --oldnoconfig An alias of olddefconfig --defconfig <file> New config with default defined in <file> --savedefconfig <file> Save the minimal current configuration to <file> --allnoconfig New config where all options are answered with no --allyesconfig New config where all options are answered with yes --allmodconfig New config where all options are answered with mod --alldefconfig New config with all symbols set to default --randconfig New config with random answer to all options make[2]: *** [Makefile:75:generate_header] 错误 1 make[2]: 离开目录“/home/spvsoc/4G_8006/vsoc_posix_env/test” make[1]: *** [Makefile:71:menuconfig] 错误 2 make[1]: 离开目录“/home/spvsoc/4G_8006/vsoc_posix_env/test” make: *** [Makefile:3:all] 错误 2
最新发布
08-08
speech_recognition.elf C:\WINDOWS\system32\cmd.exe /C "cd . && C:\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=C:/TPC/IOT/IoT_Pad_MIC_Test/AI_Pad_Test/Src/build/speech_recognition.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\speech_recognition.elf.rsp -o speech_recognition.elf && cd ." C:/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(esp32_s3_szp.c.obj): in function `bsp_i2c_init': C:/TPC/IOT/IoT_Pad_MIC_Test/AI_Pad_Test/Src/main/esp32_s3_szp.c:9: multiple definition of `bsp_i2c_init'; esp-idf/main/libmain.a(main.c.obj):C:/TPC/IOT/IoT_Pad_MIC_Test/AI_Pad_Test/Src/components/PeripheralInit/I2C/I2C.c:29: first defined here collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed. Traceback (most recent call last): File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf.py", line 855, in <module> main() File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf.py", line 745, in main cli(argv, prog_name=PROG, complete_var=SHELL_COMPLETE_VAR) File "C:\Espressif\python_env\idf5.4_py3.11_env\Lib\site-packages\click\core.py", line 1161, in __call__ return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Espressif\python_env\idf5.4_py3.11_env\Lib\site-packages\click\core.py", line 1082, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "C:\Espressif\python_env\idf5.4_py3.11_env\Lib\site-packages\click\core.py", line 1729, in invoke return _process_result(rv) ^^^^^^^^^^^^^^^^^^^ File "C:\Espressif\python_env\idf5.4_py3.11_env\Lib\site-packages\click\core.py", line 1666, in _process_result value = ctx.invoke(self._result_callback, value, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Espressif\python_env\idf5.4_py3.11_env\Lib\site-packages\click\core.py", line 788, in invoke return __callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf.py", line 640, in execute_tasks task(ctx, global_args, task.action_args) File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf.py", line 212, in __call__ self.callback(self.name, context, global_args, **action_args) File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf_py_actions\core_ext.py", line 50, in build_target run_target(target_name, args, force_progression=GENERATORS[args.generator].get('force_progression', False)) File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf_py_actions\tools.py", line 499, in run_target RunTool(generator_cmd[0], generator_cmd + [target_name], args.build_dir, env, custom_error_handler, hints=not args.no_hints, File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf_py_actions\tools.py", line 333, in __call__ for hint in generate_hints(stderr_output_file, stdout_output_file): File "C:\Espressif\frameworks\esp-idf-v5.4\tools\idf_py_actions\tools.py", line 267, in generate_hints yield from generate_hints_buffer(file.read(), hints) ^^^^^^^^^^^ UnicodeDecodeError: 'cp932' codec can't decode byte 0x84 in position 162859: illegal multibyte sequence
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值