-3

Good afternoon, I'm trying to run my code for assignment 4 but I keep getting an expression syntax error, can you tell me why?

#!/bin/sh
if [ "$#" -ne 1 ] then
        echo "Usage: assignment4.sh <directory_name>"
        exit 1
fi

if [ ! -d "$1" ] then echo "$1: No such directory" exit 1 fi num_dir=0 num_file=0 num_readable=0 num_writable=0 num_executable=0

for item in "$1"/* do #Question1 if [-d "$item" ] then num_dir=$((num_dir+1)) fi #Question 2 elif [ -f "$item" ] then num_file=$((num_file+1)) fi #Question 3 if [-r "$item" ] then num_readable=$((num_readable+1)) fi #Question 4 if [ -w "$item" ] then num_writable=$((num_writable+1)) fi #Question 5 if [ -x "$item" ] then num_executable=$((num_executable+1)) fi done

echo "Number of directories: $num_dir" echo "Number of files: $num_file" echo "Number of readable items: $num_readable" echo "Number of writable items: $num_writable" echo "Number of executable items: $num_executable"

  • 4
    When you have a syntax error, make a habit to pass the whole script to https://shellcheck.net And add your shebang and proper tags (ash, sh, bash, dash, fish, ksh, zsh, foosh...) by editing your original post. No comment – Gilles Quénot Apr 27 '23 at 20:58
  • 3
    Shellcheck should flag at least 10 errors here. Note that there is a web page for every error code: for example, remedial action for SC1049 is explained in github.com/koalaman/shellcheck/wiki/SC1049. Also, shell should give a line number with its own error messages, which can be equally helpful to you and to us. – Paul_Pedant Apr 27 '23 at 21:32

1 Answers1

0

When asking questions like this, you need to tell us what the code is supposed to do (we don't know what your "assignment 4" is), and what happens instead. Don't tell us there is a syntax error, show us exactly how you execute the script and the exact error message.

That said, your issues here are straightforward:

  1. if statements require specific syntax: if condition; then action; fi. The ; are "list terminators" and you need a list terminator to separate the if command from the then action. The same goes for the for loops which need for thing in list_of_things; do action; done. For both of these, you can use a ; or a newline as a list terminator, but you need to have a list terminator. Either of these would be fine:

    if [ "$#" -ne 1 ]; then
       command 
    fi
    

    for item in "$1"/*; do command done

    or

    if [ "$#" -ne 1 ]
    then
      command
    fi
    

    for item in "$1"/* do command done

  2. The [ is a command, so like all commands, it requires a space both before and after to make it clear that it is a single token. This means that if [-r "$item" ] is a syntax error, you need if [ -r "$item" ].

  3. if blocks are closed by fi. However, elif and else are part of the same block, so if you close your opening if, you cannot add an elif or an else afterwards, you would need to open a new if first.

Putting all this together, this is a working version of your script, keeping the exact same logic and only correcting the syntax errors:

#!/bin/sh
if [ "$#" -ne 1 ]; then
        echo "Usage: assignment4.sh <directory_name>"
        exit 1
fi

if [ ! -d "$1" ]; then echo "$1: No such directory" exit 1 fi num_dir=0 num_file=0 num_readable=0 num_writable=0 num_executable=0

for item in "$1"/*; do #Question1 if [ -d "$item" ]; then num_dir=$((num_dir+1)) #Question 2 elif [ -f "$item" ]; then num_file=$((num_file+1)) fi #Question 3 if [ -r "$item" ]; then num_readable=$((num_readable+1)) fi #Question 4 if [ -w "$item" ]; then num_writable=$((num_writable+1)) fi #Question 5 if [ -x "$item" ]; then num_executable=$((num_executable+1)) fi done

echo "Number of directories: $num_dir" echo "Number of files: $num_file" echo "Number of readable items: $num_readable" echo "Number of writable items: $num_writable" echo "Number of executable items: $num_executable"

terdon
  • 242,166