bash 读取命令不是有效的标识符错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20501737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:57:14  来源:igfitidea点击:

not a valid identifier error with read command

linuxbash

提问by debster

I have looked into reading files from this site, man pages and google but not understanding, I apologize if this has been answered. I am having trouble reading a file into my script. I am using bash shell in linux. I am also new to linux.

我已经研究了从这个站点、手册页和谷歌读取文件,但不理解,如果这已经得到回答,我深表歉意。我无法将文件读入我的脚本。我在 linux 中使用 bash shell。我也是 linux 新手。

I have a script that is supposed to read from a text file, perform tests, calculations, etc on each of the data lines specified.

我有一个脚本,它应该从文本文件中读取,对指定的每个数据行执行测试、计算等。

I am getting the following error: ./hw-script7b: line 8: read: `hw7.list3': not a valid identifier the hw7.list3 is the input file. it has full rights (read, write, even execute) hw-script7b is my script.

我收到以下错误:./hw-script7b: line 8: read: `hw7.list3': not a valid identifier hw7.list3 是输入文件。它拥有完全的权限(读、写、甚至执行) hw-script7b 是我的脚本。

the text file contains an -ls listing of a directory. this is probably a basic issue but I am lost. thanks for any assistance,

文本文件包含目录的 -ls 列表。这可能是一个基本问题,但我迷路了。感谢您的帮助,

Debbie

黛比

here is the script.

这是脚本。

echo "start"
count=0     #counting all files
xcount=0    #counting executeable files
total=0     #counting total space used
temp=0      #holding place for file being checked

exec 9< 
while read         0<&9
do
count='expr $count + 1'
if [  > "$temp" ]
then
lfname=""
linode=""
lsize=""
lrights=""
lmonth="" 
lday=""
temp=""
else
 if [  < "$temp" ]
 then
 sfname=""
 sinode=""
 ssize=""
 srights=""
 smonth=""
 sday=""
 temp=""
 fi
fi

#looking for executeable files
if [ -x "" ]
then
xcount='expr $xcount + 1'
fi
done

echo "The largest file is: "$lfile""
echo "      Inode    Size    Rights       Date     "
echo "     $linode  $lsize  $lrights  $lmonth $lday"
echo
echo
echo "The smallest file is: "$sfile""
echo "      Inode    Size    Rights       Date     "
echo "     $sinode  $ssize  $srights  $smonth $sday"
echo
echo
echo "The total number of files is "$count" and they use "$total" bytes."
echo
echo
echo "There are "$xcount" executable files."
exit 0


sample of input file: hw7.list3
934250 -rwxrwxr-x 1 1107 Dec  2 18:48 //home/others/professor/class-share/hw7.howard
7934314 -r-xr-xr-t 1 1232 Dec  2 18:48 //home/others/professor/class-share/Matts_HW6
7934139 -rwxrwxr-x 1 1232 Dec  4 20:08 //home/others/professor/class-share/Matts_HW6_2
7934366 -rwxrw-r-- 1 1537 Dec  9 19:32 //home/others/professor/class-share/bs-hw7
7934364 -rwx------ 1  965 Dec  9 19:48 //home/others/professor/class-share/hw7
7948204 -rwxr-xr-x 1 107 Nov 12 07:47 readtest1
7948209 -rwxr-xr-x 1 107 Nov 12 07:48 readtest1a
7948205 -rwxr-xr-x 1 140 Nov 12 07:48 readtest2
7948206 -rwxr-xr-x 1 160 Nov 12 07:48 readtest3
7948207 -rwxr-xr-x 1 165 Nov 12 07:48 readtest4
7948208 -rwxr-xr-x 1 211 Nov 12 07:48 readtest5
7948210 -rwxr-xr-x 1   8 Nov 12 07:49 namefile1
7948211 -rwxr-xr-x 1  17 Nov 12 07:49 namefile2
7948212 -rwxr-xr-x 1  28 Nov 12 07:49 namefile3
7932451 -rwxr--r-- 1  219 Nov 13 16:53 //home/others/professor/class-share/grades-text
7934113 -rw-r--r-- 1  111 Nov 18 17:27 //home/others/professor/class-share/test2.gz

