Shell script doesn't fail when calling routine that is not installed - linux

I have a simple shell script for installing Jenkins plugins:
installPlugin() {
{...}
}
for f in ${plugin_dir}/*.hpi ; do
#without optionals
deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
for plugin in $deps; do
installPlugin "$plugin" 1 && changed=1
done
done
I'm calling this script from a Dockerfile like so:
RUN JENKINS_HOME=$JENKINS_HOME \
http_proxy=$http_proxy \
https_proxy=$https_proxy \
$JENKINS_HOME/install_plugin.sh \
ace-editor:1.1 \
Even though the unzip routine is not installed in the Dockerfile, when I run docker build the output is still SUCCESSFUL even though unzip is missing. I would like to fail the build if the following step fails:
deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
Any help is much appreciated!
LE
The script outputs the following:
/var/jenkins/install_plugin.sh: line 62: unzip: command not found
deps=
However, the docker build doesn't fail even if that script failed:
Successfully built 54f5a5ec567d

I ended up doing an empty check like this:
for f in ${plugin_dir}/*.hpi ; do
manifestMf=$(unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r')
if [ $manifestMf=="" ]; then
echo "Invalid META-INF/MANIFEST inside $f"
exit 1
fi;
#without optionals
deps=$(echo $manifestMf | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
# with optionals
#deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | tr '\n' ' ' )
for plugin in $deps; do
installPlugin "$plugin" 1 && changed=1
done
done
done

Related

Remove text from a txt file that is in an array

So I have these two functions: cMinOption and cMaxOption.
cMinOption puts all words from pt.stop_words.txt into myArray.
cMaxOption shows a list of words from ficha01.pdf.txt, including on that same list of words from pt.stop_words.txt.
I want to know how can I delete the words from the array I created and delete those words from the list that appears in the function cMaxOption.
cMinOption(){
declare -a myArray
mapfile -t myArray < pt.stop_words.txt
sed -i 's/myArray/""/g' ficha01.pdf.txt
sed -e 's/[^[:alpha:]]/ /g' ficha01.pdf.txt | tr '\n' " " | tr -s " " | tr " " '\n'| tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | nl | head -n 7
for ((i=0; i<204; i++)); do
echo "Element [$i]: ${myArray[$i]}";
done
}
# Called when -C is passed as argument
cMaxOption(){
# Check if the file passed in $1 is a PDF file
if [ $(head -c 4 "$1") = "%PDF" ]; then
pdftotext $1 $1.txt
echo "'$1': PDF file";
file="$1.txt"
else
echo "'$1': TXT file";
file="$1"
fi
echo "[INFO] processing '$file'"
echo "STOP WORDS will be counted"
echo "COUNT MODE"
sed -e 's/[^[:alpha:]]/ /g' $file | tr '\n' " " | tr -s " " | tr " " '\n'| tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | nl | head -n 7
mv $file result---$file
echo "RESULTS: 'result---$file' "
ls -l result---$file
echo -e " $(sort -u result---$file | wc -l) distinct words"
}

Print result in order

I have an input file txt, like this
999844634|715717657|1508241298 |000018995|INSTALACION DECO + PUNTO TV ADIC DIGITAL|000000001|ALTA |73479107 |2019-03-15|246221122|0001|671564720|002|DNI|02842909 |
999844634|715717657|1508241298 |000021932|DECODER HD ZAPPER COMODATO |000000001|ALTA |73479107 |2019-03-15|246221122|0001|671564720|002|DNI|02842909 |
999844634|715717657|1508241298 |000021932|DECODER HD ZAPPER COMODATO |000000001|ALTA |73479107 |2019-03-15|246221122|0001|671564720|002|DNI|02842909 |
999846100|745304617|1501278792 |000018995|INSTALACION DECO + PUNTO TV ADIC DIGITAL|000000001|ALTA |12544155 |2019-03-15|248309282|0002|774235318|003|DNI|29600747 |
999846100|745304617|1501278792 |000021148|BLOQUE CATV FULL HD |000000001|ALTA |12544155 |2019-03-15|248309282|0002|774235318|003|DNI|29600747 |
999846100|745304617|1501278792 |000021251|DECODIFICADOR SD TV VENTA CATV |000000046|BAJA MIGRACION DE P/S |12544155 |2019-03-15|248309282|0001|774235318|003|DNI|29600747 |
999846100|745304617|1501278792 |000021956|INSTALACION PUNTO TV ANALOGICO ALTA |000000046|BAJA MIGRACION DE P/S |12544155 |2019-03-15|248309282|0001|774235318|003|DNI|29600747 |
and I have a script that converts this data
system=ATIS
username=AUTOMATICO
fecha=$(date +"%Y%m%d_%H%M%S")
header="$system|$file|$fecha|$username|"
#echo $header
## quitar espacios tr -s " "
## concatenar sed -e 's/^/'$header'/g'
## cadena awk -F'|' '{print $6"|"$7"|"$14"|"$15"|"$8"|"$4}'
#### ALTA
operacion="ALTA|"
temp1=$(cat $file | grep -i -e '001.*ALTA' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
#### ELIMINAR
operacion="ELIMINAR|"
temp2=$(cat $file | grep -i -e '046.*BAJA FINAL' -e '008.*BAJA FINAL' -e '012.*BAJA FINAL' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
#### BAJA
operacion="BAJA|"
temp3=$(cat $file | grep -i -e '008.*BAJA APC' -e '046.*BAJA MIGRACION' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
#### ACTIVAR
operacion="ACTIVAR|"
temp4=$(cat $file | grep -i -e '027.*RECONEXION APC' -e '028.*RECONEXION DEUDA' -e '042.*RECONEXION TIPIFICADA' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
#### DESACTIVAR
operacion="DESACTIVAR|"
temp5=$(cat $file | grep -i -e '031.*SUSPENSION APC' -e '032.*SUSPENSION PARCIAL DEUDA' -e '033.*SUSPENSION TOTAL DEUDA' -e '040.*SUSPENCION TIPIFICADA PARCIAL' -e '041.*SUSPENCION TIPIFICADA TOTAL' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
#### MANTENER
temp6=$(cat $file | grep -i -e '046.*ALTA - BAJA MIGRACION' -e '047.*ALTA - BAJA MIGRACION' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
#### CAMBIO TITULARIDAD
operacion="CAMBTITU|"
temp7=$(cat $file | grep -i -e '018.*CAMBIO DE TITULAR' | awk -F'|' '{print $14"|"$15"|"$8"|"$4}' | sed -e 's/^/'$header$operacion'/g' | sed -e 's/ //g' )
##armar los archivos
#echo "$temp1" > ATIS_ALTA_temp.txt
#echo "$temp2" > ATIS_ELIMINAR_temp.txt
#echo "$temp3" > ATIS_BAJA_temp.txt
#echo "$temp4" > ATIS_ACTIVAR_temp.txt
#echo "$temp5" > ATIS_DESACTIVAR.txt
echo "$temp1" > ${system}_ALL.txt
echo "$temp2" >> ${system}_ALL.txt
echo "$temp3" >> ${system}_ALL.txt
echo "$temp4" >> ${system}_ALL.txt
echo "$temp5" >> ${system}_ALL.txt
echo "$temp7" >> ${system}_ALL.txt
in this data output
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|02842909|73479107|000018995
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|29600747|12544155|000018995
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|ALTA|DNI|29600747|12544155|000021148
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|BAJA|DNI|29600747|12544155|000021251
ATIS|testArnold2.txt|20190408_111317|AUTOMATICO|BAJA|DNI|29600747|12544155|000021956
but I want my output to be in the same order as income because sometimes it is so much data and filters mess up, how do I write it in the same order in which it enters?
IIUC, you want to map a bunch of patterns into different operations. If so, you can use awk with the following code sample:
$ cat t18.awk
# here we set up "|" and optional surrounding spaces as FS, so no need
# to run sed to remove extra spaces
# OFS="|", header will be feed from the command line with -v option
BEGIN{ FS = " *\\| *"; OFS = "|"; }
# use if/else to find operation based on pattern in your "grep" commands
# I just added three, you need to add rest of them to the function
function find_operation() {
if (/001.*ALTA/) return "ALTA"
else if (/(046|008).*BAJA FINAL/) return "ELIMINAR"
else if (/008.*BAJA APC|046.*BAJA MIGRACION/) return "BAJA"
else return ""
}
# run the function(), if operation is not EMPTY, print it
operation = find_operation() {
print header, operation, $14, $15, $8, $4
}
I run the above on my MacOS, and it yields the following:
$ header="ATIS|testArnold2.txt|data|atok"
$ awk -v header="$header" -f t18.awk file.txt
ATIS|testArnold2.txt|data|atok|ALTA|DNI|02842909|73479107|000018995
ATIS|testArnold2.txt|data|atok|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|data|atok|ALTA|DNI|02842909|73479107|000021932
ATIS|testArnold2.txt|data|atok|ALTA|DNI|29600747|12544155|000018995
ATIS|testArnold2.txt|data|atok|ALTA|DNI|29600747|12544155|000021148
ATIS|testArnold2.txt|data|atok|BAJA|DNI|29600747|12544155|000021251
ATIS|testArnold2.txt|data|atok|BAJA|DNI|29600747|12544155|000021956
Certainly, you should change the header to what you defined in your code, just remove the trailing pipe |.

why the output is incorrect in my Shell string?

The script is as below:
a=`sed '/^project_folder=/!d;s/.*=//' conf.ini | tr -d '\r'`
b=`sed '/^version.versionLicense=/!d;s/.*=//' conf.properties`
path=${a}/${b}/env/123456/bin
echo $path
the content of conf.ini is project_folder=/data/AUTO-PUBLISH/iPEMS_01
the content of conf.properties is version.versionLicense=3.1.0.3
the output is /env/123456/binISH/iPEMS_01/3.1.0.3
my expected output is /data/AUTO-PUBLISH/iPEMS_01/3.1.0.3/env/123456/bin
Why this happened?
You should not use sed for what you are doing grep would be more than enough as you can see hereunder:
$ more conf.properties conf.ini
::::::::::::::
conf.properties
::::::::::::::
version.versionLicense=3.1.0.3
::::::::::::::
conf.ini
::::::::::::::
$ grep -oP '(?<=^project_folder=).*' conf.ini | tr -d '\r'
/data/AUTO-PUBLISH/iPEMS_01
$ grep -oP '(?<=version\.versionLicense=).*' conf.properties | tr -d '\r'
3.1.0.3
Your script becomes:
a=$(grep -oP '(?<=^project_folder=).*' conf.ini | tr -d '\r')
b=$(grep -oP '(?<=version\.versionLicense=).*' conf.properties | tr -d '\r')
path=${a}/{$b}/env/123456/bin
echo $path
And the output is:
/data/AUTO-PUBLISH/iPEMS_01/{3.1.0.3}/env/123456/bin
conf.properties will have \r, which will part of $path now.

Can I use hexdump in a shell script?

Can I use hexdump in a shell script?
When I use it I keep getting an error .
syntax error near unexpected token 'hexdump'
#!/bin/bash
#bash-hexdump
# Quick script to check delay of the shotpoints
echo " please enter the complete line name as mentioned in the RAID2 "
read $line
cd /argus/raid2/"$line"
echo
echo " Entering the directory "
echo
for file in /argus/raid2/"$line"/*.ffid
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list1
done
for filename in 'cat list1'
do
sed -n 6p | awk '{print $1}' = $wd
cat list.txt | sed -n 1p | cut -c13-14 = $hh
cat list.txt | sed -n 1p | cut -c15-16 = $mm
cat list.txt | sed -n 2p | cut -c1-2 = $ss
done
while [ true ]
do
$FFID=`ls -1rt $1 | grep -i ffid | tail -1`
echo " FFID value is : $FFID"
while [ $FFID = `ls -1rt $1 | grep -i ffid | tail -1` ]
do
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list
done
for filename in 'cat list'
do
cat list.txt | sed -n 1p | cut -c13-14 = $hh1
cat list.txt | sed -n 1p | cut -c15-16 = $mm1
cat list.txt | sed -n 2p | cut -c1-2 = $ss1
done
$time1 = "$hh"":""$mm"":""$ss" ;
$time2 = "$hh1"":""$mm1"":""$ss1" ;
$former_seconds = $(date --date= "$time1" +%s);
$later_seconds = $(date --date= "$time2" +%s);
$time_delay = ( "$later_seconds" - "$former_seconds" )
$wb_time = ( "$wd" * 1.33 )
if
(("$wb_time" + "$time_delay")) < 12.0
then
echo "please slow down"
fi
if [ -e EOL.ffid ]
then
echo "EOL.ffid detected, exiting script"
exit
fi
done
I am not able to figure out why the hexdump code is giving me an error . Please help .
You are missing the do in your for loop:
for file in /argus/raid2/"$line"/*.ffid
do
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list1
done

Translate Chinese to urlencoding in awk

I have a .txt file. And each line contains Chinese. I want to translate the Chinese to urlencoding.
How can I get it?
txt.file
http://wiki.com/ 中文
http://wiki.com/ 中国
target.file
http://wiki.com/%E4%B8%AD%E6%96%87
http://wiki.com/%E4%B8%AD%E5%9B%BD
I found a shell script way to approach it like this:
echo '中文' | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' | tr '[a-z]' '[A-Z]'
So, I wanna embed it in awk like this, but I failed:
awk -F'\t' '{
a=system("echo '"$2"'| tr -d '\n' | xxd -plain | \
sed 's/\(..\)/%\1/g' | tr '[a-z]' '[A-Z]");
print $1a
}' txt.file
I have tried another way to write an outside function and call it in awk, code like this, failed it again.
zh2url()
{
echo $1 | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' | tr '[a-z]' '[A-Z]'
}
export -f zh2url
awk -F'\t' "{a=system(\"zh2url $2\");print $1a}" txt.file
Please implement it with awk command because I actually have another thing need to handle in awk at the same time.
With GNU awk for co-processes, etc.:
$ cat tst.awk
function xlate(old, cmd, new) {
cmd = "xxd -plain"
printf "%s", old |& cmd
close(cmd,"to")
if ( (cmd |& getline rslt) > 0 ) {
new = toupper(gensub(/../,"%&","g",rslt))
}
close(cmd)
return new
}
BEGIN { FS="\t" }
{ print $1 xlate($2) }
$ awk -f tst.awk txt.file
http://wiki.com/%E4%B8%AD%E6%96%87
http://wiki.com/%E4%B8%AD%E5%9B%BD

Resources