Cant use default option more than one time(s) - windows

i am trying to use the "timeout" command in a batch script, and it says "ERROR: Invalid syntax. Default option is not allowed more than one time(s)" and it closes the script after.
Can anyone help me fix this? The code is as follows
#echo off
title {REDACTEDPRIVATESTACKOVERFLOW}
echo {REDACTEDPRIVATESTACKOVERFLOW}
timeout /t 3 /timeout
echo {REDACTEDPRIVATESTACKOVERFLOW}

It just worked for me in the following way:
echo "hi"
timeout 3
echo "hi"

Related

Bash script, update output once command is ended

I have tried different ways but none of them have worked so far.
echo "Starting"
checklocation(){
if (command blabla)
then locationOne=$"Found"
else locationOne=$"Not found"
fi
}
checklocation &
echo "Let's check: " $locationOne
echo "Ending"
As my command take long time to provide the results I'd like to proceed to print all the output and show the value of $locationOne once the result is ready. The following code works fine printing all the output at once however the $locationOne doesn't appear. I tried with printf and \r too without luck. Any suggestions?
To clarify, I would like to load the variable value where the arrows are pointing once the command completes
echo "Starting"
checklocation(){
if (command blabla)
then
locationOne="Found"
else
locationOne="Not found"
fi
}
echo "Calling function"
checklocation
echo "Let's check: " $locationOne
echo "Ending"
try following the above corrections,
Remove the "$" when assigning the locationOne variable
Also while calling the function remove "&", ignore this it is considered as an argument.
Goodluck !!
You want to go back and amend screen output later.
This is very difficult in the general case, but you can simplify it dramatically by making sure that the output you write doesn't scroll the screen in such a way that it's hard to predict where the amendment will have to be made.
This example does it by clearing the screen first, so that any extra output is unlikely to scroll. It can then update by coordinates:
#!/bin/bash
check() {
sleep 3
tput sc # Save cursor
tput cup 1 14 # Set y,x coordinates
printf '%s' "Found"
tput rc # Restore cursor
}
check &
clear # Clear screen so we know where we are
echo "Starting"
echo "Let's check: "
echo "Ending"
wait
This shows:
Starting
Let's check:
Ending
for three seconds, then it updates it to:
Starting
Let's check: Found
Ending
Alternative approaches include:
Keeping the data you want on screen in a string, and just clearing+writing whenever you want to update the full screen
Tracking the number of lines you write, then using the "cursor up" ANSI command (tput cuu) to move up to where you believe the line to amend will be.
You can use wait to wait till child process status is changed. (check man wait)
#for demo puposes , I have manually added sleep of 10 sec.
long_running_command()
{
sleep 10
echo "Hey, I am long running command....uh"
}
long_running_command & #<-using & to send this function to BG
echo "I am normal command"
wait #waiting till the child is terminated.
The above script will result in the following output:
I am normal command
Hey, I am long running command....uh

Passing parameter from window bat to UNIX shell script

