纯字符串和变量字符串有什么区别
What is the difference between plain string and string from variable
我正在尝试编写简单的 shell
脚本,它将 运行 对许多文件进行 linter。我将所有文件名存储在一个长字符串中,然后还从中删除了所有可能的 /n
符号。然后我 运行 对这些文件的名称进行了硬编码并从变量中获取。对于硬编码路径,我的 linter 工作并且可以找到这些文件,但是对于存储在变量中的这些文件的路径,它不能。
这是我尝试过的:
#!/bin/sh
all_files_to_commit=$(git diff --name-only --cached)
ends_with_ts=".*\.ts$"
ends_with_scss=".*\.scss$"
all_ts_files=""
all_scss_files=""
for file in $all_files_to_commit
do
if echo "$file" | grep "$ends_with_ts"; then
all_ts_files="${all_ts_files}${file} "
fi
if echo "$file" | grep "$ends_with_scss"; then
all_scss_files="${all_scss_files}${file} "
fi
done
all_ts_files=$(echo "$all_ts_files" | tr -d "\n")
echo "All files in one string"
echo "$all_ts_files"
./node_modules/.bin/eslint src/components/List/List.ts src/views/Calculator/Calculator.ts --quiet
./node_modules/.bin/eslint "$all_ts_files" --quiet
您知道为什么这些路径可能不同吗?我该如何解决这个问题?
I also tried this script without line which remove\n
signs
输出:
All files in one string
src/components/List/List.ts src/views/Calculator/Calculator.ts
/home/kaczor6418/Desktop/projects/expressions-calculator/src/components/List/List.ts
24:9 error Delete `·············` prettier/prettier
/home/kaczor6418/Desktop/projects/expressions-calculator/src/views/Calculator/Calculator.ts
53:9 error Unexpected var, use let or const instead no-var
54:9 error Delete `············` prettier/prettier
✖ 3 problems (3 errors, 0 warnings)
3 errors and 0 warnings potentially fixable with the `--fix` option.
Oops! Something went wrong! :(
ESLint: 7.11.0
No files matching the pattern "src/components/List/List.ts src/views/Calculator/Calculator.ts " were found.
Please check for typing mistakes in the pattern.
引号在
./node_modules/.bin/eslint "$all_ts_files" --quiet
找到的文件名被连接成一个文件名 spaces。
字符串 src/components/List/List.ts src/views/Calculator/Calculator.ts
不代表文件(您没有名为 List.ts src
的目录)。
使用给定的文件名,您可以删除引号:
./node_modules/.bin/eslint $all_ts_files --quiet
当您从文件名列表创建字符串时,您看不到 space(或换行符)何时是文件名的一部分。
在您的示例中,您可以尝试使用数组:
all_files_to_commit=($(git diff --name-only --cached))
# change code here when you want to use arrays
./node_modules/.bin/eslint ${all_ts_files[@]} --quiet
或使用类似
的东西
./node_modules/.bin/eslint $(git diff --name-only --cached | grep ts$) --quiet
您可以使用
在文件名(不是换行符)中支持 spaces
./node_modules/.bin/eslint $(git diff --name-only --cached | sed -n '/ts$/ s/.*/"&"/p' ) --quiet
我正在尝试编写简单的 shell
脚本,它将 运行 对许多文件进行 linter。我将所有文件名存储在一个长字符串中,然后还从中删除了所有可能的 /n
符号。然后我 运行 对这些文件的名称进行了硬编码并从变量中获取。对于硬编码路径,我的 linter 工作并且可以找到这些文件,但是对于存储在变量中的这些文件的路径,它不能。
这是我尝试过的:
#!/bin/sh
all_files_to_commit=$(git diff --name-only --cached)
ends_with_ts=".*\.ts$"
ends_with_scss=".*\.scss$"
all_ts_files=""
all_scss_files=""
for file in $all_files_to_commit
do
if echo "$file" | grep "$ends_with_ts"; then
all_ts_files="${all_ts_files}${file} "
fi
if echo "$file" | grep "$ends_with_scss"; then
all_scss_files="${all_scss_files}${file} "
fi
done
all_ts_files=$(echo "$all_ts_files" | tr -d "\n")
echo "All files in one string"
echo "$all_ts_files"
./node_modules/.bin/eslint src/components/List/List.ts src/views/Calculator/Calculator.ts --quiet
./node_modules/.bin/eslint "$all_ts_files" --quiet
您知道为什么这些路径可能不同吗?我该如何解决这个问题?
I also tried this script without line which remove
\n
signs
输出:
All files in one string
src/components/List/List.ts src/views/Calculator/Calculator.ts
/home/kaczor6418/Desktop/projects/expressions-calculator/src/components/List/List.ts
24:9 error Delete `·············` prettier/prettier
/home/kaczor6418/Desktop/projects/expressions-calculator/src/views/Calculator/Calculator.ts
53:9 error Unexpected var, use let or const instead no-var
54:9 error Delete `············` prettier/prettier
✖ 3 problems (3 errors, 0 warnings)
3 errors and 0 warnings potentially fixable with the `--fix` option.
Oops! Something went wrong! :(
ESLint: 7.11.0
No files matching the pattern "src/components/List/List.ts src/views/Calculator/Calculator.ts " were found.
Please check for typing mistakes in the pattern.
引号在
./node_modules/.bin/eslint "$all_ts_files" --quiet
找到的文件名被连接成一个文件名 spaces。
字符串 src/components/List/List.ts src/views/Calculator/Calculator.ts
不代表文件(您没有名为 List.ts src
的目录)。
使用给定的文件名,您可以删除引号:
./node_modules/.bin/eslint $all_ts_files --quiet
当您从文件名列表创建字符串时,您看不到 space(或换行符)何时是文件名的一部分。
在您的示例中,您可以尝试使用数组:
all_files_to_commit=($(git diff --name-only --cached))
# change code here when you want to use arrays
./node_modules/.bin/eslint ${all_ts_files[@]} --quiet
或使用类似
的东西./node_modules/.bin/eslint $(git diff --name-only --cached | grep ts$) --quiet
您可以使用
在文件名(不是换行符)中支持 spaces./node_modules/.bin/eslint $(git diff --name-only --cached | sed -n '/ts$/ s/.*/"&"/p' ) --quiet