lundi 16 mars 2015

writing a loop in Unix that reads user input yes or no

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


Aucun commentaire:

Enregistrer un commentaire