Case statement in UNIX shell script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case statement in UNIX shell script
# 1  
Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*).

Code:
#!/bin/sh
#Get a string from user and check whether its a existing filename or not
rm str2
rm str3
echo "enter a file \c"
read fil
#echo $fil
grep $fil fl >> str2 #fl has all filename stored in it using ls -l >> fl
cut -c 1-1 str2 >> str3
cat str2
cat str3 #it has the value '-'
case "$str3" in
-) echo "you have entered a filename" ;;
*) echo "not a filename"
exit ;;
esac

can someone help me please..

Last edited by vbe; 02-23-2016 at 08:41 AM.. Reason: code tags please
# 2  
Q:
Why are you using files?

what if you have multiple files meeting your request?
(str2 would be a file of all files satisfying... and so str3 a file of 1char lines...)

How do you expect
Code:
case "$str3" in

to work
as you have never used any variables...
# 3  
if you put
Code:
str3=$(head -1 str3)

just before the case it might work...
# 4  
Could you write out the logical flow you are trying to achieve? It's all a bit confused and mixed up with which are variables and which are files. str3 is written to as a file and then you try to use it as a variable.

If you read in a variable fil and you are looking to test if it has a leading hyphen - then you can more simply do something like this:-

Code:
read "enter a file: "?fil
not_first="${fil#?}"              # Trim off first character from input
first="${fil%$notfirst}"          # Trim off all the other characters from input

case "$first" in.......


Does that help? If the statements don't make sense, then let me know and I will explain them.

Robin
# 5  
case statement in unix

Thanks robin, have modified the code and it works only if one file exists (it doesn't work in case redundant file name exists)

Have read the file testing today and written the below code based on my understanding. it works fine. but the problem var_1 and var_2 are not resetting (how do i initialize the values because it takes the already exists value)

Program Flow:
1) Get a word from user
2) check whether a file or directory exists in the given name
3) if not, display no dir or file was exists in the given name
Code:
#!/bin/sh
# Get a word from user and check whether a filename or directory exists in the same name

echo "enter a name \c"
read name

# if its a file, var_1 will have 0
var_1=`[ -f $name ];echo $?`

# if its a directory, var_2 will have 1
var_2=`[ -d $name ];echo $?`

    if [ $var_1 -eq 0  -a $var_2 -eq 1 ]
       then
         echo "Given name has both file and dir"

     elif [ $var_1 -eq 0 -a $var_2 -ne 1 ]
      then
         echo "Given name is a file"
    elif [ $var_2 -eq 1 -a $var_1 -ne 0 ]
      then
         echo "Given name is a directory"
    else
         echo "neither a file nor a dir"
fi

Can you please help me.

Last edited by vbe; 02-24-2016 at 10:53 AM.. Reason: code tags
# 6  
Please use the "code" tag for code as you have agreed by the forum rules.

Have a look at:
Code:
#!/bin/sh
# My system has /bin/sh --> bin/bash, but should be posix compliant

printf "Enter a filename: "
read FN

# List items starting with entered FN, and expand it to a list
# If 'ls' fails, exit with message and error code
str=$(ls ${FN}* 2>/dev/null) || { echo "No entry $FN found!" ; exit 1 ; }

for F in $str
do
	if [ -L "$F" ]
	then	echo "$F is a link"
	elif [ -d "$F" ]
	then	echo "$F is a directory"
	elif [ -f "$F" ]
	then	echo "$F is a file"
	else	# This should never be printed, as its reading the existing content
		echo "No item $F found!"
	fi
done

Hope this helps
# 7  
Given FN* has multiple matches in that directory, shouldn't the for loop run over the contents of the str variable?
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #212
Difficulty: Medium
According to OSPF, an autonomous system boundary router is a router that is connected by using more than one routing protocol and that exchanges routing information with routers autonomous systems.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers. int=Server1,Server2,Server3 qa=Server4,Server5,Server6 prod=Server7,Server8,Server9 echo "Enter the Environment i.e int,qa,prod" read env case $env in int) ## Need command where all the... (9 Replies)
Discussion started by: nareshreddy443
9 Replies

3. Shell Programming and Scripting

Using shell to generate case statement

Hi Gurus, I have a very weird requirement and have no clue to resolve the issue. please help me get out this difficulty below two tables, table1 contains the column name. D means this column used for the rule. for example: rule 0 is all columns have value, rule1 is col3 and col7 have no value.... (2 Replies)
Discussion started by: Torhong
2 Replies

4. Shell Programming and Scripting

Help on case to call recursively in UNIX Shell Script

Hi, I am New to Unix Shell Scripting basically, i need some help in achieving a case statement in Shell script to call recursively That is if case having like 1 2 3 4 options , if user inputs 1 and gets executed case should ask for options again but user should not input the same input value 1,... (7 Replies)
Discussion started by: karthikram
7 Replies

5. Shell Programming and Scripting

Trouble in getting user input while using CASE statement in UNIX

i want to get user input like this please tell which option to chose 1. mango 2. tango 3. rango if user chooses mango then it should execute a set of statements and again ask like this what do you want to do 1.add 2.subtract 3.exit when i choose exit it should goto my previous menu... (4 Replies)
Discussion started by: s.deepak
4 Replies

6. Shell Programming and Scripting

Shell case statement

echo -e "Select: \c" read IN pattern="1-20" case $IN in ) echo "Selected: $IN" ;; *) echo "Invalid selection: $IN" ;; esac # sh test Select: 10 Invalid selection: 10 # sh test Select: 2 (6 Replies)
Discussion started by: Ikon
6 Replies

7. UNIX for Dummies Questions & Answers

case statement in UNIX scripting (ksh)

Hi, I have a script like below : #!/bin/ksh echo "Do you want to export all docs ?" read alld echo "Do you want to export template or report only " read temr case && ] #arguments ;; case && ] #arguments ;; case && ] #arguments ;; (4 Replies)
Discussion started by: luna_soleil
4 Replies

8. Shell Programming and Scripting

shell script case statement

In a case statement like below : case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not gat a $rental for you";;... (4 Replies)
Discussion started by: sriram003
4 Replies

9. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

10. Shell Programming and Scripting

Shell script automation using case statement

Hi, I'm trying to write a shell script that has a menu and then dependant on the selection, will automate some samba file transfer. The problem is when I run the code without the case statement it runs fine. but when I put the case statement in the only way I can get the code to run is to... (6 Replies)
Discussion started by: ianf
6 Replies

Featured Tech Videos