回答by Suchitra

I faced a similar issue while running one of my scripts. My script displays the running process ids and prompts the user to enter one of the process ids. I got this error ': not a valid identifier: read: `userPid

我在运行我的一个脚本时遇到了类似的问题。我的脚本显示正在运行的进程 ID 并提示用户输入进程 ID 之一。我收到此错误':不是有效标识符:读取:`userPid

This is due to some special characters which gets added to the script when we scp the script from windows to unix. I did the following and it worked fine. sudo yum install dos2unix dos2unix script_file.sh Then ran the script. It worked without issues.

这是因为当我们将脚本从 windows scp 到 unix 时,一些特殊字符被添加到脚本中。我做了以下工作,效果很好。sudo yum install dos2unix dos2unix script_file.sh 然后运行脚本。它没有问题。

回答by scythe

The issue here is you're doing read $variableand not read variableTry it without the $ symbol and it should work like a charm.

这里的问题是你正在做read $variable而不是read variable在没有 $ 符号的情况下尝试它,它应该像魅力一样工作。

回答by damienfrancois

Here is an example that should get you started:

这是一个应该让您入门的示例:

#!/bin/bash
echo "start"
count=0     #counting all files
xcount=0    #counting executeable files
total=0     #counting total space used
temp=0      #holding place for file being checked


while read linode lrights dummy lsize  lmonth lday dummy lfname ;
do
(( count ++ ))
(( total += lsize ))
[[ "$lrights" == *x* ]] && (( xcount ++ ))
done < 


echo "The total number of files is "$count" and they use "$total" bytes."
echo
echo "There are "$xcount" executable files."

I named the script t.sh

我将脚本命名为 t.sh

$ ./t.sh hw7.list3 
start
The total number of files is 16 and they use 7346 bytes.

There are 15 executable files.

The variables $1 $2 ...are set to the arguments given to the script. Here I use readto set the variables linod, etc.from the contents of the file whose name is given in $1(note the done < $1part). What you were trying to do is to create a variable named hw7.list3which is not possible because it is not a valid Bash variable name ( it contains a dot )

变量$1 $2 ...设置为提供给脚本的参数。在这里,我用来从文件的内容中read设置变量linod, etc.,其名称在$1(注意done < $1部分)中给出。您试图做的是创建一个名为的变量hw7.list3,这是不可能的,因为它不是有效的 Bash 变量名(它包含一个点)

回答by CLIFFORD P Y

Most cases for ': not a valid identifier:due some special characters in script

大多数情况下': not a valid identifier:由于脚本中的某些特殊字符

Make sure that your editor support shell script command,better avoid windows editors. Try use pico/vi/gedit in linux

确保您的编辑器支持 shell 脚本命令,最好避免使用 windows 编辑器。尝试在 linux 中使用 pico/vi/gedit

this may help some one with same problem.

这可能会帮助一些有同样问题的人。

回答by drunk user

I know this thread is old, but since there is no chosen solution I'll give it a try.

我知道这个线程很旧,但由于没有选择的解决方案,我会尝试一下。

I had a similar problem as you with a small script, where I wanted the script to read a decimal number as the first command line argument. Got the same error message.

我在使用小脚本时遇到了与您类似的问题,我希望脚本读取十进制数作为第一个命令行参数。得到同样的错误信息。

I'm not a bash expert and my solution is probably not very elegant, but I solved my problem by putting a for-command before my while-loop. You could try the same

我不是 bash 专家,我的解决方案可能不是很优雅,但是我通过在 while 循环之前放置一个 for 命令解决了我的问题。你可以试试同样的

for filename in $*; do
exec 9< 
while read         0<&9
do
count='expr $count + 1'
if [  > "$temp" ]
then

...

done