NSIS switch with multi-criteria case - scripting

May I do in NSIS switch with multi-criteria case like in JavaScript?
${Switch} $0
${Case} 2
DetailPrint "Connection to server failed: $0"
Abort
${Break}
${Case} 1
${Case} 3
${Case} "timeout"
DetailPrint "Database creating failed: $0"
Abort
${Break}
${EndSwitch}

Related

NSIS close process by glob

I have a code for nsis which show you a message if specified process is run
nsProcess::_FindProcess "NCover.exe"
Pop $0
${If} $0 = 0
ifSilent +1 +3
SetErrorLevel 3
Abort
MessageBox MB_OK "Process NCover is running please close."
Goto Recheck
${EndIf}
But it's not work with a glob pattern like "*.res". What can i use to do this with glob?

Is there any if statment in awk to compare a word greped from a file?

I am new to any programming and shell scripting.
I am trying to make a if condition in shell script.
I am used of some computed codes for density functional theory (say Quantum espresso).
I want to make the program automatic via a shell script.
My code produce case.data which contains at the end stop (at $2 or we can say at second column).
For example below script should print stop
cat case.data | tail -n 1 | awk '{print $2}'
so if I get stop from above script then then if statement should not produce anything and the rest file should be executed. If I do not get stop from then above script then executable commands in my file should not be executed and a text file containing exit should be executed so that it terminates my job.
What I tried is:
#!bin/bash
# Here I have my other commands that will give us case.data and below is my if statement.
STOP=$(cat $case.dayfile | tail -n 1 | awk '{print $2}')
if [$STOP =="stop"]
then
echo "nil"
else
echo "exit" > exit
chmod u+x exit
./exit
fi
# here after I have other executable that will be executed depending on above if statement
Your whole script should be changed to just this assuming you really do want to print "nil" in the success case for some reason:
awk '{val=$2} END{if (val=="stop") print "nil"; else exit 1}' "${case}.data" || exit
and this otherwise:
awk '{val=$2} END{exit (val=="stop" ? 0 : 1)}' "${case}.data" || exit
I used a ternary in that last script instead of just exit (val!="stop") or similar for clarity given the opposite values of true in a condition and success in an exit status.
Like this?:
if awk 'END{if($2!="stop")exit 1}' case.data
then
echo execute commands here
fi

SED extract first occurance after 2 patterns match

I'm trying to use c-shell (I'm afraid that other option is not available) and SED to solve this problem. Given this example file with a report of all some tests that were failing:
============
test_085
============
- Signature code: F2B0C
- Failure reason: timeout
- Error: test has timed out
============
test_102
============
- Signature code: B4B4A
- Failure reason: syntax
- Error: Syntax error on file example.c at line 245
============
test_435
============
- Signature code: 000FC0
- Failure reason: timeout
- Error: test has timed out
I have a script that loops through all the tests that I'm running and I check them against this report to see if has failed and do some statistics later on:
if (`grep -c $test_name $test_report` > 0) then
printf ",TEST FAILED" >>! $report
else
printf ",TEST PASSED" >>! $report
endif
What I would like to do is to extract the reason if $test_name is found in $test_report. For example for test_085 I want to extract only 'timeout', for test_102 extract only 'syntax' and for test_435 'timeout', for test_045 it won't be the case because is not found in this report (meaning it has passed). In essence I want to extract first occurrence after these two pattern matches: test_085, Failure reason:
To extract "Failure reason" for the specified test name - short awk approach:
awk -v t_name="test_102" '$1==t_name{ f=1 }f && /Failure reason/{ print $4; exit }' reportfile
$1==t_name{ f=1 } - on encountering line matching the pattern(i.e. test name t_name) - set the flag f into active state
f && /Failure reason/ - while iterating through the lines under considered test name section (while f is "active") - capture the line with Failure reason and print the reason which is in the 4th field
exit - exit script execution immediately to avoid redundant processing
The output:
syntax
You can try handling RS and FS variables of awk to make the parsing easier:
$ awk -v RS='' -F='==*' '{gsub(/\n/," ")
sub(/.*Failure reason:/,"",$3)
sub(/- Error:.*/,"",$3)
printf "%s : %s\n",$2,$3}' file
output:
test_085 : timeout
test_102 : syntax
test_435 : timeout
If you don't care the newlines, you can remove the gsub() function.
Whenever you have input that has attributes with name to value mappings as your does, the best approach is to first create an array to capture those mappings (n2v[]) below and then access the values by their names. For example:
$ cat tst.awk
BEGIN { RS=""; FS="\n" }
$2 == id {
for (i=4; i<=NF; i++) {
name = value = $i
gsub(/^- |:.*$/,"",name)
gsub(/^[^:]+: /,"",value)
n2v[name] = value
}
print n2v[attr]
}
$ awk -v id='test_085' -v attr='Failure reason' -f tst.awk file
timeout
$ awk -v id='test_085' -v attr='Error' -f tst.awk file
test has timed out
$ awk -v id='test_102' -v attr='Signature code' -f tst.awk file
B4B4A
$ awk -v id='test_102' -v attr='Error' -f tst.awk file
Syntax error on file example.c at line 245
$ awk -v id='test_102' -v attr='Failure reason' -f tst.awk file
syntax

get exit code of a background process that completed a while ago

This may not be a completely new topic but I ran into a little bit odd situation.
I'm processing about 1000 files in a loop by kicking off a script in background. I want to take some actions on the files based on the exit code each process returns. By the time I go in a loop to wait for each process to complete I found that some of the process were already done. I modified the script to wait only if pgrep finds a process and just assumed a process completed successfully otherwise. The problem is- I have to know exit code of each process in order to take action on the corresponding file. Any ideas?
pid_list=()
for FILE in $SOME_FOLDER
do
(process with FILE as parameter) &
done
for pid in "${pid_list[#]}"
do
if pgrep $pid; then #process could have just completed as we got here
if wait $pid; then
echo "process $pid successfully completed!" >> $logfile
else
echo "process $pid failed!" >> $logfile
fnc_error_exit
fi
else
echo "assumed that process $pid successfully completed but I DON'T KNOW THE EXIT CODE!" >> $logfile
continue
fi
done
I cannot solve your problem exactly.
Since I do not know the exact situation of you, anyway, could you try another method using parent and child scripts ?
For example, the topmost script is like this:
for FILE in $HOME/*.txt
do
parent.sh $FILE &
done
Then, the parent.sh is like this:
child.sh $1
RC=$?
case $RC in
0 ) echo Exit code 0 for $1 >> parent.log
;;
1 ) echo Exit code 1 for $1 >> parent.log
;;
* ) echo Other Exit code $RC for $1 >> parent.log
;;
esac
The child script is like this:
grep -q hello $1
Then, the parent.sh will handle every exit code of the child.sh
All files will be handled by a parent.sh, without a missing handling.

Access Hbase with in awk script

I am able to access hbase via hbase and awk command
echo "scan 'abc'" | hbase shell | awk -F"=" '{print$4}'
but i am unable to write above command with in awk programming without system call,how can i get '{print$4}' within awk script so that i can process the data within the script.
EDIT
What i am doing right now is invoking a system call within my.awk and saving it in another file but i want to use the $4 within my.awk ,rather saving it in another file. what i have done is:
#!/bin/awk -f
BEGIN {
ftable="echo" " \"" "scan" " " "'abc'" "\" ""| hbase shell | awk -F" "" "= '{print$4}'> abc.txt";
print ftable;
system(ftable);
}
i think now it is much more clear,basically i dont want to invoke system call because it always return 0;Even shell will work but what i want is to get this printed output within script.