0

I'm having problem with storing current path and comparing it with the one user entered. At the moment I have

cd $path
checkpath="$(pwd)"
if [ "$checkpath" == "$path" ]; then
chown -Rv $user *

and the $path is entered by user. Script fails at the above point each time and I can't figure out why. I just need a way to check that the chown part will really execute under the path that user entered.

edit: $path contains path that user entered while checkpath should contain output generated by pwd. So on short, yes, I'm trying to make sure that cd command worked and that chown will be executed in the directory which used wrote when he was asked for it.

  • 2
    is there any output when the script fails? – Dan Sep 08 '15 at 12:57
  • How do intend to deal with symlinks to paths? – Etan Reisner Sep 08 '15 at 12:59
  • @dan08 Yes, the output is message from other part of script that is telling that checkpath is not same as path :) – DasGjinovskaLignja Sep 08 '15 at 13:14
  • @EtanReisner No worries about that because the script will be used only on directories where there is no symlinks. It's hard to explain in short but as I said, no worries about them in this case – DasGjinovskaLignja Sep 08 '15 at 13:16
  • 1
    Ok, so what are the values of `checkpath` and `path` and how do they differ from what you expect. Please edit this extra information into your question. – Dan Sep 08 '15 at 13:30
  • All you are doing with `checkpath` is confirming that `cd` worked; I think you can omit it altogether. Make sure to quote `$path`, though: `cd "$path"`. – chepner Sep 08 '15 at 13:41
  • Add `set -x` to your script to see what is actually happening. – Etan Reisner Sep 08 '15 at 13:44
  • @chepner Yes, that is what I'm trying to do. I know that there is no reason for chown to be executed anywhere else except in the directory to which I moved with cd $path But lets just say I'll will feel easier at hearth knowing that there is "one" additional step that will prevent chown from executing in wrong directory. .... although in the end use will most likely kill the server by entering / when asked for the directory path :) – DasGjinovskaLignja Sep 08 '15 at 17:03
  • @dan08 added as requested – DasGjinovskaLignja Sep 08 '15 at 17:03

2 Answers2

1
cd "$path" && chown -Rv "$user" *

This only executes chown if the cd command succeeds. Other ways of writing the same thing:

if cd "$path"; then
    chown -Rv "$user" *
fi

or

cd "$path" || exit
chown -Rv "$user" *

or

#!/bin/bash -e
cd "$path"
chown -Rv "$user" *

The last sets the shell's -e flag, which causes it to exit immediately if any command fails rather than continuing on.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

The $path that you are giving must be a full path then only it will succeed and moreover you need to add "fi" to end . It will look like :

./script.sh  /path_to_be_given user

cat script.sh

#!/bin/bash
path=$1
user=$2
cd $path
checkpath="$(pwd)"
echo "Parameter passed : path => $path ; user => $user ; checkpath =>    $checkpath "
if [ "$checkpath" == "$path" ]; then
    echo "chown -Rv $user *"
    chown -Rv $user *
    if [ $? -eq 0 ]; then
        echo "Ownership changed"
    fi 
fi

Hope this solves your problem

soumyaranjan
  • 101
  • 3