Script SH

I can't declare a variable or list of items in a sh script.

I use csh as a user but I understood that it is recommended to use the sh shell for scripts.

however I have to loop a list of elements infinitely and I can't find a way to declare the list.
 
To declare a list and iterate through it; something like:

Code:
somelist="foo bar baz"

for item in ${somelist}; do
  echo "${item}"
done

You want it to loop infinitely?:

Code:
somelist="foo bar baz"

while true; do
  for item in ${somelist}; do
    echo "${item}"
  done
done

Or maybe with a bit more structure:

Code:
process_list()
{
  local somelist="foo bar baz"

  for item in ${somelist}; do
    echo "${item}"
  done
}

while true; do
  process_list
done
 
A POSIX™ shell does not provide the notion of arrays. As a workaround you can use positional parameters though.​
Bash:
#!/bin/sh
set -- 'one' 'two' 'three'
index=2
eval printf '%s\\n' "\${${index}}"
[…] I understood that it is recommended to use the sh shell for scripts. […] I have to loop a list of elements infinitely […]
“Infinitely”⁇ I believe you’ll be happier programming a proper program (i. e. in a programming language supporting arrays out of the box). Shell scripts are like between you could do it manually, but prefer not to, but also a full program is overkill, you know. Iterating over an array indefinitely is obviously impossible to be done manually.​
 
My script needs to keep ping active on some machines and send me a warning if they are unreachable. By running the script with sh /bin/sh script I was able to create a list of items and scroll through it
 
Back
Top