user2874061
user2874061

Reputation: 123

Finding error with shell script

I wrote a script with mbratch's help.

I run as below;

./scriptname folder1

However, I see neither error nor results and I'm not sure what's wrong.

sh -x ./scriptname folder1

    + MAIN 
    + check_directories
    + [ -d  ]

Upvotes: 0

Views: 111

Answers (2)

Adam Liss
Adam Liss

Reputation: 48330

This works fine for me:

Note: updated to support additional options.

opt="$1"
folder1="$2"
folder2="$3"

case "$opt" in
-d)
  if [ -d "${folder1}" ] && [ -d "${folder2}" ] ; then
    for i in "${folder1}/*" ; do
    echo "test <$i>"
      if [ -f "${folder2}/${i##*/}" ] ; then
        echo "<${i##*/}>"
      else
        echo "! <${i##*/}>"
      fi
    done
  fi
  ;;
# Example option
-h)
  # Print help and exit.
  ;;
# Default case
*)
  echo "Unknown option '$opt'" >&2
  exit 1
  ;;
esac

Try replacing ~/ with $HOME/, and be sure to set folder1 and folder2 before using them. Note also that this will break if your directory or file names include spaces. In that case, use find; check the find man page for details.

Upvotes: 1

beroe
beroe

Reputation: 12326

Is that the entirety of your script? The variables $folder1 are never defined anywhere. By default, the program will take the first two chunks of text in as $1 and $2, so use those variables instead.

Put some echo statements in there to see what variables have what values.

You have a for loop already, so go with that. However you might have to put the part where you are getting a file list inside of $() to have it assigned to a variable, and then loop over it.

Do a quick search on "Looping through files in bash" and you will find a good template for the for loop.

Upvotes: 0

Related Questions