I have to run some shell scripts in Windows using Cygwin. I am able to achieve that using %BASH% --login -i "/cygdrive/d/script.sh", where %BASH% is an environment variable in Windows set to C:\cygwin\bin\bash.exe.
The above script executes without any problem. But when there are Cygwin environment variables inside the .sh files then it doesn't work.
For example, I have a created a Cygwin environment variable $EXE_PATH = /cygdrive/d/somepath/dir. Inside my script the line is
$EXE_PATH/some.exe -f /cygdrive/d/password.txt
There is error in this line every time I run the script.
Error is something to do with the some.exe/password.txt not being found.
If I run the same command from the Cygwin terminal, it works fine. Is there something wrong with /cygdrive?
Here is the script
#!/bin/sh
#TESTLEELA=/cygdrive/d
echo simple shell script
echo $EXE_PATH
$EXE_PATH/runube.exe -f $TESTLEELA/password.txt DVRP910 *ALL R0006P XJDE0001 QBATCH B H S //NASRVNY1
Change the line
$EXE_PATH = /cygdrive/d/somepath/dir
to
EXE_PATH=/cygdrive/d/somepath/dir (in cygwin shell script)
Related
So I have created a very simple Shell Script. All it does it set 3 ENV variables for AWS so that I can send a .csv file to S3 in a separate script.
#!/usr/bin/env bash
export AWS_DEFAULT_REGION=us-east-1
export AWS_SECRET_ACCESS_KEY=ksjdnkjsdnfkjsndfksjnfd
export AWS_ACCESS_KEY_ID=kjsdnfkjsndfkjsndfkjsdn
The SECRET KEY and KEY_ID have been changed for this post, just so we're clear
But for some reason, when I run this Shell Script, nothing seems to happen at all. When I run the ENV command, none of the variables are set. HOWEVER, when I copy and paste each command into my terminal individually, it sets the variable without issue. And then I am able to send my .csv file to S3 without issue.
I feel like I'm doing something extremely trivial that's causing the script to simply not run the commands.
Any ideas?
System: Raspberry Pi 3
Release: 10
-Joe
If you're just executing your script ./myscript.sh, the environment variables won't be set in your shell just the subshell it executes in.
You can execute your script with a "dot space script" syntax or use source. Both of which will execute the script in the current shell instead of launching a subshell.
. myscript.sh
source myscript.sh
I am running a perl file a.pm which invokes b.sh via system command.
Here, b.sh is using find utility whose path is /usr/local/bin.
If I run env on shell directly on machine, I get output as below for PATH variable.
PATH=/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/bin:/home/bin/samba::/home/venv/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin`
Thats why if I run the b.sh directly from shell, it is able to execute find utility.
Now, If I run b.sh via a.pm as mentioned earlier using system(), and when I print PATH env variable in b.sh, its coming as
/bin:/usr/bin:/usr/X11R6/bin:/home/bin:/home/perl5/bin
which does not have /usr/local/bin, and thats why find command is failing.
If I tried to print all ENV variables in perl before invoking system(b.sh), PATH variable is not printed.
Now, I tried adding path variable in a.pm file as follows just before invoking system(b.sh).
$ENV{'PATH'} = '/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/home/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/';
Now, if I try to print all ENV variables in perl before invoking system(b.sh), PATH variable is printed with above value.
Still executing the a.pm file, the PATH variable printed in b.sh is same:
/bin:/usr/bin:/usr/X11R6/bin:/home/bin:/home/perl5/bin
How can I add corresponding path /usr/local/bin to shell of b.sh invoked using a.pm?
I suspect that the Perl program is either modifying the path it gets from the shell that invokes it, or that you have left out a step somewhere. For example, if you invoke the Perl program from a different environment, it will likely have a different PATH.
You seemed to have found your answer though. Add the necessary directory to the PATH in the Perl program. But, you say this doesn't work. Again, I think there's some step that you haven't included. I suspect that the way in which you run system overwrites the PATH inherited from the parent.
For example, here's a small Perl program the merely runs a shell script:
#!perl
use v5.10;
$ENV{PATH} = '/bin:/usr/bin';
say "PATH in Perl is $ENV{PATH}";
system( "sh ./pather.sh" );
The shell script echos the PATH:
#!/bin/sh
echo "PATH in shell:" $PATH
When I run this, both PATHs match:
PATH in Perl is /bin:/usr/bin
PATH in shell: /bin:/usr/bin
But, maybe the command in system is something else. The -l switch treats the shell as a login shell, so it will load the various profiles and whatnot:
system( "sh -l ./pather.sh" );
Now the PATH is different in the shell script because my particular profiles overwrote PATH:
PATH in Perl is /bin:/usr/bin
Path in shell: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:...
Our answers can be more targeted you can produce a minimal working example where we see actual code that demonstrates the problem. Since we don't see what you actually run in system, we can only guess.
I need to source a few environment variables in another file. If I use the source command with system() function, it's complaining about "No such file or directory". Am I missing something?
My code looks like below. In my code, I have only the system() function running the source command. The source file has just only one command: pwd (Present working directory).
perl_system.pl
#!/usr/bin/perl
system "source env.mk"
env.mk (contents of env.mk which I want to source has just pwd for now"
pwd
When I run this command, I see the below error
$ perl -w perl_system.pl
Can't exec "source": No such file or directory at perl_system.pl line 2.
source is a shell built-in that executes a shell script using the current shell interpreter. So it doesn't work as an external command and won't change the environment of your perl process even if you change your system call to invoke a shell instead of it trying to run an external program directly.
You could run your env.mk and then output the resulting environment and update perl's environment accordingly, though:
for my $env (`bash -c 'source env.mk;env'`) {
chomp $env;
my ($var,$val) = split /=/, $env, 2;
$ENV{$var} = $val;
}
(with obvious problems if environment variables contain newlines).
Update: just read all of your question, not just the beginning. If all you want to do is execute a shell script, just do:
system "sh env.mk";
source is completely unnecessary for this.
I have a Windows batch script (to be honest, it's a Groovy script). In this script I determine the root directory of a Cygwin installation. The next step is to find out the current user and home directory. In Cygwin this would be just a
echo $HOME
#=> /home/Christian
What comes pretty close to my problem is the following question: Get results of command from Cygwin in Batch.
I would like to execute something like this:
"C:\cygwin[64]\bin\bash[64].exe" "echo $HOME"
However I receive a
#=> /usr/bin/bash[64]: echo $HOME: No such file or directory
This is because bash is expecting a script file and I want to execute a single command. How can this be done? Is there a possibility without putting the command in a script file?
My goal is to get the Windows path to the current users home directory so that I can iterate over this directory from a Windows script.
The correct option to issue a single command is -c, and before you need to perform a login with --login.
C:\>c:\cygwin64\bin\bash.exe --login -c "cygpath -w $HOME"
#=> C:\cygwin64\home\Christian
I found the answer here.
I am trying to run a command in a script, something like this one:
ssh user#host:/bin/echo > /home/path/file.log
Now when I run this command on a command line it works fine, however when put in a script (shell or ruby ) it cribs saying:
/bin/sh: /home/path/*.log: No such file or directory
Am I missing something?
Thanks!
Update:
It's weird that same thing is not being executed now even on the shell when I use putty. I have verified that the path and file exists on remote machine which is being ssh'ed into.
You need to loop over the files. If it works from the command line then your interactive shell is not a standard shell.
for f in /home/path/*.log; do
:>"$f"
done
Note also the use of a null command; in many shells, you don't need a command at all. Your echo puts an unattractive empty line at the beginning of each file.
If you are attempting to run this remotely, you will need to quote it:
ssh user#remote 'for f in /home/path/*.log; do :>"$f"; done'
Its working fine when I put quotes:
ssh user#host:"/bin/echo > /home/path/file.log"