Shell Script improvement for getting diff result - shell

I have shell Script written which does a job of comparing two files and and gives me result in a HTML format for defects. But i want to improve it so that i can only get modified files defects instead of legacy defects also. I am using this script to get Coverity report.
while read line; do
n=$((++n))
if echo $line | grep '^[[:space:]]*>' &>/dev/null; then
if [ $(($n % 2)) -eq 1 ]; then
# TODO somehow get proper defect number from html
# echo "Defect num: $(($n/2 + 1))"
def_num=$((++def_num))
fi
echo $line | sed -n -e 's/>[[:space:]]*\(.*\)/\1/p'
if [ $(($n % 2)) -eq 0 ]; then
echo "-------------------------------"
fi
done < <(diff -y -W 200 ./cov-results-base/result.filt ./cov-results-changed/result.filt)
echo "==============================="
echo
echo "Number of defects in old code: $(tac cov-results-base/summary.xml |
sed -n '/num/{s|<num>\(.*\)</num>|\1|p; q;}')"
echo "Number of defects in new code: $(tac cov-results-changed/summary.xml |
sed -n '/num/{s|<num>\(.*\)</num>|\1|p; q;}')"

This enables you to get the last modification time of a file and the compare with the current time.
now=`date +%s`
modified=`stat -c "%Y" $file`
if [ $(($now-$modified)) -gt 0 ]; then
echo "not modified";
else
echo "modified";
fi
I hope that this is what you wanted.

Related

Getting errors while imitating tail command in shell script without using inbuilt tail command

I have written this code to imitate tail command without using inbuilt command and it is giving error in for loop though syntax is correct!
I am not able to figure it out where actually the error is?
Also Expected output:
It should print last n number of lines from the file.
n is scanned from user.
#!/bin/sh echo -n "Enter file name: " read file_name echo -n "Enter number of lines: " read n touch $file_name str="end" temp="temp" echo "Start entering data of file and write end for stopping: " while [ 1 ] do read string touch $temp echo $string > $temp grep -w "$str" $temp if [ $? -eq 0 ] then break else cat $temp >> $file_name fi done rm $temp echo "---------------------------------------------------------" total_lines= wc -l < $file_name echo $total_lines a=expr $total_lines - $n exec < $file_name for ((i=$a;i<$total_lines;i++ )) do read a1 echo $a1 done

Catch the search value into the variable in unix shell script

I am trying to search content of file 1 into file 2 and if the content is found then store in found.csv file or store in notfound.csv file
Below is my code,
cd /mnt/data/dobiminer/scripts
usage="Usage:sh scriptname.sh 'ToSearchFile' 'MainSearchFile' 'CR' "
Date=`date +%m%d%y%H%M%S`
File=$(<$2)
echo "File Input $2"
echo $File
if [ $# != 3 ]
then
echo $usage
exit 1
else
echo > "$3-Found-$Date.csv"
echo > "$3-NotFound-$Date.csv"
for MasterClip in `cat $1`
do
echo $MasterClip
String=$(echo "$File" | grep -x $MasterClip)
echo $String
if [ -z $String ];
then
echo "NotFound"
echo $MasterClip >> "$3-NotFound-$Date.csv"
else
echo "Found"
echo $MasterClip >> "$3-Found-$Date.csv"
fi
done
fi
My guess is that the below line of code is not working, as whenever I am running the code, the string value is empty only. It is not catching the search value into it.
String=$(echo "$File" | grep -x $MasterClip)
echo $String
I tried multiple things but not sure where I am going wrong.
THanks for helping me out

Increment variable value in function bash

So I can almost guarantee this is a dumb question but I just can't figure this out. I'm trying to count how many times I've indexed files. I need to increment a counter every time I find a pdf file that matches certain criteria (it's metadata must contain 3 specific values). The variable in question is indexCount and I've marked the line where I'm trying to increment it with #NOT SURE ABOUT THIS LINE
index() {
for file in *
do
[ -d "$file" ] && (cd "$file"; index)
oldPath=$(pwd)
if [ "$( echo "$file" | grep -E '.*\.pdf' )" ]; then
metadata="$(pdftk "$file" dump_data)"
echo "$metadata" | $(grep -e '^InfoKey: Title' >/dev/null 2>&1) && echo "$metadata" | $(grep -e '^InfoKey: Author' >/dev/null 2>&1) && echo "$metadata" | $(grep -e '^InfoKey: CreationDate' >/dev/null 2>&1)
if [ $? -eq 0 ]; then
path="$(pwd)/""$file"
title=$(getAttr "$metadata" '^InfoKey: Title')
author=$(getAttr "$metadata" '^InfoKey: Author')
creation=$(getAttr "$metadata" '^InfoKey: CreationDate')
authorsArray=($(getAuthors "$author"))
for auth in "${authorsArray[#]}";
do
createFolders "$auth" "$creation" "$title" "$path" "$oldPath"
done
$1=$(($1+1)) #NOT SURE ABOUT THIS LINE
fi
fi
done
echo $1
}
indexCount=0
index $indexCount
The correct syntax is:
var=$((var+1))
So instead of
$1=$(($1+1))
you should use a variable name plus the syntax I indicate above. In general, remember bash variables are set without the $ and used with it.
Quoting from Charles Duffy:
If targeting bash as opposed to POSIX sh, there's also the option of (( ++var )) or (( var += 1 ))

