command errors in a file but runs fine on a shell - ruby

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"

Related

Getting the value of a cygwin environment variable from a windows script

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.

bash in windows error?

I'm trying to execute a very simple script with cygwin, composed of:
#!/bin/bash\n
echo "hi"\n
with cygwinpath\bin\bash.exe /cygdrive/c/my_path/test.bash
but it says
/cygdrive/c/my_path/test.bash: line 1: #!/bin/bash: No such file or directory
However, it still prints 'hi'.
Why is this, and how to fix it ?
Thanks.
The first line of your script should just be #!/bin/bash and not #!/bin/bash\n
The code is still executing because the heading #!/bin/bash specifies a shell, and echo "hi"\n is a command to the terminal.
As for your issue I'm having no problems running it using the following path in the cygwin terminal:
/cygdrive/c/<my_path>/bin/bash.exe /home/user/test.bash

Environment variable not working in Cygwin

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)

How to run multiple Unix commands in one time?

I'm still new to Unix. Is it possible to run multiple commands of Unix in one time? Such as write all those commands that I want to run in a file, then after I call that file, it will run all the commands inside that file? or is there any way(or better) which i do not know?
Thanks for giving all the comments and suggestions, I will appreciate it.
Short answer is, yes. The concept is known as shell scripting, or bash scripts (a common shell). In order to create a simple bash script, create a text file with this at the top:
#!/bin/bash
Then paste your commands inside of it, one to a line.
Save your file, usually with the .sh extension (but not required) and you can run it like:
sh foo.sh
Or you could change the permissions to make it executable:
chmod u+x foo.sh
Then run it like:
./foo.sh
Lots of resources available on this site and the web for more info, if needed.
echo 'hello' && echo 'world'
Just separate your commands with &&
We can run multiple commands in shell by using ; as separator between multiple commands
For example,
ant clean;ant
If we use && as separator then next command will be running if last command is successful.
you can also use a semicolon ';' and run multiple commands, like :
$ls ; who
Yep, just put all your commands in one file and then
bash filename
This will run the commands in sequence. If you want them all to run in parallel (i.e. don't wait for commands to finish) then add an & to the end of each line in the file
If you want to use multiple commands at command line, you can use pipes to perform the operations.
grep "Hello" <file-name> | wc -l
It will give number of times "Hello" exist in that file.
Sure. It's called a "shell script". In bash, put all the commands in a file with the suffix "sh". Then run this:
chmod +x myfile.sh
then type
. ./myFile
or
source ./myfile
or just
./myfile
To have the commands actually run at the same time you can use the job ability of zsh
$ zsh -c "[command1] [command1 arguments] & ; [command2] [command2 arguments]"
Or if you are running zsh as your current shell:
$ ping google.com & ; ping 127.0.0.1
The ; is a token that lets you put another command on the same line that is run directly after the first command.
The & is a token placed after a command to run it in the background.

Bash script in agi-bin fails to execute - asterisk

I am trying to execute a bash script from within my dialplan. The bash scripts are from within a directory projectFiles in /var/lib/asterisk/agi-bin. When I try to execute the script, like so:
exten => 0,n,System(/var/lib/asterisk/agi-bin/projectFiles/main.sh ${RECORDED_FILE}.wav ${SOUND_PATH}/menus/wav2.wav)
I end up with the following error:
WARNING[27515]: app_system.c:125 system_exec_helper: Unable to execute '/var/lib/asterisk/agi-bin/projectFiles/main.sh /tmp/rec62.wav /var/lib/asterisk/sounds/en/projects/menus/wav2.wav'
== Spawn extension (test-project, 0, 5) exited non-zero on 'DAHDI/15-1'
The permissions to the script main.sh are set at asterisk as owner and group. Where might I be going wrong?
Any help is most welcome,
Sriram.
I had the same problem and found another reason, which might be common:
I had created the script on Windows, and copied over in binary mode. The text file contained the hidden ^M at each line end. I removed all of them and everything is fine! The error message is of course extremely misleading!
The script within agi-bin, main.sh did not execute successfully. Hence the error message. One line within main.sh copied $1 of the input arguments to the present working directory, which I had assumed would be the same directory in which the script was placed. $HOME for asterisk (and from where it executes all scripts) is /etc. So, a line like the following:
cp $1 .
would copy $1 to /etc. This caused mayhem further down the script. Changing that line makes everything work OK.

Resources