i know topic seems very familiar and there are many answers given for same, but my issue is something else.I have a .bat file in which i am passing more then 10 parameters(I know limit is 9 but i need it).It seems to be working fine.Script is below-:
#ECHO OFF
setlocal EnableDelayedExpansion
set n=0
for %%a in (%*) do (
set vector[!n!]=%%a
set /A n+=1
)
SET sadminUser=%vector[0]%
SET sadminPassword=%vector[1]%
SET dsnProvider=%vector[2]%
SET dbUserName=%vector[3]%
SET schemaFilePath=%vector[4]%
SET ddldictschemalogPath=%vector[5]%
SET repositoryName=%vector[6]%
SET ddlimpschemalogPath=%vector[7]%
SET grantUserRole=%vector[8]%
SET userId=anyuser
SET host=myhost
SET password=password
SET datatable=%vector[9]%
SET indexTable=%vector[10]%
SET ddlimpschemalogPath2=%vector[11]%
echo %ddlimpschemalogPath2%
echo y | "C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% exit
"C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% "PATH=/bin:/usr/bin:/usr/local/bin /opt/siebel/w44gq8sw/DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
Now as you see i am doing echo %ddlimpschemalogPath2% which is last parameter in array,and i can see correct output as well.
Problem is when i try to pass these parameters into UNIX Shell Script.You can see i am doing in bat file using putty command line Plink.I can successfully connect to shell script as well,And i am trying to echo all the passed parameters in shell script.But facing some issue . Script is below-:
#!/bin/bash
sadminUser=$1
sadminPassword=$2
dsnProvider=$3
dbUserName=$4
schemaFilePath=$5
ddldictschemalogPath=$6
repositoryName=$7
ddlimpschemalogPath=$8
grantUserRole=$9
datatable=${10}
indexTable=${11}
ddlimpschemalogPath2=${12}
echo "$sadmimUser"
echo "$sadminPassword"
echo "$ddlimpschemalogPath2"
echo password | sudo -S -l
sudo host << EOF
// Do something else
EOF
I Found one issue. In .bat i have parameter called -: repositoryName . When i do echo %repositoryName% it gives "Siebel Repository" correct output. Now when this spaced value is passed as parameter to shell script it breaks into 2 different values so:
These two parameter in script take 2 different values-:
repositoryName=$7
ddlimpschemalogPath=$8
Output-:
Siebel
repository
And it should be one value for parameter repositoryName=$7 . Giving value Siebel Repository. Why is this happening? Can this me issue why value are up and down?
As you can see in .bat i am making call to shell script, and passing parameters taken from .bat file as below-:
DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
You can see complete line above in .bat.
Invoking .bat file -:
dlSync.bat "SAD" "glob81" "gepf_DSN" "SIEBEL" "/global/u70/globepfdev/siebel/schema.ddl" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/expschem.log" "Siebel Repository" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync1.log" "SSE_ROLE" "GLOB_DATA_SMALL" "GLOB_INDEX_SMALL" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync2.log"
Same parameter are passing out to shell script . where "Siebel Repository" breaks into 2. Thanks

Error codes seemingly ignored by '&&' on Windows command prompt

I have some Batch scripts I use for automating application build processes, most of which involve chaining commands together using the && operator. Admittedly, I'm more experienced with Linux, but based on that experience some_command && other_command should result in other_command being run iff some_command returns an exit code of 0. This answer and this answer seem to agree with that. However this appears not to be the case on Windows cmd.exe, all of the scripts run regardless of the error code of the previous.
I decided to make a simple test for this to convince myself I wasn't going insane. Consider this test.bat, which returns an exit code of 1:
#echo off
EXIT /B 1
Running test.bat && echo This shouldn't print prints 'This shouldn't print'. But since the exit code is clearly 1, echo should not be called. I've tested that the error code was actually 1 using the %errorlevel% variable, they're coming out as expected (0 before I run the script, 1 after).
On Linux I tried the same thing. Here's test.sh:
#!/bin/bash
exit 1
Running ./test.sh && echo "This shouldn't print" gives no output, exactly what I expected.
What's going on here?
(Note: OS is Windows 7 Enterprise)
You need to use call to run the batch script, like this:
call test.bat && echo This shouldn't print
Without call, the && operator does not receive the ErrorLevel returned by the batch script.
When you run a batch file from within another one, you need to use call in order to return to the calling batch file; without call, execution terminates as soon as the called batch file has finished...:
call test.bat
echo This is going to be displayed.
...but:
test.bat
echo You will never see this!
When running test.bat is involved in a command line where multiple commands are combined (using the concatenation operator &, the conditional ones && and ||, or even a block of code within parentheses ()), all the commands following test.bat are ecexuted even if call was not used. This is because the entire command line/block has already been parsed by the command interpreter.
However, when call is used, the ErrorLevel value returned by the batch file is received (which is 1 in our situation) and the following commands behave accordingly:
call test.bat & echo This is always printed.
echo And this is also always printed.
call test.bat && echo This is not printed.
call test.bat || echo But this is printed.
(
call test.bat
echo This is printed too.
echo And again this also.
)
call test.bat & if ErrorLevel 1 echo This is printed.
But without call you will get this...:
test.bat & echo This is printed.
echo But this is not!
...and...:
test.bat && echo Even this is printed!
echo Neither is this!
...and...:
test.bat || echo But this is not printed!
echo And this is not either!
...and:
(
call test.bat
echo This is printed.
echo And this as well.
)
It seems that the && and || operators receive an ErrorLevel of 0 -- even in case ErrorLevel has already been set before test.bat is executed, strangely. Also when if ErrorLevel is used, the behaviour is similar:
test.bat & if ErrorLevel 1 echo This is not printed!
...and...:
set = & rem This constitutes a syntax error.
test.bat & if ErrorLevel 1 echo This is still not printed!
Note that the commands behind test.bat execute after the batch script, even without call.

