SFTP batch...cancel command if previous command fails - unix

I am running an SFTP batch to get a series of files from one unix server to another. I have a shell script that builds the batch files like:
cd path
get filename
cd path
get filename
and more.
Is there a way within SFTP to NOT run a get command if the preceding cd command fails, but keep running the rest of the batch?
To be clear, I do NOT want to terminate the whole SFTP batch, just the one get command if it's related cd command does not work.

After each cd check error code, if it is 0, everything is fine. If not, some error occured in last command...
Like:
cd directory
if [!$?]
then
get something
fi

Related

How to debug this scheduled .bat task in task scheduler using cmd and log file?

I'm trying to basically run the following .bat file as a scheduled task, while also logging errors in a .txt file:
In the 'program/script' box, I just have cmd. Then in the add arguments box I have:
/k ""T:\Some_folder\mybatchfile.bat" >>"T:\somelog.txt" 2>&1"
This had been working just fine originally before I tried to add the log function and calling cmd explicitly as seen in several posts, but I'd really like to add this function. I'm using /k for now so that I can watch the cmd window as things happen, but plan to replace it with /c so it closes when its done.
I tried many permutations of where my quotation marks are but am not having a lot of luck. I'm also intentionally using >> vs > in order to append the log, not overwrite it.
The contents of the .bat file are basically:
"C:\RDirectory\R.exe" CMD BATCH "T:\Some_folder1\Preworkforbatch.R"
copy T:\Some_folder2\some_data.csv "C:\Users\ABC1\Another_folder"
copy T:\Some_folder3\some_more_data*.csv "C:\Users\ABC1\Another_folder"
I'm wondering if part of it is that T is a network folder that is mapped? Thanks for your help.
edit:
Here is more info on the task:
"T:\Some_folder\mybatchfile.bat" >> "T:\somelog.txt" 2>&1
When redirecting the output of a batch file to a log file you will not see as much output in the cmd window. You have to repeatedly open, close, open the log file to see your progress. Use the title command in your batch to display progress info in the cmd window.
title This is %~F0. The time is %time%. The date is %date%. SLEEP 3600 FOR :RECORDRADIO
Ok this ended up working for me, by editing the batch file itself, and just running the batch file (not cmd explicitly) in the task scheduler:
mybatchfile.bat:
#echo on
"C:\RDirectory\R.exe" CMD BATCH "T:\Some_folder1\Preworkforbatch.R" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
copy T:\Some_folder2\some_data.csv "C:\Users\ABC1\Another_folder" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
copy T:\Some_folder3\some_more_data*.csv "C:\Users\ABC1\Another_folder" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
Writing the log file to the network was causing errors. Writing to the local computer solved this issue. Not using double quotes also was key, instead just quotes around the file/path. This setup gives me an output for each line, so for every line 2>71 shows up, I get an output if there's an error or a completion message.
This is what the task scheduler looks like:

nohup command getting permission issues

I am trying to run a filewatcher.sh script in background. It further invokes a java code through a jar file.
Whenever I run the command("nohup sh filewatcher.sh") through terminal, everything is ok. I have to hit enter two times so the output file is appended to nohup.out. But whenever I try to run the equivalent command("nohup filewatcher.sh >> nohup.out < /dev/null &") from a shell script automate.sh, I get following permission error.
error-log. Click to see snippet.
Could this an issue of permission for the automate.sh?
I have crossed check to see this script is having same permissions and ownership as the user when I log in and enter the nohup command from command line.
I also checked the ownership of the folders where the files are getting moved but everything is having same permissions and ownership.

Convert ZIP command from windows to unix

I am trying to recreate a zip command from windows in Unix and facing issues as expected.
Windows command is
zip "zipfilename.zip" -j -u -m -g -T -9 "sourcefiledirectory\*20181010.*
On UNIX the pattern *20181010.* is not getting identified and i am getting the message
"zip warning: name not matched".
Please help in getting over this.
You have specified the wildcard path in Windows format (i.e. using "\" for the path separator, rather than "/")
"sourcefiledirectory\*20181010.*"
try this instead
"sourcefiledirectory/*20181010.*"
I was able to execute the command on the command line in unix but was getting issues while trying to replicate the same through an ETL tool 'BODS'. The issue was fixed when i put the command into a .sh executable and called that executable from BODS by passing the zipfile name and source file pattern as parameters.

SSH key not found when using system() to send git commands to command prompt in windows

I am trying to execute git commands with a public SSH key using the system() function in R (on Windows 7 64 bit). When I try to execute a git command I get the following error:
ssh_askpass: exec(rpostback-askpass): No such file or directory
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I can run the exact same command on the command prompt and it works. I can also run the exact same command on the git bash shell and it works. I am not sure why cmd can't find the key when run from R using the system() command. Is there a way to specify the key location for the command prompt used by the system() command?
Update:
An example call that won't work:
system("git push --set-upstream git#gitlab.mygit.com:MyGroup/testGitlab.git master")
also
system("git clone git#gitlab.mygit.com:MyGroup/gitLabTest.git")
Both of these calls get the same error above.
Update 2: It appears that the ssh keys are not visible from R using list.files()
> list.files("C:/Users/Me/Documents/.ssh/", all.files = TRUE)
[1] "." ".."
I have verified that there are multiple ssh keys in this directory which are visible from the git bash shell using ls -a.
As it turns out, on my Windows system (7, 64 bit), when creating SSH keys with the git bash shell they were saved to "C:\Users\MyAcount\.ssh". However, the cmd.exe called by system() in R was looking in "C:\Users\MyAccount\Documents\.ssh".
For some reason, when using the cmd prompt called directly from the OS it looks in the same .ssh directory as the bash shell. Go Figure. Windows, I don't think I will ever understand you. Hope this helps someone out there.

Why am i able to run unix commands on my PC?

How am i able to execute UNIX commands on my PC Command prompt? Note i do not have cygwin installed, although i was going to before i discovered this.
This is a development machine so i have a lot installed on it like ruby, python, git, github, node and so on.
What does this mean? can i use this without cygwin?
Here is a list of programs installed on my PC program list
How am I able to execute UNIX commands on my PC Command prompt?
You can use the where command in a cmd shell to find out the exact location of your Unix commands, for example:
where ls
This assumes, of course, that ls is located somewhere in your current PATH.
The location returned will show you in which directory your Unix commands are installed and may be enough for you to determine how they were installed.
The where command is roughly equivalent to the Unix which command.
By default, the search is done in the current directory and in the
PATH.
Syntax
WHERE [/r Dir] [/q] [/f] [/t] Pattern ...
WHERE [/q] [/f] [/t] [$ENV:Pattern
Source where
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
where - Locate and display files in a directory tree.
Running Unix commands in windows can be done by having a tool like Cygwin which has those commands.
You can also get many of those commands compiled for windows and then run them using the command with the full path or only the command if the executable is in a path known by adding the paths to the executable files in Windows by :
1) Running in the terminal: PATH %PATH%;C:\<new_path>
2) Creating command aliases like: doskey np=C:\<new_path>\new_command.exe $*. $* is used to be able to transmit parameters

Resources