start bash shell from where it stopped(due to error) - bash

I'm working on bash Unix shell. My script executes five 4gl files in order.if an error occurs in any of the files, the Scrip stops. But the problem is when we clear the error in the file and execute the script again..it should start from when it stopped (i.e from the step where the error is cleared)not from the first file.

You'll have to do this yourself:
if ! some-check-for-first-commmand; then
do-something-with first.4gl
fi
if ! some-check-for-second-commmand; then
do-something-with second.4gl
fi
# and so on
where "some-check-for-..." is something you write that checks if that 4gl file has been processed. It might look for some text in an output file, or the timestamp of an output file, or whatever you can do.

Related

Centreon not printing output from bash script

I have a bash script that do checks and show result correctly in local server
but when using graphic web of Centreon the message is empty
I use echo as output of function called from main
Thanks for all, the problem it is when i run script in Nagios-Centreon
i have this outpout NRPE: Unable to read output in OS AIX
the code is large, i resolve it by changing #!/bin/ksh instead of
#!/bin/bash
Basically the problem is because of special caracters in script shell.
https://support.nagios.com/kb/article/nrpe-nrpe-unable-to-read-output-620.html#:~:text=This%20error%20implies%20that%20NRPE,when%20running%20the%20remote%20plugin%20.

How to send a command using AppleScript to terminal one by one and save the output, which is not writable to file anywhere?

So, I have a problem. I have downloaded a program from the web. And it's a command line app. I have written a code, which generated some n-k commands to the app. I have written them into an output file. I can write an app in Python, but it freezes on some of the commands. I have tested them manually and seems like there are two issues:
Commands must be run one-by-one;
Some of the commands give an output like bla-bla-bla, this thing is not written into an output file. So, if I run a command ./app -p /file1 -o /file2 -s -a smth- > /fileOutput.txt The fileOutput.txt is empty, though in the terminal, there's is this bla-bla-bla message, stating, that something is wrong. If the command gives bla-bla-bla the app may freeze for a while.
Here is what I want to do:
CD into folder, the containing app;
For command in fileWithCommands perform command and start the next, only when the previous finishes;
If the command gives message, containing bla-bla-bla (cause it may look like file1 bla-bla-bla), write the command and this strange output into file badOutputs.txt.
Have never done applescript before. However, this's what I've done so far:
set theFile to "/Users/MeUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
#I have found the previous code here: http://alvinalexander.com/mac-os-x/applescript-read-file-into-list-array-examples
close access fileHandle
tell application "Terminal"
activate
do script "cd /Users/MeUser/Desktop/anApp/"
repeat with command in arrayCommand
do script command
end repeat
end tell
Though there's a problem, if in one window the commands make up a huge queue. Without window 1 cd and the command are in different windows. And I am still unable to save the output.
UPDATE
Did with accordance to #Mark Setchell's recommendations. So now I have such code:
set theFile to "/Users/meUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
close access fileHandle
repeat with command in arrayCommand
do shell script "cd /Users/meUser/Desktop/App/; " & command
end repeat
To the command I have added the following:
2>&1 /Users/meUser/Desktop/errorOut.txt
However, the apple script says that a mistake of the app is the mistake of the script. I.e.: file corrupted, app fails. I want it to write into error file where has it failed and move to the next command, while the script just fails.
Maybe not a complete solution, but more than a comment and easier to format this way...
First Issue
Your command-line app which writes on the Terminal may be writing to stderr rather than stdout. Try redirecting stderr to the same place as stdout by using
./app -p ... > /FileOutput.txt 2>&1
Second Issue
You cannot do:
do shell script cd somewhere
do shell script do_something
because each do shell script will execute in a separate, unrelated process. So your first process will start - in the default directory like all processes - and correctly change directory and then exit. Then your second process will start - in the default directory like all processes - and try to run your command. Rather than that, you can do this:
do shell script "cd somewhere; do_something"
which starts a single process which changes directory and then runs your command line program there.
Issue Three
Why do you want to send your commands to Terminal anyway? Does the user need to see something in Terminal - seems unlikely because you want to capture the output, don't you? Can't you just run your commands using do shell script?
Issue Four
If you want to keep your normal output separate from your error output, you can do:
./app ... params ... > OutputFile.txt 2> errors.txt
Suggestion 1
You can retain all the errors from all the scripts and accumulate them in a single file like this:
./app .. params .. >> results.txt 2>&1
That may enable you to deal with errors separately later.
Suggestion 2
You can capture the output of your shell script into an Applescript variable, say ScriptOutput, like this, then you can parse it:
set ScriptOutput to do shell script "..."
Suggestion 3
If errors caused by your script are stopping your loop, you can enclose them in a try block like this so they are handled and everything continues:
try
do shell script "..."
on error errMsg
display dialog "ERROR: " & errMsg
end try

