简体   繁体   中英

change directory and run command with #!bin/bash file

I need a cron job which changes the directory and runs a command.

Commands:

cd /var/www/domain.com/httpdocs
sake cron/test

I wrote this .sh file (/var/here/and/there/test.sh):

#!/bin/bash
cd /var/www/domain.com/httpdocs
sake cron/test

I "chmoded" the file:

chmod 777 test.sh

When I run it:

sh /var/here/and/there/test.sh

the output is this:

[1] : No such file or directoryrons/test.sh: line 2: cd: /var/www/domain.com/httpdocs
[2] Can't find ./sapphire/cli-script.php or ./cli-script.php

(the second line is from the sake command, which only is successfull when executed in the correct directory)

thx

I think you have a stray carriage return ( \\r ) at the end of your cd command. The shell assumes the '\\r' is part of the directory name. When it prints it in the error message, the cursor jumps to the beginning of the line, and the remainder of the message overwrites whatever was there.

Make sure that test.sh uses Unix-style line endings, not Windows-style endings. (This may be a side effect of whatever editor you used to create it.)

Are you running under Cygwin by any chance?

dos2unix --help

(Warning: Unlike most filters, dos2unix converts files in place .)

Does directory /var/www/domain.com/httpdocs actually exist? Can you manually cd /var/www/domain.com/httpdocs from a shell on that system without error?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM