Skip to content

Instantly share code, notes, and snippets.

@aclud
Created February 29, 2024 20:47
Show Gist options
  • Save aclud/d3e9354f34d295a8d5c7d046816ef142 to your computer and use it in GitHub Desktop.
Save aclud/d3e9354f34d295a8d5c7d046816ef142 to your computer and use it in GitHub Desktop.
File Exists shell script
#!/bin/ksh
#sample of shell script to query db for file information and verify that files exist where expected
#########################################################################
#
# ***REMOVED*** "File Exists" Utility
#
# Author: ***REMOVED***
# Version: 0.1
# Usage ./***REMOVED***.sh
# Requirements: ***REMOVED***
#
#########################################################################
#find out if the user is ***REMOVED***
if [ ! `/usr/ucb/whoami`="***REMOVED***" ]; then
echo "You must be logged in as ***REMOVED*** to use this utility, exiting..."
exit 1
fi
#let's check out the ***REMOVED*** version
APACS_VER=`ls -d /***REMOVED***/[2-3].[0-9]|tr -d '[:alpha:]/'`
case $APACS_VER in
2.1) ACKEYTYPE="exam_ckey"
A_INFOSQL="select '^'+left(p.pat_name,2)+'^'+case ex.ris_exam_id when \"\" then \"Does not exist in PACS\" when NULL then \"Does not exist in PACS\" else ex.ris_exam_id end+'^'+convert(varchar(22),ex.study_dttm)+'^'+esl.exam_stat_desc+'^'+ex.lta_stat from patient p, examination ex, exam_status_lkup esl where p.pat_ckey=ex.pat_ckey and ex.exam_stat=esl.exam_stat and ex.exam_ckey=("
;;
3.0) ACKEYTYPE="study_ckey"
A_INFOSQL="select '^'+left(p.pat_name,2)+'^'+case ex.ris_exam_id when \"\" then \"Does not exist in PACS\" when NULL then \"Does not exist in PACS\" else ex.ris_exam_id end+'^'+convert(varchar(22),ex.study_dttm)+'^'+esl.exam_stat_desc+'^'+ex.lta_stat from patient p, examination ex, exam_status_lkup esl where p.pat_ckey=ex.pat_ckey and ex.exam_stat=esl.exam_stat and ex.exam_ckey in (select exam_ckey from exam_study_ckey_view where study_ckey="
;;
*) echo "Your PACS version ($APACS_VER) is not currently supported, exiting..."
exit 1
;;
esac
#let's figure out which OS this is
case `uname` in
SunOS) SAMBA_CMD="/usr/local/samba/bin/smbclient"
;;
Linux) SAMBA_CMD="smbclient"
;;
*) echo "Your OS ($A_OS) is not currently supported, exiting..."
exit 1
;;
esac
#obtain the accession number from the user
clear
if [ -z "$1" ]; then
printf "Enter Accession Number to check IV file existence for: "
read ACKEY
if [ -z $ACKEY ]; then
exit 1
fi
else
ACKEY="$1"
if [ -z $ACKEY ]; then
exit 1
fi
echo "Using $ACKEYTYPE entered via command line: $ACKEY"
fi
#TODO: change this to adapt to acc# input
#TODO: find out if the entry is valid
echo $ACKEY|grep "[^0-9]" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
echo "Your entry ($ACKEY) is not valid, exiting..."
exit 1
elif [ $ACKEY -le 0 ]; then
echo "Your entry ($ACKEY) must be greater than zero, exiting..."
exit 1
elif [ $ACKEY -gt 0 ]; then
AGOODCKEY=1
else
if $AGOODCKEY !=1; then
echo "Your entry ($ACKEY) was not accepted, exiting..."
exit 1
fi
fi
#find out if the ckey entered exists in the ***REMOVED*** table at all and how many times
***REMOVED***=`***REMOVED***`
ACOUNT=`***REMOVED*** -U***REMOVED*** -w300 -P ***REMOVED*** -D***REMOVED*** << ENDOFINPUT | grep -v "-"|tr -cd '[[:digit:]]'
set nocount on
select count(distinct file_name) from ***REMOVED*** where $ACKEYTYPE=$ACKEY
go
exit
ENDOFINPUT`
# if it's 0 or there was an error exit, otherwise say how many files exist and proceed
echo $ACOUNT|grep "[^0-9]" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
printf "\n\tAn invalid reponse was received from the database, exiting...\n"
exit 1
elif [ "$ACOUNT" = "0" ]; then
printf "\n\tNo files in ***REMOVED*** associated with $ACKEYTYPE $ACKEY, exiting...\n"
exit 1
elif [ "$ACOUNT" = "1" ]; then
printf "\n\tFound $ACOUNT file associated with $ACKEYTYPE $ACKEY\n\n"
else
printf "\n\tFound $ACOUNT files associated with $ACKEYTYPE $ACKEY\n\n"
fi
#display some patient info so the user can confirm they are looking at the right exam/study
A_INFO=`***REMOVED*** -U***REMOVED*** -w900 -P ***REMOVED*** -D***REMOVED*** <<EOF|awk '$1 ~ /^[\^]/ {print}'
set nocount on
go
${A_INFOSQL}${ACKEY})
go
exit
EOF`
pn=`echo $A_INFO|cut -d"^" -f2`
acc=`echo $A_INFO|cut -d"^" -f3`
dttm=`echo $A_INFO|cut -d"^" -f4`
examstat=`echo $A_INFO|cut -d"^" -f5`
ltastat=`echo $A_INFO|cut -d"^" -f6`
printf "\tLast Name:\t\t$pn (only first two letters displayed)\n\tAccession number:\t$acc\n\tStudy Date/Time:\t$dttm\n\tExam Status:\t\t$examstat\n\tLTA Status:\t\t$ltastat\n\n"
#echo "$A_INFO"
#grab unique files related to this ckey
A_OUT=`***REMOVED*** -U***REMOVED*** -w900 -P ***REMOVED*** -D***REMOVED*** <<EOF | awk '$1 ~ /^[0-9]/ {print}'
set nocount on
go
select distinct stsd.device_addr + ' ' + left(il.device_id, len(il.device_id)-1) + ' ' + left(right(il.file_name, len(il.file_name)-6),4) + '/' + left(right(il.file_name, len(il.file_name)-10),8) + '/' + il.file_name from ***REMOVED*** il, ***REMOVED*** stsd where stsd.device_id=il.device_id and $ACKEYTYPE=$ACKEY
go
exit
EOF`
#perform the samba ls to check for file existence (0=ok, 1=failure)
A_ROWCOUNT=0
A_ROWGOODCOUNT=0
A_ROWBADCOUNT=0
printf "\n\tChecking for file existence...\n"
echo "${A_OUT}" | while read A_LINE; do
A_ROWCOUNT=`expr $A_ROWCOUNT + 1`
a=`echo ${A_LINE}|awk '{print $1}'`
b=`echo ${A_LINE}|awk '{print $2}'`
c=`echo ${A_LINE}|awk '{print $3}'`
d=`$SAMBA_CMD \\\\\\\\$a\\\\$b -l /dev/null -Unasro%nasro -c "ls $c"|grep listing|wc -l|sed s/\ //g`
if [ "$d" = "0" ]; then
printf "\tFILE EXISTS: \x05C\x05C$a\x05C$b\x05C$c\n"|tr '\057' '\134'
A_ROWGOODCOUNT=`expr $A_ROWGOODCOUNT + 1`
elif [ "$d" = "1" ]; then
printf "\tFILE NOT FOUND: \x05C\x05C$a\x05C$b\x05C$c\n"|tr '\057' '\134'
A_ROWBADCOUNT=`expr $A_ROWBADCOUNT + 1`
A_BADFOUND=1
else
printf "\tFILE NOT CHECKED: \x05C\x05C$a\x05C$b\x05C$c\n"|tr '\057' '\134'
A_NOTCHECKED=1
fi
done
if [ "$A_ROWCOUNT" -gt "1" ]; then
A_ROWP="s"
fi
if [ "$A_ROWGOODCOUNT" -eq "1" ]; then
A_ROWGCP="s"
fi
if [ "$A_ROWBADCOUNT" -eq "1" ]; then
A_ROWBCP="es"
fi
#instruct user to escalate if any problems were encountered
if [ "`expr $A_ROWBADCOUNT + $A_ROWGOODCOUNT`" -ne "$A_ROWCOUNT" ]; then
printf "\n\tNot all file$A_ROWP were successfully checked, please escalate for manual investigation.\n"
fi
if [ "$A_BADFOUND" -eq "1" ]; then
printf "\n\tErrors were encountered, please investigate and escalate as needed.\n"
fi
#provide summary
if [ "$A_NOTCHECKED" -ne "1" ]; then
if [ "A_ROWBADCOUNT" -gt "0" ]; then
printf "\nComplete: $A_ROWCOUNT file$A_ROWP checked, $A_ROWGOODCOUNT exist$A_ROWGCP and $A_ROWBADCOUNT do$A_ROWBCP not.\n"
elif [ "A_ROWBADCOUNT" -eq "0" ]; then
printf "\nComplete: $A_ROWCOUNT file$A_ROWP checked, all files exist.\n"
fi
fi
printf "This utility does not verify the contents of each file, it only confirms that they exist.\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment