Home > OS >  shell script stops proceeding inside the loop
shell script stops proceeding inside the loop

Time:03-21

Using bash version 5, I have a loop that goes through a list, and uses each item in that list in a find command inside the loop, and then append that find result to an empty array.

This code however stops at saving 1st result successfully in pid and script exits. I have tried trap and set -x with no progress.

Anyone understands what's happening here ?

#!/bin/bash

set -e
set -o pipefail
set -vx

## run cleanup on signal 1, 2, 3, 6
trap cleanup 1 2 3 6

cleanup()
{
  echo "Caught Signal $? ... cleaning up."
}

r=$(cat /proc/net/tcp |grep " 01 " |awk '{print $10}')
inodes=()
inodes =($r)
pattern="^.*(proc)\/\K([a-zA-Z0-9_-]*)(?=\/[a-zA-Z0-9]*)?"

con_pids=()

for inode in ${inodes[*]}
do
        echo inode number is: $inode
        pid=$(find /proc/* -type l -ls 2>/dev/null |grep "socket:\[$inode\]" | grep -m1 -oP $pattern)
        echo "code didn't stop"
        # echo $?
        con_pids =($pid)
        #con_pids=(${con_pids[*]} "$pid")
done

echo ${con_pids[*]}

CodePudding user response:

See Charles Duffy's comment under the question.

  • Related