Question: Im struggling with creating a bash script similiar to ps.

I just can't get it how bash works and how to make it work. Worst thing, it's first language that I don't even know how to start with and the pressure from college is making only things worse.

My plan is:

1. Create a while loop
2. Trough all the stuff of "ls /proc"
3. Create a variable out of found folder like "folder=$(awk '{print $i}')"
4. Printf the created variable and some stuff about if from /proc/i/status
5. End while loop if something happend to be not an integer.

And I just can't get it. Im facing tons of problems and googling them hard as usually when I start to learn a new language isn't helping to solve anything. I feel helpless with this task. I don't even know if my approuch to the problem is right :(

Answer: 
You [don't need or want to parse `ls` output](http://mywiki.wooledge.org/ParsingLs). Just loop over a wildcard directly.

```
for proc in /proc/*; do 
    pid=${proc#/proc/}
    case $pid in *[!0-9]*) break;; esac
    awk -v pid="pid" '{ printf "%i %s\n", pid, "stuff" }' $proc/status
done

```

You might want `continue` instead of `break` but the shell will alphabetize the hits when expanding the wildcard so the numeric entries will actually be grouped before other entries.

The [parameter expansion](https://www.gnu.org/s/bash/manual/html_node/Shell-Parameter-Expansion.html) `${variable#pattern}` produces the value of `variable` with any leading match on `pattern` trimmed off. There is also `${variable%suffix}` to trim a suffix matching a pattern, and a number of other simple string manipulation facilities.
You're not saying what exactly you want to extract from the process status, so let's say you want it's name and state

Borrowing from @tripleee answer, you could do:

```
#!/bin/sh

for proc in /proc/*
do
    pid=${proc#/proc/}
    case $pid in *[!0-9]*) continue;; esac
    name=$(grep  "Name" $proc/status | awk '{print $2$3}')
    state=$(grep "State" $proc/status | awk '{print $2 " " $3}')
    echo $proc $name $state | awk '{print $1 ", whose name is " $2 " is in state " $3 " " $4}'
done

```

Which would print something like:

```
/proc/98, whose name is kthrotld is in state I (idle)
/proc/9813, whose name is WebContent is in state S (sleeping)
/proc/99, whose name is acpi_thermal_pm is in state I (idle)

```

Or, if you want to get fancy, you could print with some color:

```
#!/bin/sh

for proc in /proc/*
do
    pid=${proc#/proc/}
    case $pid in *[!0-9]*) continue;; esac
    name=$(grep  "Name" $proc/status | awk '{print $2$3}')
    state=$(grep "State" $proc/status | awk '{print $2 " " $3}')
    echo $proc $name $state | awk '{print "\033[0;33m" $1 "\033[0;m, whose name is " "\033[0;32m" $2 "\033[0m is in state \033[0;34m" $3 " " $4 "\033[0m"}'
done

```

Which would output something like

[![enter image description here](https://i.stack.imgur.com/dr7Rn.png)](https://i.stack.imgur.com/dr7Rn.png)