Batch file doesn't execute completely after other .exe is executed - windows

I'm trying to create batch file which should have this commands:
cd "c:\Program files\NuSMV\2.5.2\bin\"
NuSMV -int short.smv
go
pick_state -r
print_current_state -v
simulate -r 3
show_traces -t
show_traces -v
The problem I encounter is this: after the second line is executed, NuSMV.exe runs in cmd and rest of the commands don't execute until I exit NuSMV, but I want to run commands 3-8 in NuSMV. What do I need to change in my .bat file. Thanks.

Put commands 3-8 in a text file (eg., cmds.txt), then run NuSMV as follows:
NuSMV -int short.smv -source cmds.txt
From the manual (nusmv.pdf) p.48:
It is also possible to make NUSMV read
and execute a sequence of commands
from a file, through the command line
option -source:
system prompt> NuSMV -source cmd file

Completing Vik answer, you can create the NUSMV commands file in the same BAT file
#echo off
pushd "c:\Program files\NuSMV\2.5.2\bin\"
echo go >"%TEMP%\cmds.txt"
echo pick_state -r >>"%TEMP%\cmds.txt"
echo print_current_state -v >>"%TEMP%\cmds.txt"
echo simulate -r 3 >>"%TEMP%\cmds.txt"
echo show_traces -t >>"%TEMP%\cmds.txt"
echo show_traces -v >>"%TEMP%\cmds.txt"
NuSMV -int short.smv -source "%TEMP%\cmds.txt"
del "%TEMP%\cmds.txt"
popd
Additionally I would recommend you to not change the current directory to NuSMV directory. Either editing the PATH, or just specifying NuSMV with its full path. In both cases you should then invoke the BAT from the current directory where short.smv is located.C
#echo off
echo go >"%TEMP%\cmds.txt"
echo pick_state -r >>"%TEMP%\cmds.txt"
echo print_current_state -v >>"%TEMP%\cmds.txt"
echo simulate -r 3 >>"%TEMP%\cmds.txt"
echo show_traces -t >>"%TEMP%\cmds.txt"
echo show_traces -v >>"%TEMP%\cmds.txt"
"c:\Program files\NuSMV\2.5.2\bin\NuSMV" -int short.smv -source "%TEMP%\cmds.txt"
del "%TEMP%\cmds.txt"

Related

Can not write variable to any file in bash scripting using ~ in path?

I'm writing bash script to automate java 8 installation and setting up home variable. Its a part of a big automation script.
I have completed everything and able to print path of home variable. But I can't seem to write or append in any file when I'm using ~ in the path.
Example
Not work: echo "$writeit" >> ~/.bashrc
Works: echo "$writeit" >> /home/geek/.bashrc
Output of my script states empty JAVA_HOME variable
These commands works perfect if I indivisibly run in terminal.
CODE
#!/bin/bash
LOG_FILE="java.log"
touch $LOG_FILE
exec > >(tee $LOG_FILE) 2>&1
echo "Java Installation Succeded"
echo $JAVA_HOME
javapath=$(ls -l `which java` | grep -oP "(?<=\-\>\s)(.*)")
javahome=$(ls -l ${javapath} | grep -oP "(?<=\-\>\s\/)(.*)(?=bin)")
writeit="export JAVA_HOME=${javahome}"
echo "$writeit"
echo "$writeit" >> ~/.bashrc
source ~/.bashrc
echo 'JAVA_HOME is set'"${JAVA_HOME}"

How to run .bash file in Mac?

I want to execute
Path : "/Users/Trans/Downloads/solr-6.1.0"
Command: "bin/solr start -p 8983"
and
Path : " /Users/Trans/Downloads/apache-activemq-5.12.0/bin"
Command: "./activemq console"
To make it executable on click I create one .command file
having
!#/bin/bash
"/Users/Trans/Downloads/solr-6.1.0" bin/solr start -p 8983
"/Users/Trans/Downloads/apache-activemq-5.12.0/bin" ./activemq console
Given chmode +x permissions to it
But something seems to be wrong, Code is not getting executed.
My aim is to create one batch/bash file on double clicking on it should open terminal and execute both commands or more than that.
Shebang begins with #! not !#
#!/bin/bash
# verbose mode ( or -v in shebang )
set -v
cd "/Users/Trans/Downloads/solr-6.1.0" && bin/solr start -p 8983
echo "press a key to continue"
read -n1
cd "/Users/Trans/Downloads/apache-activemq-5.12.0/bin" && ./activemq console
echo "press a key to continue"
read -n1

