I am relatively new to shell, and I am getting a syntax error where I am still confused.
#!/bin/dash
ls="ls -l test"
small=0
medium=0
large=0
for i in $(seq 11 9 56)
do
filename=$(echo $($ls) | cut -d ' ' -f $i)
count=$(wc -w test/$filename | cut -d ' ' -f 1)
if [ "$count" -lt 10 ]; then
small=(( $small+1 ))
elif [ "$count" -gt 100 ]; then
large=(( $large+1 ))
else
medium=(( $medium+1 ))
fi
done
echo $small
echo $medium
echo $large
In this code, I am trying to get a list of files, retrieve how many words a file has, and sort them into small/medium/large size files.
I am pretty sure I haven't made any syntax errors for incrementing the variables using test, but they don't seem to work within an if statement. Can anyone help me out with this? There is a syntax error when using multiple brackets within an if statement, but I have no idea what it is.
small=(( $small+1 ))should probably besmall=$(( $small+1 )). I.e., you want the variablesmallto be assigned the value that comes from the arithmetic operation$small + 1. The lines with assignments tomediumandlargewould benefit from the same syntax change.https://shellcheck.net, a syntax checker, or installshellchecklocally. Make usingshellcheckpart of your development process.wc -w < test/$filename, it will not print the filename, so you won't need to cut it off. Also, inside$(( ... ))words are assumed to be variables:small=$(( small + 1 ))is enough.