docker compose command with multiline batch command fails with if statement - batch-file

I have a docker compose file with a windows container service.
The command section takes a multiline batch command.
command:>
cmd /c "
if "%build%"=="release" (
cd some_dir ) else (cd other_dir) &&
7z.exe a some_z * &&
last_command & exit 0 "
with the if statement the multiline batch command always fails. Does anyone know why ? Thanks.

Related

sqlcmd Exit Code Handling in for....loop (Jenkinsfile) and handling domain based authentication

I have the below command which executes all the sql scripts in a folder.
I am using Jenkins Pipeline to execute the below command.
Problem :
In current scenario I have 4 scripts
Ex: 01.sql, 02.sql, 03.sql, 04.sql
If any script fails in first three and the last one executes successfully , it doesn't raise alert of failure in Jenkins, as the last exit code is Success.
Is there any other way to handle it ? Below is the Pipeline Section.
// Execute SQL Scripts
stage('Execution Scripts Section') {
steps {
script
{
bat """
cd src\\sql_scripts"
set target_db=${db}
for /R %%i in (*) do sqlcmd -S%db% -E -b -i"%%i"
"""
}
}
}
Additions :
SQL Server username and password works fine with -E(system user). How to pass a domain based user with password in sqlcmd ? I tried the below command it fails.
sqlcmd -S%db% -U%user% -P'%pass%' -b -i"01.sql"
A workaround is to separate your multi-line bat command into a series of single-line bat commands.
bat """"
cd src\\sql_scripts
"""
bat """"
set target_db=${db}
"""
bat """"
for /R %%i in (*) do sqlcmd -S%db% -E -b -i"%%i"
"""
Each one of those will throw an error if the command exits with a non-zero code (which is not necessarily the case for multi-line bat commands)

multiple command prompt command on single line

I created a batch file with 2 commands on a single line but I can't get it work.
What I'm trying to do is that create a subdirectory and copy test.pdf to DIR3 with new filename hello world.pdf.
Any thoughts guys? how about if the directory already exist?
mkdir W:\DIR1\DIR2\DIR3 && copy "W:\test\test.pdf" W:\DIR1\DIR2\DIR3\hello world.pdf
This is the syntax:
command1 && command2
Tested this for dir && echo foo and it works.
So your command becomes :
mkdir -p "W:\DIR1\DIR2\DIR3" && copy "W:\test\test.pdf" W:\DIR1\DIR2\DIR3\hello world.pdf
Maybe you could have used Google?
Oh and there was an extra " after copy in your command.

How to execute multiple commands in command prompt using batch file?

I am writing a batch file to execute the command (to execute Bitvise SFTP)
start cmd /k sftpc [SERVERNAME] -pk=1
After which the command prompt ask the following
Accept and (S)ave/(A)ccept for This Session/(C)ancel (S/A/C)?
Now the batch file should execute the next command as "A" to go through process.
I want automate the process of providing input "A" in command prompt. How can a batch file execute commands continuously ?
Updated :
After accepting the session, there are two more commands that needs to be executed,
cd [RemoteLocation]
get *.*
How to automate these two commands after the first two commands using the same batch file ?
You could try this to send an "A" to sftpc:
start cmd /k "echo A | sftpc [SERVERNAME] -pk=1"
I don't have sftpc and am just trying a suggestion to help out...
Updated:
If you have multiple commands to give to sftpc, you may find that one of the following methods works, depending on how sftpc works - I am working blind here - still don't have, or want, sftpc.
start cmd /k "echo A & echo cd somewhere & echo get *.* | sftpc [SERVERNAME] -pk=1"
... change "somewhere" to wherever you want to go to.
Or maybe:
Save the following in a file called ftp.cmd
A
cd somewhere
get *.*
Then try:
start cmd /k "sftpc [SERVERNAME] -pk=1 < ftp.cmd"

batch script to open an command window and run directory scan

I'm trying to create a batch script which opens a
1) command window
2) get the command prompt pointing to c (come to root)
3)run dir /s command which performs an extensive scan of the system starting from the root.
The window should not dissapear.
but i' m only able to come to come to c prompt and the command window disappears, i have posted the script here . Please let me know where exactly i'm going wrong .
start cmd /c cd / && dir /s
EDIT : The issue is fixed
ANSWER
start cmd /c
cd / && dir /s
You can use cmd with the /k switch to pass in your commands, such as:
cmd /k "pushd C: & cd\ & dir /s"

Clearcase find command in combination with Tcl Script and Batch-File: Redirection doesn't work

it's me again ;-)
I try to start the cleartool find command from a batch file. That batch files gets his arguments from a Tcl script.
Batch File:
SET VERARG="version(\main\LATEST) && !lbtype(%3)"
cleartool find . -version %VERARG% -print > Y:\x\Logs\%3.log
Tcl Script:
set v_cmd "y:\\x\\clearcase_find_change.cmd $v_drive $v_path $v_label \n"
set v_outp [exec cmd << $v_cmd]
When I start the batch file from command line, it works and it wrotes the hits to the log-file, but when I start it from Tcl, I see the hits in the command window, but it only creates the log-files with no content. I tested it already with a very simple batch file, which only contents a echo command and a redirection. That worked, I've also tried to use different drives, but with no success. I checked also the received arguments of the batch-file, but everything looked fine to me.
Simple Call, which worked:
Batch: echo %1 > Y:\x\%2.log
Tcl: set v_cmd "y:\\x\\simple_echo.cmd $v_lib $v_label \n"
Maybe it is missing a cmd directive to specify that the DOS session should execute and then quit:
set v_outp [exec cmd << $v_cmd /c]
or:
set v_outp [exec cmd /c << $v_cmd]
or:
set v_cmd "/c y:\\x\\clearcase_find_change.cmd $v_drive $v_path $v_label \n"
set v_outp [exec cmd << $v_cmd]
(to make sure the /c is passed first)
See Tcl/Tk FAQ:
exec cmd.exe >&#stdout <#stdin /c dir
will do the directory command for the current directory.
Check out the Windows help for the switches available under cmd.exe.
"/c" tells it to execute the command and then exit. "/k" tells it to execute the command and keep the DOS command interpreter active.
(Note that cmd.exe is the name of the MS-DOS interpreter on Windows NT.)

Resources