Add exception handling when file missing in shell script - shell

Inside shell script it is picking some files from UNIX directory. Sometimes when any of these file missed then throw error & shell script got failed. I need to add Exceptional Handling in shell script. Here is the below shell script where it is picking files from directory.
#!/bin/ksh
..........
while read file
do
upd_date=$((`date +%s`-`stat -c %Y ${file}`))
file_nm=`basename "${file}"`
if [[ `lsof | grep "${file_nm}"` != '' || $upd_date -lt 60 ]];
then
log_info "Waiting for the file ${file} to complete transfer/skip"
else
log_info "Data file found ${file}"
fi
done<${TEMP}/filepath
...............
exit 0
In the line upd_date=.. throwing error whenever file missed & shell script got failed. So I need to add exception handling if file missed then it will show in log & script will execute successfully.

Use continue in between while loop. continue will skip the current iteration in for, while and until loop.
#!/bin/ksh
..........
while read file
do
[ -f "$file" ] || continue;
upd_date=$((`date +%s`-`stat -c %Y ${file}`))
file_nm=`basename "${file}"`
if [[ `lsof | grep "${file_nm}"` != '' || $upd_date -lt 60 ]];
then
log_info "Waiting for the file ${file} to complete transfer/skip"
else
log_info "Data file found ${file}"
fi
done<${TEMP}/filepath
...............
exit 0

Related

suppress extra o/p generated by gcloud command