Date checker script

I need help with writing a bash script. I have very little knowledge on scripting, but am willing to learn.
I have a file with lines that looks like this: 1204200141345_song.mp3. The numbers are date and time like day=12, month=04, year=2014, hour=13, min=45
I need a script that will read through the file and if a line matches the current date and time it will play the mp3 via mplayer/mpg123 at the correct time. When the mp3 ends it must continue. The file changes all the time. New lines go in and other line are removed. The script will run as long as the pc is on.
I am not sure where to start, any advice is appreciated.
So, you willing to learn - some starting points...
The script will run as long as the pc is on.
Ok, this can be achieved with a loop what will run forever, like the next one:
while :
do
some_commands
done
When the mp3 ends it must continue.
As above - if the some_commands contains the command to playing the file - the script will wait until the command finishes and will start over - again, and again, and again... forever...
The file changes all the time. New lines go in and other line are
removed.
Don't need to care until it is a file...
... if a line matches
the current date and time it will play the mp3 via mplayer/mpg123 at
the correct time.
So, first you need get the date in the needed format. This can be achieved with the date command. (man date)
The needed format is %d%m%Y%H%M, so the
date +"%d%m%Y%H%M"
will print the current date/time in the wanted format
assing the result of the command to some variable in bash can be done with the
variable=$(command argumens)
so,
datestr=(date +"%d%m%Y%H%M")
echo $datestr
will assing the current date/time to variable datestr.
... read through the file and if a line matches the current date and time ...
The Read-thru and match can be done with the grep command (man grep), so, you need use
grep "what_want_to_match" filename_where_want_to_match.txt
The grep exits with a status codes
0 - found a match
1 - match not found
2 - the file doesn't exists
The exist status of previous command is stored in the special variable $?. You can test the exit status, for example with the bash's case construction, like:
some_command
case $? in
2) echo "The some_command exited with status 2" ; exit 2 ;;
1) echo "with status 1" ; do_something_else ;;
0) echo "normal exit" ;;
esac
Now, you have enough informations to try write the script yourself, and if you meet some specific error - ask again... ;)

unix tee command not working as expected

While i'm using the following command in unix command prompt everything is working fine,log fiel is creating fine.
ls -l|echo "[`date +%F-%H-%M-%S`] SUCCESS - FILES"|tee -a logger2.log
but using the same thing in side the shell script it is showing error
No such file or directory.
I'm not getting what is the problem here!!
If I read between the lines: you want a list of files, followed by a date and a message?
try:
{ ls -l ; echo "[$(date "+%F-%H-%M-%S")] SUCCESS - FILES" ; } |tee -a logger2.log
That should give you in 'logger2.log' of the current directory the lines
.................... file
.................... file2
(ie the list of all files and dirs in the current dir, EXCEPT those starting with a ".")
[2013-12-26-..-..-..] SUCCESS - FILES
Please note that, if you put nothing in front of the script, it could be started by a different shell than the one you use when testing. It depends what/who does invoke the script : if it is a crontab, it will probably be interpreted by "sh" (or by "bash" in "sh-compatibility" mode)...
Please tell us what the above gives you as output, and how you start the script (by a user, on the prompt? or via a crontab [which one: /etc/crontab ? or a user's crontab? which user?], etc. And what error messages you see.

rtorrent execute shell script

I can't figure out how to get output from shell script back to rtorrent after command has been executed.
Is it possible to return back output from exeternal command back to rtorrent session?
I use rtorrent scripting interface to auto execute shell command after torrent is finished
event line in .rtorrent.rc looks like this:
system.method.set_key = event.download.finished,mycustomcommand,"execute=~/myshellscript.sh"
myshellscript.sh file looks like this
#!/bin/sh
echo "Torrent finished!"
Is there a way to do this?
I'm not sure what you're searching for, but I found this on rtorrent's wiki site:
execute_capture_nothrow={command,arg1,arg2,...}
This will execute the external command with arguments arg1,arg2,.... It will return the
stdout output of the command.
system.method.set_key = event.download.finished,mycustomcommand,print="$execute_capture=/path/to/script"
should work, at least
print="$execute_capture=/path/to/script"
works when you do it inside rtorrent. If you want to store the output then intstead of print use d.custom1.set= if that helps.
You forgot to add parameters to the rtorrent.rc itself and also the bash script is incomplete according to me.
.rtorrent.rc line should have
method.set_key = event.download.finished,whatever,"execute2={/path/myscript.sh,$d.name=,$d.base_path=,$d.hash=}"
bash script
#!/bin/bash
TORRENT_NAME=1
TORRENT_PATH=2
TORRENT_HASH=3
touch "$1" Finished download!
exit
this will create touch file telling you particular file has finished downloading.

Resources