Continuously for a file, but alert only once

This post is updated, look below for the solution.
i have the need to check a folder for a presence of a file, which is not always present.
i made a script like this:
#!/bin/sh
while true; do
file=/path/to/file
if [[ "$file" = *filename* ]]
then
echo "$file is present"
else
echo "No present"
fi
sleep 3
done
Works perfectly, except for the fact that the "$file is present" is continuosly repeated, until i delete or move the file.
Which command can I insert after "echo "$file is present" to stop the alert but continue to check for this file (eg when the file will be again available) ?
Thank you.
Since i can't add an answer until 8 hours, i post here my solution:
In anyway i have solved using this script: comparing the time of the file with the current date and then, using "touch" changing the date to 10 seconds ago:
#!/bin/sh
while true
do
cd $(dirname "$0")
current=$(pwd)
cd /boot/home/Downloads
last=$(ls -t | head -n1)
name=$(basename "$last")
filedate=$(date -r "$last" +%G%m%d%H%M%S)
currentdate=$(date +%G%m%d%H%M%S)
if [ "$filedate" -eq "$currentdate" ]
then
echo "$name" "is present"
touch -d '-10 seconds' "$name"
fi
done
Now works as espected and indipendently from the file name!
Alert me about every new file just once and keep to watch that folder :-)
To keep the whole history of script, below there is the script of iamauser. I have a little bit improved this script: now it alert me for every new file (indipendentely from name, kind and so on) inside a folder and also alert me for deleted files :-)
#!/bin/bash
cd /boot/home
filename=$(ls -t | head -n1)
tstamp=$(stat --print "%Y" "$filename")
while true; do
prev=$(ls "/boot/home/Downloads" | tr '\n' '\n' > /tmp/prev.txt)
check=$(ls -t /boot/home/Downloads | head -n1)
if [ ! -d "$filename" ]; then
after=$(ls "/boot/home/Downloads" | tr '\n' '\n' > /tmp/after.txt)
echo "Not present";
sleep 5;
elif [[ "$filename" == "$filename" && $tstamp -ne $(stat --print "%Y" "$filename") ]]; then
sleep 2
difference=$(comm -2 -3 "/tmp/after.txt" "/tmp/prev.txt">/tmp/Diff.txt)
lost=$(cat /boot/common/cache/tmp/Diff.txt)
alert --idea "/boot/home/Downloads: $check is the most recent file in this folder.";
alert --idea "/boot/home/Downloads: $lost removed.";
tstamp=$(stat --print "%Y" "$filename")
else
sleep 3;
fi
done
Something like this may work. I am checking the timstamp of the file to check if there is a new copy of the same filename in the folder.
#!/bin/bash
filename="/path/to/file"
tstamp=$(stat --print "%Y" "$file")
while true; do
if [ ! -f "$filename" ]; then
echo "Not present";
sleep 5;
elif [[ "$filename" == "myfile" && $tstamp -ne $(stat --print "%Y" "$filename") ]]; then
echo "$filename is present";
tstamp=$(stat --print "%Y" "$filename")
else
sleep 3;
fi
done
I tested the script, by touching the filename while the script was running, each time I touched, it printed $filename is present.
Sure, konsolebox; you're right. my script isn't perfect also if i add a sleep to reduce CPU usage.
In anyway, maybe, i miss something with the script from iamauser:
The above script print the echo out when the file is not present, but doesn't print echo out when is present.. Why?
In anyway, since i'd like to experiment, i was also thinking to another way: check the number of files inside a folder using "ls -1 | wc -l", a new file is added and the number of files is +1 than "ls -1 | wc -l"
and, so, i can be alerted indipendently from the file name.. If there is any kind of file with any name, the script should alert me once, while keep to check for new files in the folder.
Any other suggestion?
Thank you!
UPDATE
Ok: this is the definitive revision of the script:
When a new file is written in a folder, this script alert me about. And if I remove this file, the script will alert me, of the most recent file present in that folder.
Very nice, since this is what i was looking for :-)
Since I am on Haiku, i have also replaced the "echo" with alert, an alert window, a sort of graphic "echo".
#!/bin/bash
cd /boot/home
filename=$(ls -t | head -n1)
tstamp=$(stat --print "%Y" "$filename")
while true; do
check=$(ls -t /boot/home/Downloads | head -n1)
if [ ! -d "$filename" ]; then
echo "Not present";
sleep 5;
elif [[ "$filename" == "$filename" && $tstamp -ne $(stat --print "%Y" "$filename") ]]; then
alert --idea "/boot/home/Downloads: $check is the most recent file in this folder";
tstamp=$(stat --print "%Y" "$filename")
else
sleep 3;
fi
done

