Sponsored Content
Homework and Emergencies Homework & Coursework Questions How to read user keyboard input inside the case? Post 302728477 by femchi on Thursday 8th of November 2012 02:13:36 AM
Old 11-08-2012
how should I apply this part to the script?

The “Delete files or directories” selection should prompt for a list of files or directories to be deleted. The script asks user for confirmation before deleting a file or a whole directory. The script must perform reasonable argument validation.

I tried to read the list of files and directories in a string but it is pretty hard to split the string based on space and put them in an array and then check them one by one and delete them. Also I was thinking to run a while loop to ask user to enter the name of file and then delete it and again repeat the job but it is not a nice way.
would you give me some clue?

Code:
#!/bin/sh
clear
	echo "\t `date "+%d/%m/%Y"`"
	echo "\t Hostname Machine: `hostname`"
	echo "\t user: `whoami` "
	echo "\t M A I N - M E N U"
	echo "\t 1.Copey Directory Tree"
	echo "\t 2.Delete Files or Directories"
	echo "\t 3.Output Information"
while true ; do
	echo -n "\t Please enter an option [ 1 - 4 ] : "
	read opt

	case "$opt" in
		1) echo "Enter the source of the files and destination for copying "
                   read src dest
                if [ -n "$src" -a -n "$dest" ]
		then
			if [ -d "$src" -o -f "$src" -a -d "$dest" ]
			then
			cp -r "$src" "$dest"
			else
			echo "Enter the valid file or directory addresses" 
			fi
		else
		echo "please enter valid address as a source and destination to copy the files "
			fi ;;
		2) echo "enter the name of files want to be deleted"
		read files
		for f in "$files"
		do
		echo "$f\n"
		done  
		 ;;

		3) echo "\t the number of possible users on the system: `cat /etc/passwd |wc -l`"
		   echo "\t the number of current user on the system : `who | wc -l`"
		   echo "\t the number of processes running: `ps -e | wc -l`"
			 ;;


		*) exit ;; 
	esac
done

---------- Post updated at 02:13 AM ---------- Previous update was at 12:50 AM ----------

Code:
2) echo "enter the name of files want to be deleted"
		read a
		
	for CHARACTER in $a ; do
	  if [ -f $CHARACTER -o -d $CHARACTER ] ; then
		rm -ir $CHARACTER
	else
	echo "$CHARACTER is not a valid file or directory"
	fi
	done
		 ;;

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

input inside while read loop

Hi all Does anyone have a script that will allow me to stop inside a while read loop. I want to pause the loop until a enter is pressed. e.g. While read line do echo something if LINECOUNT > 40 then read ENTER?"PRESS ENTER TO CONT..." ... (3 Replies)
Discussion started by: jhansrod
3 Replies

2. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies

3. UNIX for Dummies Questions & Answers

Testing for correct user input from keyboard

What script can I use to catch errors in a shell script if user inputs alpha numeric characters instead on integers from the keyboard? (0 Replies)
Discussion started by: Pauline mugisha
0 Replies

4. Shell Programming and Scripting

To ignore user input case.

hi, i will like to know whether awk command can ignore case? i written a script that will take in user input and search for data on the 1st field from a text file. echo -n "Title:" read title awk -F":" '$1~/'"$title"'/{print $0}' Filename read ans return ... (5 Replies)
Discussion started by: Cheranime
5 Replies

5. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

6. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

7. Programming

Keyboard User Input in awk language ?

Hi, does someone know how to make a keyboard data input in the AWK programming language ? Regards Zabo (6 Replies)
Discussion started by: Zabo
6 Replies

8. Shell Programming and Scripting

Unable to read user input inside a loop

Hi, This query is a part of a much more lengthy script. I wish to look for all the files in a folder named "data" which in this case has two files i.e. plan.war and agent.properties. For all the files found under data I wish to ask the user as to where they wish copy the files to. Below,... (14 Replies)
Discussion started by: mohtashims
14 Replies

9. UNIX for Beginners Questions & Answers

Case inside While read File

Hi Experts, Need your guidance for case statement. I tried many way but no success yet.Now my existing code is doing something like below. Each Line of the input file contains one test case.#!/bin/bash FILE=$1 while read LINE; do do COMMAND done < $FILE Now I want to modify the code... (6 Replies)
Discussion started by: pradyumnajpn10
6 Replies

10. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies
SHTOOL-ECHO.TMP(1)					      GNU Portable Shell Tool						SHTOOL-ECHO.TMP(1)

NAME
shtool-echo - GNU shtool echo(1) extensional command SYNOPSIS
shtool echo [-n|--newline] [-e|--expand] string DESCRIPTION
shtool echo is an echo(1) style command which prints string to stdout and optionally provides special expansion constructs (terminal bold mode, environment details, date, etc) and newline control. The trick of this command is that it provides a portable -n option and hides the gory details needed to find out the environment details under option -e. OPTIONS
The following command line options are available. -n, --newline By default, output is written to stdout followed by a "newline" (ASCII character 0x0a). If option -n is used, this newline character is omitted. -e, --expand If option -e is used, string can contain special "%x" constructs which are expanded before the output is written. Currently the following constructs are recognized: %B switch terminal mode to bold display mode. %b switch terminal mode back to normal display mode. %u the current user name. %U the current user id (numerical). %g the current group name. %G the current group id (numerical). %h the current hostname (without any domain extension). %d the current domain name. %D the current day of the month. %M the current month (numerical). %m the current month name. %Y the current year. EXAMPLE
# shell script shtool echo -n -e "Enter your name [%B%u%b]: "; read name shtool echo -e "Your Email address might be %u@%h%d" shtool echo -e "The current date is %D-%m-%Y" HISTORY
The GNU shtool echo command was originally written by Ralf S. Engelschall <rse@engelschall.com> in 1998 for Website META Language (WML) under the name buildinfo. It was later taken over into GNU shtool. SEE ALSO
shtool(1), echo(1). 18-Jul-2008 shtool 2.0.8 SHTOOL-ECHO.TMP(1)
All times are GMT -4. The time now is 09:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy