I am trying to run a shell script located on a Linux server from Windows. The shell script does two things:
- Do a
sed
command to replace text in an .sql file in the same directory. - Run the .sql file with
sqlplus
.
The shell script:
!/bin/sh
arg1=$1
arg2=$2
arg3=$(echo $arg1 | tr '[:lower:]' '[:upper:]')
arg4=$(echo $arg2 | tr '[:lower:]' '[:upper:]')
echo $arg1
echo $arg2
echo $arg3
echo $arg4
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
sqlplus $arg2/$arg2@MYDB <<EOF
@sequence.$arg1.sql
exit;
(My database is located on the same Linux server.)
1) Script runs correctly when I log in to the server via MobaXterm
- Connect to server with
userID
. - Set
my_env
. cd
to the shell script's directory.- Run script with
./myscript.sh
with arguments.
2) Same shell script runs successfully via .cmd manually
- Create a Windows script
test.cmd
on my Windows PC. In the .cmd file I have the line:
plink.exe -ssh userID@Server
After the console window pops up, I repeat the steps 2 to 4 and script runs successfully.
What I am failing to do so is to automate the whole process.
Here's the line in my .cmd file which I attempted:
plink.exe -ssh userID@Server /myfilepath/myscript.sh %arg1% %arg2%
I can see the arguments passed correctly using multiple echo
in the shell script. However, the shell script fails to locate the .sql file.
Error log:
/mypath/myscript.sh[1]: !/bin/sh^M not found [No such file or directory] myarg1value myarg2value :No such file or directory[myarg1value] /mypath/myscript.sh[12]: sqlplus: not found [No such file or directory]
I also tried below, but unfortunately with same result:
plink.exe -ssh userID@Server -m command.txt
Where file command.txt
contains:
. my_env
cd /filepath/
./myscript.sh %arg_with_actual_value%
I do not know why it is not working, especially when 2) works and the script is relatively simple.
Do I assume things incorrectly about plink
(path, variable, etc.)?
Is Cygwin the only way out?
I tried not to rely on yet another tool as I have been using plink
.
EDIT: While the line
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
fails to run on the .sh, i can run it on the .cmd file itself via:
plink.exe -ssh userID@Server sed -i "s/%arg3%/%arg4%/g" /myfilepath/sequence.%arg1%.sql
Hence I am suspecting the problem comes from the .sh file not having the required components to run (i.e. set env variable, path, etc)