debster
debster

Reputation: 39

not a valid identifier error with read command

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.

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.

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

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< $1
while read $1 $2 $3 $4 $5 $6 $7 $8 0<&9
do
count='expr $count + 1'
if [ $4 > "$temp" ]
then
lfname="$8"
linode="$1"
lsize="$4"
lrights="$2"
lmonth="$5" 
lday="$6"
temp="$4"
else
 if [ $4 < "$temp" ]
 then
 sfname="$8"
 sinode="$1"
 ssize="$4"
 srights="$2"
 smonth="$5"
 sday="$6"
 temp="$4"
 fi
fi

#looking for executeable files
if [ -x "$8" ]
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

Upvotes: 3

Views: 38910

Answers (8)

JAYTEE
JAYTEE

Reputation: 21

I realise that this is a VERY old post, but I just discovered the answer

The text file should be saved as UNIX Line Feeds instead of Mac OS CR or Windows CRLF

Upvotes: 1

Mathias Asberg
Mathias Asberg

Reputation: 3840

In my case it was caused from the "End of Line Sequence" LF vs CRLF. It's a setting in VSCode.

Upvotes: 1

Nitin Sharma
Nitin Sharma

Reputation: 51

I was getting the same error. I was editing on notepad++. If you're doing the same thing, covert the eof character to the one for linux (lf)

Upvotes: 4

Suchitra
Suchitra

Reputation: 69

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

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.

Upvotes: 7

scythe
scythe

Reputation: 71

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

Upvotes: 3

CLIFFORD P Y
CLIFFORD P Y

Reputation: 17404

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

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

this may help some one with same problem.

Upvotes: 4

drunk user
drunk user

Reputation: 65

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

for filename in $*; do
exec 9< $1
while read $1 $2 $3 $4 $5 $6 $7 $8 0<&9
do
count='expr $count + 1'
if [ $4 > "$temp" ]
then

...

done

Upvotes: -1

damienfrancois
damienfrancois

Reputation: 59120

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 < $1


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 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 read to set the variables linod, etc. from the contents of the file whose name is given in $1 (note the done < $1 part). What you were trying to do is to create a variable named hw7.list3 which is not possible because it is not a valid Bash variable name ( it contains a dot )

Upvotes: 1

Related Questions