How can a bash script write out a binary file, without using uudecode?

I've got a little puzzler here for the bash scripting experts... I have a bash script that needs to create a small (80 byte) binary file when it runs. The contents of the file need to be contained inside the script itself (i.e. I don't want to just package the file along with the script).
My script currently does it like this:
echo 'begin-base64 644 dummy.wav' > /tmp/dummy.uu
echo 'UklGRkgAAABXQVZFZm10IBAAAAADAAEAAHcBAADcBQAEACAAZmFjdAQAAAAAAAAAUEVBSxAAAAAB' >> /tmp/dummy.uu
echo 'AAAAQDYlTAAAAAAAAAAAZGF0YQAAAAA=' >> /tmp/dummy.uu
echo '====' >> /tmp/dummy.uu
uudecode -o /tmp/dummy.wav /tmp/dummy.uu
rm /tmp/dummy.uu
... after the above runs, I have my file /tmp/dummy.wav. But I just found out that the computer this script is to run on does not have uudecode installed (and I'm not allowed to install it), so I need to find some other way to create this file. Any ideas?
Some installers do something similar to this:
#!/bin/bash
tail -n +4 $0 | tar xvzf -
exit
<tgz file appended here><newline>
If the target computer has perl available:
perl -ne 'print unpack("u",$_)' > dummy.wav <<EOD
M4DE&1D#```!7059%9FUT(!`````#``$``'<!``#<!0`$`"``9F%C=`0`````
C````4$5!2Q`````!````0#8E3```````````9&%T80``````
EOD
That's using the non-base64 format you get from just doing uuencode dummy.wav < dummy.wav on your original computer.
Failing that, you can always do this:
echo -ne '\x52\x49\x46\x46\x48\x00\x00\x00' > dummy.wav
echo -ne '\x57\x41\x56\x45\x66\x6d\x74\x20' >> dummy.wav
echo -ne '\x10\x00\x00\x00\x03\x00\x01\x00' >> dummy.wav
echo -ne '\x00\x77\x01\x00\x00\xdc\x05\x00' >> dummy.wav
echo -ne '\x04\x00\x20\x00\x66\x61\x63\x74' >> dummy.wav
echo -ne '\x04\x00\x00\x00\x00\x00\x00\x00' >> dummy.wav
echo -ne '\x50\x45\x41\x4b\x10\x00\x00\x00' >> dummy.wav
echo -ne '\x01\x00\x00\x00\x40\x36\x25\x4c' >> dummy.wav
echo -ne '\x00\x00\x00\x00\x00\x00\x00\x00' >> dummy.wav
echo -ne '\x64\x61\x74\x61\x00\x00\x00\x00' >> dummy.wav
This little bit of bash was generated with:
$ hexdump -e '"echo -ne '\''" 8/1 "x%02x" "'\'' >> dummy.wav\n"' dummy.wav | sed 's;x;\\x;g;1s/>/ /'
Edited to add:
As pointed out in a reply here, something like this is also a possibility:
xargs -d'\n' -n1 echo -ne > dummy.wav <<EOD
\x52\x49\x46\x46\x48\x00\x00\x00\x57\x41\x56\x45\x66\x6d\x74\x20
\x10\x00\x00\x00\x03\x00\x01\x00\x00\x77\x01\x00\x00\xdc\x05\x00
\x04\x00\x20\x00\x66\x61\x63\x74\x04\x00\x00\x00\x00\x00\x00\x00
\x50\x45\x41\x4b\x10\x00\x00\x00\x01\x00\x00\x00\x40\x36\x25\x4c
\x00\x00\x00\x00\x00\x00\x00\x00\x64\x61\x74\x61\x00\x00\x00\x00
EOD
(the -d argument is important to turn off xargs's own backslash processing)
You can also turn the 8/1 in my hexdump command into 80/1 and have a single long echo line.
Put an exit at the end of your script, append the file to the end of the script, and use tail -c 80 to get at the contents. This will work as long as you don't need to worry about newline conversion issues.
This is another example to decode radix 64 formatted data, it runs slow, but it is functional.
#!/bin/bash
exec<$0
while read line ; do if [ "$line" = "#payload" ] ; then break; fi; done
r64='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
i=0; while [ $i -lt 256 ] ; do tab[$i]=-1 ; let i=$i+1 ;done
i=0; while [ $i -lt 64 ] ; do tab[`printf "%d" "'${r64:$i:1}"`]=$i ; let i=$i+1; done
bi=0
while read -n 1 x
do
in=${tab[`printf "%d" "'$x"`]}
if [ $in -ge 0 ]; then case $bi in
0 ) out=$(($in<<2)); bi=6 ;;
2 ) out=$(($out|$in)); printf \\$(printf '%03o' $(($out&255)) ); bi=0 ;;
4 ) out=$(($out+($in>>2))); printf \\$(printf '%03o' $(($out&255)) );
bi=0; out=$(($in<<6)); bi=2 ;;
* ) out=$(($out+($in>>4))); printf \\$(printf '%03o' $(($out&255)) );
bi=0; out=$(($in<<4)); bi=4 ;;
esac fi
done
exit
#payload
dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2XmAgICAte3Z2dnZ2dnZ2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jVVVoX2FRUVFRUVFR
UVFQOlhaIyNVI1ojVVojaAp2dklkIyNaI1ojI1ojWlpaV1FRUVFRUVFXUCA9ZFojWiNaIyNaIyNaCnZ2
dlojWiMjVVVaI1ojWlpKUVFRUVFXUF4gIClYIyNaI1VVWiNVWjEKdnZ7WlojWlVaIyNaIyNaVXMtIT8i
fiAgICAgdlgjWiMjWiNaWF5sdgp2bCBZWFhYWFpaVVUjWlpaMS4gICAgICAgICB2WFojWiNaWCIgIDx2
CnZzICAtfiJJMVhYWFpaWm8xICAgICAgICAgIEluWFhaU31gICAgPHYKdnY7ICAgICAtLTwxMjIxbGAg
ICAgICAgICAgPElubjF9ICAgICB2SQp2dmwsICAgICAgICB+Kz5gICAgICAgICAgICAgfnwrfiAgICAu
JUl2CnZ2dnZpLiAgICAgICAgICAgIF9pc2ksICAgICAgICAgICAgX3ZJdnYKdnZ2dnZ2c19fXy4uLi5f
XyV2dnZ2SXZpLCxfLiAuLl9fPnZ2dnZ2dgp2dnZ2SXZ2dm52dnZ2dnZudnZ2dnZ2dnZubnZ2dnZ2dnZ2
dnZ2dnZ2Cg==
#!/bin/bash
# Define usage help
usage () {
echo -e "USAGE:\n\t$0 <file to create> <dir to tar> <name of script or command to run>\n"
exit 0
}
# check commandline arguments
if [ "$1" = "-h" ]; then usage; fi
if [ -z $1 ]; then usage; fi
if [ -z $2 ]; then usage; fi
if [ -z $3 ]; then usage; fi
# test for the directory and if it exists, create the bin file using tar
if [ -d "$2" ]; then
cat >$1<<EOF
#!/bin/sh -e
sed -e '1,/^exit$/d' "\$0" | tar xzf - && "./$2/$3"
exit
EOF
tar czf - $2 >> $1
else
echo "$2 does not exist, aborting!"
exit 1
fi
# make the new file executable and exit
chmod +x $1
exit 0
I would use base64 encoding, as that seems to be the general replacement for uu encoding, and operates on very similar principles.
From my point of view uuencode and uudecode are essential, but
that's only my opinion.
Without creating temporary files you could also do something
like this (uudecode.sh):
#!/bin/bash
# --
# -- Uudecoding without using a regular temporary file
# --
# -- Create a named pipe:
mknod /tmp/.dummypipe p
# -- Starting uudecoding on that pipe in the background:
uudecode -o dummy.txt /tmp/.dummypipe &
# -- Push base64-uuencoded content into the named pipe:
cat <<END_DUMMY > /tmp/.dummypipe
begin-base64 644 dummy.txt
VGhpcyBpcyB0aGUgdXVkZWNvZGVkIHRleHQuCg==
====
END_DUMMY
# -- Remove the named pipe
rm /tmp/.dummypipe
Just encode the binary data in base64 and do something like this:
#! /bin/bash
cat <<EOF | base64 -d > wherever-it-goes.bin
UtEOtUaZcUCrEJtPJrA34BH8Wdpxb1/DtfMo5syiE/h+moNyApEkg2ZwA2/jTDdfl4WijnNbMzvZ
RrF3i7X353AjmTjLBz1NcOOJJhRPYLJ4WQYONyYj/fAhnXQd+s4SHaNponOWKj1AAzdlJY1VLWaX
P8QBJQcn2FTL4pJ3N04=
EOF

Resources