我试图将bash命令的结果存储在for循环中,以便在命令中使用。这是我目前拥有的:
for filename in /home/WIN/USER/files/*
var=$(basename ${filename%.*}) | awk -F'[_.]' '{print $1}'
do echo var
done
然而,我得到这些错误:
./script.sh: line 2: syntax error near unexpected token `var=$(basename ${filename%.*})'
./script.sh: line 2: `var=$(basename ${filename%.*}) | awk -F'[_.]' '{print $1}''
有人知道如何解决这个问题或如何做我想做的事吗?
谢谢。
您的for
语句是错误的,您的变量赋值语句也是错误的。你应该这样写:
for filename in /home/WIN/USER/files/*; do
var=$( your shell code goes here ) # you assign the output of the shell code here
echo "$var" # you echo the results here
done