Case statement is not showing any output - unix

I am trying to execute below script,
data=$(printf "%s " $(find output.log -type f -exec grep 'ACTIVE\| NOT ACTIVE' {} \; | awk '{print $1}'))
status=`find output.log -type f -exec grep 'ACTIVE\| NOT ACTIVE' {} \; | awk '{print $3}'`
case "$data" in
("Instance1")
echo "Status for Instance1 is : $status";
;;
("Instance2")
echo "Status for Instance2 is : $status";
;;
"") echo "empty things"
;;
esac
but it is not showing any output.. maybe i am missing something in my script (may be lot)
The logfile I am using in above script is ie. output.log,
INSTANCE_NAME OPEN_STATUS STATUS
---------------- ------------ -----------------
Instance1 OPEN ACTIVE
Instance2 OPEN NOT ACTIVE
Can anyone tell me what is wrong with above script ?
Thanks,

You don't loop over your entries, thus you never have a data "Instance1" but "Instance1 Instance2".
You probably want something as the following:
items=$(grep -0 'ACTIVE\| NOT ACTIVE' output.log | tr -s ' ' )
IFS=$'\r\n'
for it in $items; do
data=$(echo $it | cut -d ' ' -f1 )
status=$(echo $it | cut -d ' ' -f3- )
case "$data" in
("Instance1")
echo "Status for Instance1 is : $status";
;;
("Instance2")
echo "Status for Instance2 is : $status";
;;
"") echo "empty things"
;;
esac;
done

Related

SED command use for writing back to the same file