Introduction to batch scripting

This is my shell script
echo "Name"
read name
if [ "$name" == "abcd" ]; then
echo "correct name"
else
echo "wrong name"
fi
echo "Password"
read password
if [ "$password" == "pwd" ]; then
echo "Correct password"
else
echo "Wrong password"
fi
echo "City"
read city
if [ "$city" == "bangalore" ]; then
echo "correct city"
else
echo "wrong city"
fi
I'm totally new to batch scripting.How do i write an equivalent .bat file?
There are two things you need to read up on:
How to prompt for user input: see the set command and especially set /p. You can get information about this by typing help set at the prompt.
How to test string equality: The if command has some info on this, type help if at the prompt to get some details.
When you have these things in place, you should be able to replicate the behavior of your bash script.
Have a look here for how to get input:
Batch File Input
Edit: Link contents (no longer available) was:
The following method will work on Windows 2000 and XP:
set INPUT=
set /P INPUT=Type input: %=%
echo Your input was: %INPUT%
If user hits ENTER without typing anything, the variable (in this case, %input%) keeps it value. So, the first line is to reset its value, so that if nothing is entered the variable will have no value at all, instead of some senseless value.
As you can see, that accepts blank inputs. You can make it to don't accept:
:input
set INPUT=
set /P INPUT=Type input: %=%
if "%INPUT%"=="" goto input
echo Your input was: %INPUT%
So, after getting user's input and saving it in a variable, you can use it as you will.
Consider running bash script on windows instead of porting script. See bash-shell-for-windows. It might be easier approach.

capturing error message from echo in cmd prompt

I'm writting out some text to a text file within a cmd batch script like such:
echo FlagValue=Y>>flag.txt
This normally works fine but occassionally if the text file is open by a different process, an error messgae is returned saying Access Denied. What I'd like to do is stop the batch file if an error occurs with something like:
if return_code GEQ 1 GOTO ERR
But can't find a return code from echo command. Does one exist, or is there a better tactic to use to capture error message?
echo FlagValue=Y>>flag.txt || echo access_denied Ensure you have rights
or
echo FlagValue=Y>>flag.txt
if /i %errorlevel% NEQ 0 do (
echo access_denied Ensure you have rights
call sendmail.cmd
)
Sample:
C:\Users\Me\Desktop>echo Hello > MyFile.txt || echo ERROR
Access is denied.
ERROR
C:\Users\Me\Desktop>echo Hello > a.txt || echo ERROR
C:\Users\Me\Desktop>
Everytime you run a command the ERRORLEVEL environment variable is set to your command's return. So try echo %ERRORLEVEL% straight after you run your command. (Be careful as any command you run inbetween (including echo) will override the %ERRORLEVEL%.
Also, check these out for more information:
Can a batch file capture the exit codes of the commands it is invoking?
Batch Files - Error Handling

Resources