1

I am trying to write a program that will display the running processes and ask the user if they would like to kill each process. here is my code

#! /bin/bash

ps

PID TTY          TIME CMD
1681 pts/1    00:00:00 tcsh
1690 pts/1    00:00:00 bash
1708 pts/1    00:00:00 script
1710 pts/1    00:00:00 ps

ps | while read line; do

id=$(echo $line | cut -d' ' -f 1)
name=$(echo $line | cut -d' ' -f 4)

echo 'process ID: ' $id 'name: ' $name
echo -n 'Would you like to kill this process? Yes/No: '
read word < /dev/tty
if [$word == 'yes' ]; then
kill $id

fi

done

I have two issues..one is that when i type no I get an error line 10 [no: command not found. the second one is that when I am assigning the variables to id and name, it automatically reads the first line which gives me

process ID: name: cmd

2 Answers 2

1

Your if is wrong:

if [ "$word" = "yes" ]
    ^^^^^^^^--note the space + quotes around the var.

As well, [ doesn't use == for equality testing. it's just =.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you shouldn't have to do the < /dev/tty part on that one line; read word is sufficient.

Secondly, you just need a space in your if clause, after the opening bracket - like so:

if [ $word == 'yes' ]; then

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.