Home > Enterprise >  Bash script to cancel a backup task process ID
Bash script to cancel a backup task process ID

Time:09-27

I am getting error(canpr.sh: line 15: syntax error near unexpected token `fi') while executing below script to cancel a particular process ID of a backup job. Can someone please help to check this code and help me to identify the issue or can suggest a better way to perform this task.

#!/bin/bash
while true;
do
PROC=`dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "q proc" | grep "Backup Storage Pool" | awk '{print $1}'`
if ["${PROC}Test" == "Test"]
then
echo "Process list is empty. Exiting from program";
break;
else
  for pid in $PROC
do
  dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "cancel proc $pid"
sleep 30;
fi;
done
echo "Script execution completed"

Result"canpr.sh: line 15: syntax error near unexpected token `fi'

CodePudding user response:

Indenting your code not only makes it easier to read, but also aids in easily identifying bugs such as not closing a loop properly as mentioned by Orion.

You also have a space issue after the opening if statement that needs to be addressed. Please be sure to use shellcheck as mentioned in the bash description as part of your troubleshooting process.

#!/bin/bash
while true; do
    PROC=$(dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "q proc" | grep "Backup Storage Pool" | awk '{print $1}')
    if [ "${PROC}Test" == "Test" ]; then
        echo "Process list is empty. Exiting from program"
        break
    else
        for pid in $PROC; do
            dsmadmc -se=user -id=XXX -password=XXXXX -dataonly=yes "cancel proc $pid"
            sleep 30
        done
    fi
done
echo "Script execution completed"

CodePudding user response:

You need to close the for loop before the if statement. You need to add another done before the fi to close the for loop.

  •  Tags:  
  • bash
  • Related