0

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  ]
9
  • 1
    Are you in your home dir? Either prefix every instance of ${folderN} with ~/, or none, but not some. Commented Oct 20, 2013 at 0:16
  • If neither $folder1 or $folder2 are actually directories, this script will not do anything. Is that the case? Commented Oct 20, 2013 at 0:16
  • Whether you are reaching even inside if condition? make habit of printing log messages when you are debugging. paths in your initial condition and that in the loop don't match. Difference of ~/ Commented Oct 20, 2013 at 0:18
  • @user2874061 you can echo messages at regular intervals. e.g. after first if condition, echo $folder1 and $folder2 both are directory Commented Oct 20, 2013 at 0:24
  • 1
    Have you run sh -x ./sriptname -d folder1 folder2 to see what the script thinks it is doing? If not, now is a very good time to try. It is the basic debugging technique for shell scripts. Commented Oct 20, 2013 at 0:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why do you need the parameter? What does it accomplish?
So change $1 and $2 to $2 and $3. If you want to get fancy, you will have to something like getopts
Updated to show a very simple way to support additional options. Use getopts as beroe suggested if you need something more complex.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.