LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Problem executing mv command inside shell script (https://www.linuxquestions.org/questions/linux-general-1/problem-executing-mv-command-inside-shell-script-103572/)

pablogosse 10-13-2003 06:36 PM

Problem executing mv command inside shell script
 
Hi all. In a php application I'm writing under Redhat 9 I'm using php's system() function to execute a shell script.

This shell script takes two variables, an old directory location and a new directory location, and should move the old directory to the new directory.

Here's the script:

Code:

#!/bin/bash

# Params:
# $1 original directory name
# $2 new directory name

# set site webroot
SITE=/path/to/web/root

if [ ! -d $SITE ] ; then
        exit 10
elif [ ! -d $SITE$1 -o -d $SITE$2 ] ; then
        exit 20
else
#      exit 30
        mv $SITE$1 $SITE$2
fi

So the executing command would be
Code:

mv /path/to/web/root/and/olddirectory/ /path/to/web/root/and/newdirectory/
When this script executes it returns an exit code of 1 (even if I wrap $SITE$1 $SITE$2 in quotes), but if I comment out the mv command and uncomment the exit 30 line, it returns an exit value of 30.

I've been looking in my reference manuals but I can't seem to find what the exit code 1 for the mv command is.

The only thing I can see is that it might be is a syntax error in the mv command, but if that's it I'm lost as to what it is. I've executed the mv command from the command line, using the value of $SITE and $1 and $2 and it works fine.

If anyone can shed some light on what this could be I would greatly appreciate it. I realize that it's probably something quite small but I'm relatively new to shell scripting under Linux so any help would be great.

Thanks in advance,
Pablo

jailbait 10-13-2003 09:30 PM

Try:

mv ${SITE}$1 ${SITE}$2

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites

Walt_R 10-13-2003 10:20 PM

If I remember correctly, you need to cat variables together in a shell script.

IE:
mv `cat $site $1` `cat $site $2`

{` = accent graves}

Walt R.

pablogosse 10-14-2003 10:03 AM

Hi guys. Thanks for the replys. Actually the problem was with the permissions on the shell script and on the site itself.

Here's the breakdown:

/cmsutil/CMS_move_area_dirs (gossep:gossep)
/path/to/site/dir/to/move/ (gossep:gossep)

However the shell script when being executed is running under the privelidges of the apache user, so for that reason it was failing. I need to talk to my sysadmin about setting it up such that whenever the shell scripts execute they do so under the permissions of the gossep user, even when called by apache.

The workaround for now was to chown everything under the site to apache:apache and since this isn't a production site I'm okay with that, just wanted to get everything working properly.

Thanks,
Pablo.

nbtest 12-16-2009 11:40 AM

Problem executing mv command inside shell script
 
i was able to run the mv command by using the format below for the shell script

`mv $1 $1`

Note command is enclosed in [` = accent graves]



Hope this helps!

catkin 12-16-2009 12:01 PM

Quote:

Originally Posted by pablogosse (Post 535927)
The workaround for now was to chown everything under the site to apache:apache and since this isn't a production site I'm okay with that, just wanted to get everything working properly.

Glad you solved it :)

Without in any way wishing to be disrespectful to the members who responded in this thread (we're all volunteers, it's great that the community helps itself and all of us make mistakes some time -- or contribute nothing) none of the advice given is relevant. Thanks to the posters for contributing and I hope that the following critique spreads bash knowledge:
  • Regards mv ${SITE}$1 ${SITE}$2: in bash, variable name strings are terminated by any character that is not valid in a name. It is never wrong to enclose the name in { } but it is not necessary in this case.
  • Regards mv `cat $site $1` `cat $site $2`: this is wrong; it would give the mv command arguments of whatever the contents of any files identified by the $site, $1 and $2 variables happened to be -- possibly removing files.
  • Regards `mv $1 $1`: this would work to move file $1 to file $2 but the backticks are unnecessary (and it is safer to enclose the $1 and $2 in double quotes in case their values contain whitespace)

GrapefruiTgirl 12-16-2009 12:05 PM

Catkin, you've got a typo in your third list point ;)

catkin 12-16-2009 12:21 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3793997)
Catkin, you've got a typo in your third list point ;)

Thanks for pointing it out Sasha -- that will teach me to copy-and-paste from posts, believing they actually said what I thought they said (and maybe even what they meant)! :scratch: "Say what you mean and mean what you say". Anybody seen the movie that quote is from (one of my faves)? Went to check the name of the movie (Senior Members have senior moments) and found the line came from "Alice in Wonderland" in which the Cheshire Cat says it. Cool :cool:


All times are GMT -5. The time now is 09:52 PM.