I have the below code which adds Logger.info line after every function definition which I need to run on a python script which is the requirement.
The only question is this has to be written back to the same file so the new file has all these looger.info statements below each function definition.
e.g. the file abc.py has currently below code :
def run_func(sql_query):
return run_func(sql_query)
and the code below should create the same abc.py file but with all the logger.info added to this new file
def run_func(sql_query):
LOGGER.info (''MIPY_INVOKING run_func function for abc file in directory'
return run_func(sql_query)
I am not able to write the sed in this file to the new file (with same file name) so that the original file gets replaced by same file name and so that I have all the logger.info statements in there.
for i in $(find * -name '*.py');
do echo "#############################################" | tee -a auto_logger.log
echo "File Name : $i" | tee -a auto_logger.log
echo "Listing the python files in the current script $i" | tee -a auto_logger.log
for j in $(grep "def " $i | awk '{print $2}' | awk -F"(" '{print $1}');
do
echo "Function name : $j" | tee -a auto_logger.log
echo "Writing the INVOKING statements for $j function definition" | tee -a auto_logger.log
grep "def " $i |sed '/):/w a LOGGER.info (''INVOKING1 '"$j"' function for '"$i"' file in sam_utilities'')'
if [ $? -ne 0 ] ; then
echo " Auto Logger for $i filename - Not Executed Successfully" | tee -a auto_logger.log
else
echo "Auto Logger for $i filename - Executed Successfully" | tee -a auto_logger.log
fi
done
done

recovering deleted script running in background

I have script which is running in bg(nohup) but it was accidently deleted,but that is continue running now I need to edit the code which is already deleted.
How can I now get that code.I assume somewhere it should be as it is running.
Try this :
#!/bin/bash
if [[ ! $1 || $1 == -h || $1 == --help ]]; then
echo -e "Usage:\n\n\t$0 '[path/]<file name>'"
exit 1
fi
files=(
$(file 2>/dev/null /proc/*/fd/* |
grep "(deleted)'$" |
sed -r 's#(:.*broken\s+symbolic\s+link\s+to\s+.|\(deleted\).$)# #g' |
grep "$1" |
cut -d' ' -f1
)
)
if [[ ${files[#]} ]]; then
for f in ${files[#]}; do
echo "fd $f match... Try to copy this fd to another place quickly!"
done
else
echo >&2 "No matching fd found..."
exit 2
fi
Not tested on non GNU-Linux

How to calculate total size of all css,js and html pages separately uing shell script?

I have the following code in shell script
find -name "*.css" -exec -printf '%16f Size: %6s\n'
This gives me the file size of every css file. How do I modify this to get the added sum of all the file sizes ?
You could use awk:
find . -name "*.css" -type f -printf '%s\n' | awk '{ tot+=$0 } END { print tot }'
Or in pure bash:
total=0
while read -r s;
do
total=$(( total+s ))
done < <(find . -name "*.css" -type f -printf '%s\n')
echo $total
In 2 steps:
1) ll *css | tr -s " " > filename.txt
2) awk 'BEGIN {x=0} {x+=$5} END {print x}' filename.txt

Unix Loop if Condition and exit comand

I am facing an issue, I have to delete files from some folders given in Path.lst,
The entire script is working fine but when some wrong path is given in Path.lst the script does exits out of the loop and perform no operation on the next paths.
But the last line
echo -e "\n ENDING SCRIPT SUCCESSFULLY ON `date` " >> $LOG_FILE
gets executed because exit 1 is not working in this part
if [ ! -d $path ]
then
echo -e "\nERROR :$path IS INVALID." >> $LOG_FILE
echo -e "\nENDING SCRIPT WITH ERRORS ON `date`" >> $LOG_FILE
exit 1
---------------------------------------------------------------------------------------
THE SCRIPT IS LIKE :
echo -e "\nSTARTING SCRIPT ON `date`">> $LOG_FILE
if [ $1 -gt 0 ]
then
DAYS_BFOR="$1"
else
echo -e "\nERROR :Please pass a single positive integer to the script" >>$LOG_FILE
echo -e "\nENDING SCRIPT WITH ERRORS ON `date` " >> $LOG_FILE
exit
fi
cat Path.lis | sed 's|^PATH[0-9]*=||g' | while read path
do
if [ ! -d $path ]
then
echo -e "\nERROR :$path IS INVALID." >> $LOG_FILE
echo -e "\n ENDING SCRIPT WITH ERRORS ON `date` " >> $LOG_FILE
exit 1
else
echo -e "\nFILES DELETED FROM THE "$path" DIRECTORY --" >> $LOG_FILE
find $path -type f -mtime +$DAYS_BFOR -printf "%TY-%Tm-%Td %kKB %p\n" | column -t | sed "s|"$path"||g" >> $LOG_FILE 2>&1
file_count=`find $path -type f -mtime +$DAYS_BFOR | wc -l`
if [ $file_count -ge 1 ]
then
find $path -type f -mtime +$DAYS_BFOR | xargs rm 2>>$LOG_FILE 2>&1
fi
fi
done
echo Exit Status : $?
echo -e "\n ENDING SCRIPT SUCCESSFULLY ON `date`" >> $LOG_FILE
Please help and explain the reason as well.
If you only want the "ENDING SCRIPT SUCCESSFULLY" message to appear if files were successfully deleted, not if an invalid path was given you could just move the last two echo lines up to the end of the else statement like this:
else
echo -e "\nFILES DELETED FROM THE "$path" DIRECTORY --" >> $LOG_FILE
find $path -type f -mtime +$DAYS_BFOR -printf "%TY-%Tm-%Td %kKB %p\n" | column -t | sed "s|"$path"||g" >> $LOG_FILE 2>&1
file_count=`find $path -type f -mtime +$DAYS_BFOR | wc -l`
if [ $file_count -ge 1 ]
then
find $path -type f -mtime +$DAYS_BFOR | xargs rm 2>>$LOG_FILE 2>&1
fi
echo Exit Status : $?
echo -e "\n--------------------------- ENDING SCRIPT SUCCESSFULLY ON `date` ----------------------------------" >> $LOG_FILE
fi
done
If you want to just skip to the next item in the Path.lis file then just remove the exit statement from the first loop. That way it will continue to execute the script until all the lines in the file have been read, and just show an error if the current file is not a valid path.

Showing all directories and files, also readable writable and executable

#!/bin/sh
LOCATION=$1
if [ "$#" -ne "1" ]
then
echo "Usage: test1 <directory_name>"
else
echo "Number of directories:` find -type d | wc -l `"
echo "Number of files: ` find -type f | wc -l`"
echo "Number of readable:` find -perm -g+r | wc -l`"
echo "Number of writable:` find -perm -g+w | wc -l`"
echo "Number of executable:` find -perm -g+x | wc -l`"
fi
is there anything wrong with this code? I want it to show all the directories, files readable, writable, and executable when i type in ./test1
You need to add LOCATION in find command. If you didn't give any value, then it will start to find files from current directory and recursively.
Your script should be like,
#!/bin/bash
LOCATION=$1
if [ "$#" -ne "1" ]
then
echo "Usage: test1 <directory_name>"
else
echo "Number of directories: $(find $LOCATION -type d | wc -l)"
echo "Number of files: $(find $LOCATION -type f | wc -l)"
echo "Number of readable:$(find $LOCATION -perm -g+r | wc -l)"
echo "Number of writable: $(find $LOCATION -perm -g+w | wc -l)"
echo "Number of executable: $(find $LOCATION -perm -g+x | wc -l)"
fi

Resources