how to check the user input from terminal


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to check the user input from terminal
# 1  
Old 11-26-2008
how to check the user input from terminal

Hello everybody!!!

I am writing my own rm command in unix.
I prompt the user to type if he wants to delete a file and then read what he typed.
But how do i check what he typed?

This is my program so far:

Code:
echo 'Delete prog1.c (y/n)?'
read yesOrNo
if yesOrNo == 'y'
then
rm prog1.c
echo prog1.c deleted
else
echo prog1.c not deleted!
fi

Can anyone HELP me???

Thanks!!!
# 2  
Old 11-26-2008
An alternative would be to create an alias for "rm" and have it execute "rm -i" which is the interactive mode.

alias rm="rm -i"
# 3  
Old 11-27-2008
Thanks for the answer 'candlejack' but i want to make my own rm command not nessecerily to use it but in order to learn how to make my own commands.
This is the first time i try to do it.

So please can you tell me how to check the user input from the keybord if it is 'y' or 'n'?

Thanks!!!
# 4  
Old 11-29-2008
Quote:
Originally Posted by mskart
Hello everybody!!!

I am writing my own rm command in unix.
I prompt the user to type if he wants to delete a file and then read what he typed.
But how do i check what he typed?

This is my program so far:

Code:
echo 'Delete prog1.c (y/n)?'
read yesOrNo
if yesOrNo == 'y'
then
rm prog1.c
echo prog1.c deleted
else
echo prog1.c not deleted!
fi

Can anyone HELP me???

Thanks!!!

Your Script needs some correction whith respect to Unix.

If you are using Unix, then may be you should modify your script as follow:

Code:
echo 'Delete c (y/n)?'
read yesOrNo
if [ $yesOrNo = 'y' ]
then
rm c
echo c deleted
else
echo c not deleted!
fi

Hope this will help you....

Last edited by radoulov; 11-30-2008 at 04:06 AM.. Reason: added code tags, please use them.
# 5  
Old 11-30-2008
Thank you reboot it worked but now i hane an other problem:

When i use my command i type for example:

Code:
./myrm file.c

How can i tell my program that i want the 'c' variable (like Reboot wrote before) to represent the file.c so that i will be able to delete it???

Thanks!!!
# 6  
Old 11-30-2008
if i understand you right, try this

Code:
#!/usr/bin/bash
echo -n 'Enter the file name to delete:'
read c
echo "c is $c"
echo 'Delete c (y/n)?'
read yesOrNo
if [ $yesOrNo = 'y' ]
then
rm $c
echo $c deleted
else
echo $c not deleted!
fi

this will save the file name you entered into a variable c and then remove that file..
# 7  
Old 11-30-2008
Thank you very much "npatwardhan" it worked!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Would like to check user input for letters within a loop

Hi All, #!/bin/bash #Just trying to check if letters are in the user input. Any tips? # I have tried regexp and using 0-9 etc, i cannot get this to work either in just an if statement or while in a loop. echo "Please pick a number" read num if ; then echo "Please enter a number"... (7 Replies)
Discussion started by: jvezinat
7 Replies

2. What is on Your Mind?

Should we use CODE Tags for terminal input and output?

I've always used code tags for code but not for showing terminal input and output. I noticed a mod edited one of my threads and now I'm confused as to proper protocol. Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

5. Shell Programming and Scripting

How-To Check & Filter user input

Hi, On my Java webpage which invokes the shell script has two checkboxes viz ... apache and weblogic apache require one parameter i.e apache home from the user while Weblogic requires three or five params from the user vi.z weblogic_home or <jdk_home, config_home & pid>, username and... (4 Replies)
Discussion started by: mohtashims
4 Replies

6. Shell Programming and Scripting

Check user input

Hi, I need my script to check if the user enters 3 values if not 5 values to my script and alert if the input has any other number of values. for example: ./myscript.sh 22 56 3221 - > correct ./myscript.sh 22 56 3221 45 777 -> correct ./myscript.sh 22 56 3221 45 -> incorrect Please... (6 Replies)
Discussion started by: mohtashims
6 Replies

7. Homework & Coursework Questions

Function to Check if string input from user is alphabetic only

Good Evening. I'm new to C. Can you please help me. I'm creating an error checking function, user will input a string, this will check if the input is all alphabet or all letters only. If there is a digit or other special char, it will print Error then ask input from user again. Here's my... (1 Reply)
Discussion started by: eracav
1 Replies

8. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

9. Shell Programming and Scripting

Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ? (7 Replies)
Discussion started by: CheeSen
7 Replies

10. Cybersecurity

Is my terminal input secure?

My knowledge of Unix input/output/devices is very hazy so could someone please tell me if the following is secure? I log on to an account on a shared Unix server (Linux 2.6.18-6-686) using ssh (PuTTY). I start a python program and then type into it (python raw_input command) the... (2 Replies)
Discussion started by: pnp
2 Replies
Login or Register to Ask a Question