Bash Array Problem

Posted by Deepak Prasanna on Stack Overflow See other posts from Stack Overflow or by Deepak Prasanna
Published on 2009-11-22T03:08:18Z Indexed on 2010/03/17 3:01 UTC
Read the original article Hit count: 426

Filed under:

I wrote a bash script which tries to find a process and run the process if it had stopped. This is the script.

#!/bin/bash
process=thin
path=/home/deepak/abc/
initiate=thin start -d

process_id=`ps -ef | pgrep $process | wc -m`

if [ "$process_id" -gt "0" ]; then
  echo "The process process is running!!"
else     
  cd $path    
  $initiate
  echo "Oops the process has stopped"    
fi

This worked fine and I thought of using arrays so that i can form a loop use this script to check multiple processes. So I modified my script like this

#!/bin/bash
process[1]=thin
path[1]=/home/deepak/abc/
initiate[1]=thin start -d

process_id=`ps -ef | pgrep $process[1] | wc -m`

if [ "$process_id" -gt "0" ]; then
  echo "Hurray the process ${process[1]} is running!!"
else
  cd ${path[1]}
  ${initiate[1]}
  echo "Oops the process has stopped"
  echo "Continue your coffee, the process has been stated again! ;)"
fi

I get this error if i run this script.

DontWorry.sh: 2: process[1]=thin: not found
DontWorry.sh: 3: path[1]=/home/deepak/abc/: not found
DontWorry.sh: 4: initiate[1]=thin start -d: not found

I googled to find any solution for this, most them insisted to use "#!/bin/bash" instead of "#!/bin/sh". I tried both but nothing worked. What am i missing?

© Stack Overflow or respective owner

Related posts about shellscript