I am using gcloud commands to deploy simple VMs. I am using startup script to configure all the required packages on the machine. Our packages are fetched from nexus. Sometimes our changes or nexus network issues result in failure of startup script. I configured a block of code to validate startup script return code. I run gcloud create command and provide startup script, the code block to validate startup script output looks like this..
echo "Step 2: sleep till startup script finishes"
## block to check return code of the VM startup script
MAX_WAIT_TIME=1200
WAIT_TIME=0
RC_CODE=""
echo "[INFO] waiting for startup-script return code"
while [ -z $RC_CODE ] && [ $WAIT_TIME -le $MAX_WAIT_TIME ]
do
RC_CODE=$(gcloud compute instances get-serial-port-output gce-$GCP_ZONE-d-$APP-dev \
--project=$GCP_PROJECT_ID \
--zone=$GCP_ZONE | awk '/^[0-9]{4}/ && /google_metadata_script_runner/ && /startup-script-url exit status/ {sub("\r", "", $NF); print $NF}')
if [[ -z $RC_CODE ]] ; then
echo -n "."
WAIT_TIME=$((WAIT_TIME+10))
sleep 10
else
if [[ $RC_CODE -eq 0 ]] ; then
echo -e "\n[INFO] startup script completed with return code $RC_CODE."
break
else
echo -e "\n[INFO] Startup script completed with return code $RC_CODE."
exit $RC_CODE
fi
fi
done
# to check timeout scenario
if [[ -z $RC_CODE ]]
then
echo "[INFO] Startup script timed out after $((MAX_WAIT_TIME/60))."
echo "[INFO] Startup script completed with return code 1."
exit 1
fi
My output looks like this,
+ ./sh/create-dev-vm-app.sh
Deleted [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].
Created [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].
waiting for startup-script return code
Specify --start=2473 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=32157 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=37602 in the next get-serial-port-output invocation to get only the new output starting from here.```
.
Startup script completed with return code 0.
How can I suppress the these 'Specify' lines from appearing on o/p screen? AND/OR print all startup-script messages received from get-serial-port-output output after I receive the return code.
Swallow standard error to avoid the extraneous output:
gcloud ... 2>/dev/null | awk ...

Bash : How to tail latest file in continuous loop

I am trying to write script that monitors log file and on specific error it performs certain function.
I got the main code working however stuck with one problem.
Application create new log file when system date changes (log file name pattern is LOG.NODE1.DDMMYYYY), how do I get my code to automatically switch to new file that is created. Following is my script so far,
#!/bin/sh
logfile=$(ls -t $DIR"/env/log/LOG"* | head -n 1)
echo $logfile
tail -f $logfile | while read LOGLINE
do
if [[ "${LOGLINE}" == *";A database exception has occurred: FATAL DBERR: SQL_ERROR: ORA-00001: unique constraint (IX_TEST1) violated"* ]];
then
#Do something
fi
done
#!/bin/bash
# ^^^^ -- **NOT** /bin/sh
substring=";A database exception has occurred: FATAL DBERR: SQL_ERROR: ORA-00001: unique constraint (IX_TEST1) violated"
newest=
timeout=10 # number of seconds of no input after which to look for a newer file
# sets a global shell variable called "newest" when run
# to keep overhead down, this avoids invoking any external commands
find_newest() {
set -- "${DIR?The variable DIR is required}"/env/log/LOG*
[[ -e $1 || -L $1 ]] || return 1
newest=$1; shift
while (( $# )); do
[[ $1 -nt $newest ]] && newest=$1
shift
done
}
while :; do
find_newest # check for newer files
# if the newest file isn't the one we're already following...
if [[ $tailing_from_file != "$newest" ]]; then
exec < <(tail -f -- "$newest") # start a new copy of tail following the newer one
tailing_from_file=$newest # and record that file's name
fi
if read -t "$timeout" -r line && [[ $line = *"$substring"* ]]; then
echo "Do something here"
fi
done

Bash sub script redirects input to /dev/null mistakenly

I'm working on a script to automate the creation of a .gitconfig file.
This is my main script that calls a function which in turn execute another file.
dotfile.sh
COMMAND_NAME=$1
shift
ARG_NAME=$#
set +a
fail() {
echo "";
printf "\r[${RED}FAIL${RESET}] $1\n";
echo "";
exit 1;
}
set -a
sub_setup() {
info "This may overwrite existing files in your computer. Are you sure? (y/n)";
read -p "" -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
for ARG in $ARG_NAME; do
local SCRIPT="~/dotfiles/setup/${ARG}.sh";
[ -f "$SCRIPT" ] && echo "Applying '$ARG'" && . "$SCRIPT" || fail "Unable to find script '$ARG'";
done;
fi;
}
case $COMMAND_NAME in
"" | "-h" | "--help")
sub_help;
;;
*)
CMD=${COMMAND_NAME/*-/}
sub_${CMD} $ARG_NAME 2> /dev/null;
if [ $? = 127 ]; then
fail "'$CMD' is not a known command or has errors.";
fi;
;;
esac;
git.sh
git_config() {
if [ ! -f "~/dotfiles/git/gitconfig_template" ]; then
fail "No gitconfig_template file found in ~/dotfiles/git/";
elif [ -f "~/dotfiles/.gitconfig" ]; then
fail ".gitconfig already exists. Delete the file and retry.";
else
echo "Setting up .gitconfig";
GIT_CREDENTIAL="cache"
[ "$(uname -s)" == "Darwin" ] && GIT_CREDENTIAL="osxkeychain";
user " - What is your GitHub author name?";
read -e GIT_AUTHORNAME;
user " - What is your GitHub author email?";
read -e GIT_AUTHOREMAIL;
user " - What is your GitHub username?";
read -e GIT_USERNAME;
if sed -e "s/AUTHORNAME/$GIT_AUTHORNAME/g" \
-e "s/AUTHOREMAIL/$GIT_AUTHOREMAIL/g" \
-e "s/USERNAME/$GIT_USERNAME/g" \
-e "s/GIT_CREDENTIAL_HELPER/$GIT_CREDENTIAL/g" \
"~/dotfiles/git/gitconfig_template" > "~/dotfiles/.gitconfig"; then
success ".gitconfig has been setup";
else
fail ".gitconfig has not been setup";
fi;
fi;
}
git_config
In the console
$ ./dotfile.sh --setup git
[ ?? ] This may overwrite existing files in your computer. Are you sure? (y/n)
y
Applying 'git'
Setting up .gitconfig
[ .. ] - What is your GitHub author name?
Then I cannot see what I'm typing...
At the bottom of dotfile.sh, I redirect any error that occurs during my function call to /dev/null. But I should normally see what I'm typing. If I remove 2> /dev/null from this line sub_${CMD} $ARG_NAME 2> /dev/null;, it works!! But I don't understand why.
I need this line to prevent my script to echo an error in case my command doesn't exists. I only want my own message.
e.g.
$ ./dotfile --blahblah
./dotfiles: line 153: sub_blahblah: command not found
[FAIL] 'blahblah' is not a known command or has errors
I really don't understand why the input in my sub script is redirected to /dev/null as I mentioned only stderr to be redirected to /dev/null.
Thanks
Do you need the -e option in your read statements?
I did a quick test in an interactive shell. The following command does not echo characters :
read -e TEST 2>/dev/null
The following does echo the characters
read TEST 2>/dev/null

A bash script to check the tar.gz files

I have the following script that I would like to run after a cPanel backup is created (1 .tar.gz file per websites) to check the compressed files. Its not meant to replace manual test restores, but just an extra check. The problem is instead of resturning a list of files that failed the check it returns a list of all the files.
#!/bin/bash
date=`date +%Y-%m-%d`
path="/backups/$date/*.gz"
found_errors=0
errors='The following backup files failed the automatic test: \n'
for f in $path
do
gunzip -c $f | tar t > /dev/null
#if the exit status was not 0
if [ $?=0 ]; then
found_errors=1
errors="$errors\n$f"
#echo $f ": Exit status code is " $?
fi
done
#if an error was found
if [ $found_erros!=0 ]; then
#email the list of files that could not be extracted/tested
echo -e $errors | mail -s "Backup Error Check" "admin#example.com"
fi
Thanks in advance.
It seems there's a typo. I bet the check on the status should be
#if the exit status was not 0
if [ $? -ne 0 ]; then

Check the output of a command in shell script

I'm writing a very simple shell scripts that would looked at the log of all failed tests, and print out all the name of all files in the current directory that are in the log
1 #! /bin/sh
2 for file in *
3 do
4 echo "checking: $file"
5 if [$(grep $file failed.txt -c) -ne 0]
6 then
7 echo "$file FAILED"
8 fi
9 done
When I execute it, I get this error:
line 6: [0: command not found
Does anyone have any idea why?
Thanks!!
[ is actually a command in linux (like bash or cat or grep).
$(grep $file failed.txt -c) is a command substitution which in your case evaluated to 0. Thus the line now reads [0 -ne 0], which is interpreted as run a program called [0 with arguments -ne 0].
What you should write instead is [ $(grep $file failed.txt -c) -ne 0 ]. Shell scripts require that there be spaces between the opening and closing square braces. Otherwise you change the command that is executed (the closing ] indicates that there are no more arguments to be read.
So now the command evaluates to [ 0 -ne 0 ]. You can try executing this in your shell to see what happens. [ exits with a value of 0 if the expression is true and 1 if it is false. You can see the exit value by echoing $? (the exit value of the last command to be run).
Instead of testing the count, you can test the return code of grep:
if grep -q $file failed.txt &>/dev/null
The script can be
#!/bin/sh
for file in *; do
echo "checking: $file"
grep failed.txt $file && echo "$file FAILED"
done
or, as an one-liner in user shell command history:
for file in *; do { echo "checking: $file" && grep failed.txt $file && echo "$file FAILED"; done
in man grep
EXIT STATUS
The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)

Resources