Return code from shell script run from PuTTY to calling batch file?

I would like to ask a question about how to apply PuTTY and control its return values. My setup is like this.
There's a xxxxx.bat file which contains PuTTY call with a ssh connection in the likes of:
putty.ext ssh 10.10.10.10 -l xxxx -pw yyyy -t -m wintest.txt
The file wintest.txt contains:
echo "wintest.txt: Before execute..."
/home/tede/n55115/PD/winlinux/RUN.lintest.bsh
echo "wintest.txt: After execute..."
echo $?
The file lintest.bsh contains various commands, what interests me is to be able to capture the return value of a specific command in the .bsh file, and call on that value from the bat file, to add it into an if loop with a warning, ie if $? (or %ERRORLEVEL - I don't know what would work) then BLABLA
I've read a lot of posts about this, but frankly this is my first time doing anything with .bat files so its all slightly confusing.
First, do not use PuTTY for automation, use Plink (PuTTY command-line connection tool).
But even Plink cannot propagate the remote command exit code.
You can have the remote script print the exit code on the last line of its output (what you are doing already with the echo $?) and have the batch file parse the exit code:
#echo off
plink.exe putty.ext ssh 10.10.10.10 -l xxxx -pw yyyy -t -m wintest.txt > output.txt 2>&1
for /F "delims=" %%a in (output.txt) do (
echo %%a
set "SHELL_EXIT_CODE=%%a"
)
if %SHELL_EXIT_CODE% gtr 0 (
echo Error %SHELL_EXIT_CODE%
) else (
echo Success
)
But of course, you have to fix your script to return the exit code you want (the current code returns exit code of the previous echo command):
echo "wintest.txt: Before execute..."
/home/tede/n55115/PD/winlinux/RUN.lintest.bsh
EXIT_CODE=$?
echo "wintest.txt: After execute..."
echo $EXIT_CODE

Can I make bash report errors only errors at the end of a script?

I have a bash script which sequentially executes many tasks.
However, because I do not want to see simple status messages (such as the long output of yum -y update), I ignored all those messages using:
#!/bin/bash
(
yum -y update
cd /foo/bar
cp ~/bar /usr/bin/foo
...
...
) > /dev/null
This does the job just fine, but what if something went wrong, like cp failed to copy some file? If this happens, I would like to catch the error and exit immediately, before the process continues.
How can I exit the process and show the related error? Normally,
an if/else clause would have to be used, like this:
#!/bin/bash
yum -y update
if [ $? -ne 0 ]; then
echo "error "
exit 1
fi
But the problem with this approach is that it would show the process; therefore, I would have to use > /dev/null on each line; more importantly, if I had more than 100 things to do, then the I would have to use many if/else statements.
Is there a convenient solution for this?
Rather than running your commands in (...) use set -e OR bash -ec to execute them:
bash -ec 'set -e
yum -y update
cd /foo/bar
...
...
cp ~/bar /usr/bin/foo' > /dev/null 2> errlog
OR using set -e:
(
set -e
yum -y update
cd /foo/bar
...
...
cp ~/bar /usr/bin/foo
) > /dev/null 2> errlog
-e option will make sure to exit the sub shell as soon as an error occurs.

How To get this script to run on your server - Bash Script

#!/bin/bash
echo "Content-Type: image/gif"
echo
cat x.gif
OUTPUT=" $QUERY_STRING,$REMOTE_ADDR,$HTTP_USER_AGENT,$HTTP_REFERER,`date`"
echo $OUTPUT >> log.txt
sleep 2.5
You can try this:
save the script into some file, for example script.sh
go to the directory, where the file exists, like cd my_folder
change file mode to executable with command: chmod +x script.sh
run the file: ./script.sh
That should work for you.
I remember, once I was also looking for the answer on exactly the same question.

Resources