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  ]
Community
  • 1
  • 1
user2874061
  • 123
  • 1
  • 8
  • 1
    Are you in your home dir? Either prefix _every_ instance of `${folderN}` with `~/`, or none, but not _some_. – Wrikken Oct 20 '13 at 00:16
  • If neither `$folder1` or `$folder2` are actually directories, this script will not do anything. Is that the case? – SethMMorton Oct 20 '13 at 00: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 `~/` – jkshah Oct 20 '13 at 00:18
  • @user2874061 you can `echo` messages at regular intervals. e.g. after first if condition, `echo $folder1 and $folder2 both are directory` – jkshah Oct 20 '13 at 00: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. – Jonathan Leffler Oct 20 '13 at 00:24
  • Can you show the whole script? How do the command line parameters from `./scriptname -d folder1 folder2` become `$folder1` and `$folder2`? And what does `-d` on the command line do? The code you show will display nothing if either `folder1` or `folder2` are not directories or if one of them doesn't exist, or if they're valid folders but there are no files in common. – lurker Oct 20 '13 at 00:25
  • If you want us to help you interpret the output, then you need to (a) make sure the complete script is in the question, and (b) the output from the trace (`sh -x`) is in the question. That way, you can format things so they're comprehensible. Posting code or error messages into a comment is generally a bad idea (though I do occasionally do it — but not to comments to my own questions). – Jonathan Leffler Oct 20 '13 at 00:29
  • Your output seems to show that the variable `folder1` hasn't been set. – Adam Liss Oct 20 '13 at 00:30
  • If you write functions, you have to add `set -x` to the front of each function. That's a real pain, but ... this is why it is crucial you show the whole code (and why I don't write functions the whole time in shell). – Jonathan Leffler Oct 20 '13 at 00:30

2 Answers2

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.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • Why do you need the parameter? What does it accomplish? – Adam Liss Oct 20 '13 at 01:31
  • So change `$1` and `$2` to `$2` and `$3`. If you want to get fancy, you will have to something like [getopts](http://stackoverflow.com/a/7680682/1681480) – beroe Oct 20 '13 at 06:53
  • Updated to show a very simple way to support additional options. Use `getopts` as beroe suggested if you need something more complex. – Adam Liss Oct 20 '13 at 12:53
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.

beroe
  • 11,784
  • 5
  • 34
  • 79