Bash shell脚本 - 设置变量时出错

She*_*rSt 2 variables bash shell

我是bash脚本的新手.我尝试了以下方法:

filename01 = ''

if [ $# -eq 0 ]
        then
                filename01 = 'newList01.txt'
        else
                filename01 = $1
fi
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

./smallScript02.sh: line 9: filename01: command not found
./smallScript02.sh: line 13: filename01: command not found
Run Code Online (Sandbox Code Playgroud)

我想我没有正确对待变量,但我不知道如何.此外,我试图使用grep从文本文件中提取第二个和第三个单词.该文件看起来像:

1966 Bart Starr QB Green Bay Packers 
1967 Johnny Unitas QB Baltimore Colts 
1968 Earl Morrall QB Baltimore Colts 
1969 Roman Gabriel QB Los Angeles Rams 
1970 John Brodie QB San Francisco 49ers 
1971 Alan Page DT Minnesota Vikings 
1972 Larry Brown RB Washington Redskins 
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激

Chr*_*is 6

在bash中分配变量时,=符号两侧不应有空格.

# good
filename0="newList01.txt"
# bad
filename0 = "newlist01.txt"
Run Code Online (Sandbox Code Playgroud)

对于你的第二个问题,请awk不要使用grep.以下将从名称存储在其中的文件的每一行中提取第二项和第三项$filename0:

< $filename0 awk '{print $2 $3}'
Run Code Online (Sandbox Code Playgroud)