#!/bin/sh
INF_PROC=`ps -l`
for (( i=1; i<5; i++ ))
do
id$i=$(echo $INF_PROC | cut -d ' ' -f $i)
echo $id$i
done
Im learning bash and trying to use a for similar to this but it gives me an error. "Command not found" Why is that?
#!/bin/sh
INF_PROC=`ps -l`
for (( i=1; i<5; i++ ))
do
id$i=$(echo $INF_PROC | cut -d ' ' -f $i)
echo $id$i
done
Im learning bash and trying to use a for similar to this but it gives me an error. "Command not found" Why is that?
The shebang #!/bin/sh
is the deciding factor on which shell executes your script.
Since the script is using bash-specific features, namely (( i=1; i<5; i++ ))
, the shebang should be #!/bin/bash
.
Alternatively you can rewrite the script in such a way that it only uses elements that can be interpreted by all POSIX-compliant shells, such as sh
.
You can do this by writing your loop like so:
#!/bin/sh
INF_PROC=`ps -l`
for i in $(seq 5)
do
id$i=$(echo $INF_PROC | cut -d ' ' -f $i)
echo $id$i
done