Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not a valid identifier error with read command

Tags:

linux

bash

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
like image 761
debster Avatar asked Dec 17 '25 09:12

debster


1 Answers

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.

like image 118
Suchitra Avatar answered Dec 19 '25 23:12

Suchitra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!