I have a requirement to download the latest file from sftp server.I have written the below code in shell script but am not able to download the file.
After retrieving the file am getting the below Error
Invalid command
Please help me how to download the file.
#!/bin/sh
HOST='xx.xx.xx.nxx'
USER='xx'
PASSWD='xx'
sftp $USER#$HOST <<EOF
cd /inbound
file=$(ls -ltr *.xml | tail -1 | awk '{print $NF}')
get $file
EOF
You are trying to run shell commands in sftp, but sftp is not a shell. The command ls happens to exist in sftp, but not $(), tail, or awk. To see this, just type sftp $USER#$HOST to open a sftp session and type help to get all of the available commands.
So what you need to do is to execute the shell commands using ssh to get the filename. So something like this:
file=$(ssh $USER#$HOST "ls -ltr /inbound/*.xml" | tail -1 | awk '{print $NF}')
This execute the command ls -ltr /inbound/*.xml remotely on the server. The output of that is then processed by your shell script locally. Or maybe more efficiently by doing the processing on the server:
file=$(ssh $USER#$HOST "ls -ltr /inbound/*.xml | tail -1 | awk '{print \$NF}'")
Now the shell variable file contains the name of the newest file. Then you can download that file with sftp as
sftp $USER#$HOST:$file .
Related
I try to open a File from within Windows10 in an emacsclient running on wsl2/Debian.
Upon startup I launch wsl/debian, X410 as X-server and the emacs daemon.
I can start a new emacs frame with emacsclient with the following startclient.bat:
#echo off
debian.exe run "if [ -z \"$(pidof emacsclient)\" ]; then export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0; emacsclient -c; pkill '(gpg|ssh)-agent'; fi;"
Then I created a shortcut so I can open a new emacs frame from the taskbar. To my surprise it works quite smoothly... so far. However, I can not figure out how to open a file with the emacsclient from within windows explorer.
I guess I would need to pass an argument to the emacsclient -c in the bat file, but how do I do this?
EDIT:
by doing the following:
debian.exe run "if [ -z \"$(pidof emacsclient)\" ]; then export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0; emacsclient -c "%1"; pkill '(gpg|ssh)-agent'; fi;"
I can open a file with emacsclient with:
startclient.bat file.txt
However when using open with the path to the file is messed up
instead of
/mnt/c/Users/path/to/file
I get
/mnt/c/Windows/system32/C:\Users\path\to\file
How would I go about passing the correct path as variable?
Not quite sure where the /mnt/c/Windows/system32/ is coming from, but you can convert the C:\Users\path\to\file into /mnt/c/Users/path/to/file by surrounding the %1 argument with wslpath, as in something like:
emacsclient -c "$(wslpath '%1')"
I'm trying to run the following command on each file of a directory.
svn blame FILEPATH | gawk '{print $2}' | sort | uniq -c
It works well however it only works on individual files. For whatever reason, it won't run on the directory as a whole. I was hoping to create some form of batch script that would iterate through the directory and would grab the file path and store it as a variable to be used in the command. However, I've never written a batch script nor do I know the first thing about them. I tried this loop but couldn't get it to work
set codedirectory=%C:\Repo\Pineapple% for %codedirectory% %%i in (*.cs) do
but I'm not necessarily sure what to do next. Unfortunately, this all has to be run on windows. Any help would be greatly appreciated. Thanks!
use for and find, similar to example on
https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
for i in $(find . -name "*.cs"); do
svn blame $i | gawk '{print $2}' | sort | uniq -c
done
I am trying to make a connection from a bash script, accomplishing something similar to this:
rmcguigan$ snowsql --filename test.sql
* SnowSQL * v1.1.86
Type SQL statements or !help
+----------------+
| GREATEST(1, 2) |
|----------------|
| 2 |
+----------------+
1 Row(s) produced. Time Elapsed: 0.108s
So I am running a bash file with chmod 755
>./test.sh
This is the contents of the file
./snowsql test.sql
syslog -s -l "Good to go"
results
rmcguigan$ ./test.sh ./test.sh: line 3: ./snowsql: No such file or
directory Unknown level: Good to go
How should my bash script call ./snowsql test.sql ?
It looks like the problem is that you have ./snowsql test.sql in your test.sh file, which tries to find and execute a file named snowsql in the current directory rather than running the command with that name. Try removing the ./ and using the --filename argument so that the file looks like this:
snowsql --filename test.sql
Make sure that you have a file named test.sql in your current directory as well with whatever SQL statement you want to execute.
Hi all. I gave a git-bash installed and want some automatisation. I've got a .bat file, which I want to run as
some.bat param | iconv -cp1251 > l.log | tail -f l.log
And I want to run it not in WinCMD but in git-bash shell - tell me plz how to do it?
Git bash on windows uses BASH. You can use bash to create a quick script that can take in parameters and echo them so that you can pipe them to your next utility.
It could look something like this
File: some.sh
#!/bin/bash
#Lots of fun bash scripting here...
echo $1
# $1 here is the first parameter sent to this script.
# $2 is the second... etc. $0 is the script name
then by setting some.sh as executable
$ chmod +x some.sh
You'll then be able to execute it in the git-bash shell
./some.sh param | cat ... etc | etc...
You can read about bash programming
I'd reccomend looking at some bash scripting tutorials such as this one
I am using the following script on a command line to kill a hypothetical notepad process (using a KornShell (ksh) in Windows XP, if that matters):
kill $(tasklist | grep -i notepad.exe | awk '{print 2}')
Now I take this line, and put it into a batch file c:\temp\testkill.bat, thinking that I should just as well be able to kill the process by running the batch file. However, when I run the batch file, I get the following awk error about unbalanced parentheses:
C:/Temp> ./testkill.bat
C:\Temp>kill $(tasklist | grep -i notepad.exe | awk '{print $2}')
awk: unbalanced () Context is:
>>> {print $2}) <<<
C:/Temp>
So I am baffled as to why I am getting this error about unbalanced parentheses when I run this script via a batch file, but have no issues when I run the command directly from the command line?
(I am not necessarily tied to this way of killing a process - I am additionally wondering why if I write the following on the command line:
tasklist | grep -i notepad.exe | awk '{print $2}' | kill
The process ID that comes out of the tasklist/grep/awk calls does not seem to properly get piped to kill.
Why are you making a batch file if you have a Korn shell? Write a shell script - that will probably help you out a lot.
I can answer your final question - kill doesn't take the PID to kill from the standard input, it takes it on the command line. You can use xargs to make it work:
tasklist | grep -i notepad.exe | awk '{print $2}' | xargs kill