How to capture the status of each sed command inside function

Hi Experts

I am executing below script where running 3 sed commands over array and getting the status of each commands if any sed fails then it should exit ,need your help to make below work
Getting below error as of now

sed: -e expression #1, char 0: no previous regular expression
Command 'sed -i s///g .abc' failed with exit code 1

CODE

#!/bin/bash

E=java6
F=java7
cd /tmp/dd

Declare an array of commands

commands=("sed -i "s/${E}/${F}/g" "abc"" "sed -i "s/${G}/${H}/g" "pqr"" "sed -i "s/${G}/${H}/g" "xyz"")

Iterate over the commands array

for cmd in "${commands[@]}"
do
# Execute each command and store its exit status
$cmd
status=$?
# Check the exit status
if [ $status -ne 0 ]; then
echo "Command '$cmd' failed with exit code $status"
exit 1
fi
done

echo "All commands completed successfully"
exit 0

@abhaydas,
as your posts have been edited multiple time using the proper/required markdown tags (e.g. here) AND you're asked to use proper formatting, please use markdown code tags to edit your post before proceeding any further.
For starters, I'd suggest pasting your script in shellcheck.net and fix any identified issues.

P.S. I'd also suggest going through your earlier posts with suggested solutions - it might actually help you here as well.

3 Likes

Please do as @vgersh99 suggests. I see some potential issues, but I cannot tell for sure.

Or, mark the thread as solved, if you found your answer.

This topic was automatically closed 300 days after the last reply. New replies are no longer allowed.