Bash
執行 for..in 並觸摸時收到消息“文件名太長”
我想觸摸目錄中的每個文件:
files=$(ls -a "node_modules/suman-types/dts") echo "files $files"; for file in "$files"; do echo "touching file $file"; touch "node_modules/suman-types/dts/$file"; done
但在執行之後,我得到:
inject.d.ts injection.d.ts integrant-value-container.d.ts it.d.ts reporters.d.ts runner.d.ts suman-utils.d.ts suman.d.ts table-data.d.ts test-suite-maker.d.ts test-suite.d.ts: File name too long
那個“文件名太長”的消息是關於什麼的?
更新#1
我將腳本更改為:
files=$(find "node_modules/suman-types/dts" -name "*.d.ts") for file in "$files"; do echo "touching file $file"; touch "$file"; done touch "node_modules/suman-types"
但後來我明白了:
$ ./types-touch.sh touching file node_modules/suman-types/dts/after-each.d.ts node_modules/suman-types/dts/after.d.ts node_modules/suman-types/dts/before-each.d.ts node_modules/suman-types/dts/before.d.ts node_modules/suman-types/dts/describe.d.ts node_modules/suman-types/dts/global.d.ts node_modules/suman-types/dts/index-init.d.ts node_modules/suman-types/dts/inject.d.ts node_modules/suman-types/dts/injection.d.ts node_modules/suman-types/dts/integrant-value-container.d.ts node_modules/suman-types/dts/it.d.ts node_modules/suman-types/dts/reporters.d.ts node_modules/suman-types/dts/runner.d.ts node_modules/suman-types/dts/suman-utils.d.ts node_modules/suman-types/dts/suman.d.ts node_modules/suman-types/dts/table-data.d.ts node_modules/suman-types/dts/test-suite-maker.d.ts node_modules/suman-types/dts/test-suite.d.ts touch: node_modules/suman-types/dts/after-each.d.ts node_modules/suman-types/dts/after.d.ts node_modules/suman-types/dts/before-each.d.ts node_modules/suman-types/dts/before.d.ts node_modules/suman-types/dts/describe.d.ts node_modules/suman-types/dts/global.d.ts node_modules/suman-types/dts/index-init.d.ts node_modules/suman-types/dts/inject.d.ts node_modules/suman-types/dts/injection.d.ts node_modules/suman-types/dts/integrant-value-container.d.ts node_modules/suman-types/dts/it.d.ts node_modules/suman-types/dts/reporters.d.ts node_modules/suman-types/dts/runner.d.ts node_modules/suman-types/dts/suman-utils.d.ts node_modules/suman-types/dts/suman.d.ts node_modules/suman-types/dts/table-data.d.ts node_modules/suman-types/dts/test-suite-maker.d.ts node_modules/suman-types/dts/test-suite.d.ts: No such file or directory
您的問題源於將所有
ls
輸出擷取到一個名為files
. 該變數看起來像:filename1\nfilename2\nfilename3\n...
親自查看:
echo "$files" | od -c
你真正做的是在一個非常長的字元串上循環一次,該字元串對應於一個不存在的文件。你得到的錯誤資訊量很小——它告訴你這個長的文件名字元串不存在。
要觸摸目錄中的每個文件,只需使用 shell globbing 並執行
touch
:touch node_modules/suman-types/dts/*
或一一觸摸:
for file in node_modules/suman-types/dts/*; do touch "$file"; done
或找到它們並觸摸它們:
find node_modules/suman-types/dts -type f -exec touch -- {} \;