3

I am trying to give the option to the users to run the script in a different directory. However when the script changes the directory, the mkdir does not work. I change the directory like this:

case $1 in
 "-d")
   PWD=$2;;
   *)
     ;;
esac

but when I run the script, it runs in the directory that it exists. it does not change the directory.

here is the complete code:

#!/bin/bash
#File sorter
set -e    # Abort in case of error
START=$(date +"%T")
SAVEIFS=$IFS
case $1 in
    "-d")
      PWD=$2;;
    *)
      ;;
esac
echo "trigger took place in $PWD"
for i in *; do
  echo "processing \"$i\" ..."
  case $i in
    *.zip)
      [ -d archive ] || mkdir archive
      mkdir "archive/${i%.*}"
      unzip -d "archive/${i%.*}" "./$i"
      echo -e "\"$i\" extracted in $PWD/archive/${i%.*}" >> report.txt;;
    *.*)
      mkdir -p "${i##*.}"
      mv -- "$i" "${i##*.}/"
      echo -e "\"$i\" moved to $PWD/${i##*.}" >> report.txt;;
    *)
      tmp=$(TMPDIR=. mktemp -d)
      mv -- "$i" "$tmp/"
      mv -- "$tmp" "$i";;
  esac
done
echo "Done in $SECONDS seconds!"
END=$(date +"%T")
echo "Start time: $START" >> report.txt
echo "End time: $END" >> report.txt
1
  • 1
    A comment not on the question: why don't you use cd to change directory? Commented Mar 4, 2012 at 7:38

2 Answers 2

3

I'm not surprised that changing PWD doesn't do anything.

Simply do a cd "$2"

but when I run the script, it runs in the directory that it exists.

It runs in the working directory of the shell you executed it.. If you're in the directory of the script, then yes, it will execute there.

0
1

The PATH variable is reserved for finding programs that the shell will use. It is a list of directories, separated by colons (:) which is searched in order until the program is found. From the manpage of bash

PATH

The search path for commands. It is a colon-separated list of directories in which the shell looks for commands (see COMMAND EXECUTION below). A zero-length (null) directory name in the value of PATH indicates the current directory. A null directory name may appear as two adjacent colons, or as an initial or trailing colon. The default path is system-dependent, and is set by the administrator who installs bash. A common value is ''/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.

If you change that variable, it changes how the shell finds programs, like mkdir.

I suggest changing the variable to path or dir (case matters in Linux/UNIX).

4
  • Where does he use the PATH var? I cannot see it. Commented Mar 4, 2012 at 7:36
  • @enzotib I edited the question, first it was PATH then I tried it with PWD Commented Mar 4, 2012 at 10:38
  • PWD is a variable that is set by the shell. To change the directory, use cd _directory_. For example, cd /var/log. If returns a non-zero return value if the shell is not able to change the current directory. The cd is a builtin command in the shell, not a program. Commented Mar 4, 2012 at 13:42
  • Also, you're not doing anything about the fact that if $1 is -d, you're not removing it and $2 from the list of files to operate on. Take a look at the 'shift' operator in the bash documentation. Commented Mar 4, 2012 at 14:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.