Finding a path like /dir1/dir1


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Finding a path like /dir1/dir1
# 1  
Old 01-10-2013
Finding a path like /dir1/dir1

I have a script that asks user to input a path like this:

Code:
echo "please input path"
read path

This works just fine but I want to make sure that the user inputs the path as /xyz/xyz

In other words, I want to make sure that they use a forward slash for the first part of the path with NO forward slash at the end of the second path. I want to print an error message if they enter it incorrectly. I assume I could use a test condition beginning like this as a start:

Code:
tryme=/dir1/dir1/
#if [[ $tryme == '^\/*\/.*[\/]$' ]]; then echo "error";else echo "no error";fi#

However grep correctly matches /dir/dir/ when I use this expression but not the test expression. What am I doing wrong? If the path is /dir1/dir1 "error" should be displayed.

Last edited by Scott; 01-10-2013 at 02:16 PM.. Reason: Code tags. Last reminder.
# 2  
Old 01-10-2013
Code:
[ $(echo $tryme | egrep -c '\/.*\/.*\/') -eq 0 ] && echo "Correct" || echo "Wrong"

egrep returns 0 if the path is /dir1/dir1 and 1 if it's /dir1/dir1/
This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 01-10-2013
Wondering about the test condition regex

That solution worked! But I am wondering why my original test condition did not. Is the regex that is used in the test condition different than with the egrep? Or is there something wrong with the syntax?
# 4  
Old 01-10-2013
It would work if you use the match (=~) operator instead:

Code:
tryme=/dir1/dir1/
if [[ $tryme =~ ^\/*\/.*[\/]$ ]]; then echo "error";else echo "no error";fi
error

tryme=/dir1/dir1
if [[ $tryme =~ ^\/*\/.*[\/]$ ]]; then echo "error";else echo "no error";fi
no error

This User Gave Thanks to in2nix4life For This Post:
# 5  
Old 01-10-2013
I could not get your shell expression in post #1 to work - but be assured the ^ and $ char don't have the meaning in shells they have in regexes, unless you use the =~ binary operator (see post #4). What I could get to work is substring expansion (in bash):
Code:
 if [ "${tryme: -1}" ==  "/" ]; then echo "error";else echo "no error";fi

means: check if last char is a "/" and, if yes, output "error".

Last edited by RudiC; 01-10-2013 at 04:11 PM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

2. Solaris

Finding the path to an executable , installed package

Hi all, Recently i wanted to see if i have openssl installed in my system (solaris 10), so i do this (not sure if this is the right way to do this) pkginfo -i | grep -i "ssl" system SUNWopenssl-commands OpenSSL Commands (Usr) system SUNWopenssl-include ... (3 Replies)
Discussion started by: javanoob
3 Replies

3. Shell Programming and Scripting

Finding a script/program on $PATH

I am trying to put together a script to find a requested script or program on the user's search path. Am trying to replace the colons separating the parts of a path with a newline to let xargs pass the directories to a list command, but I haven't gotten that far. This is my progress: echo... (7 Replies)
Discussion started by: wbport
7 Replies

4. Programming

Finding the path of the C program

Hi All, I have a c program called findPath.c in a path /home/harsh/c-Programs/. How can i find the path where the program is stored at runtime?? I have given the following #include<stdio.h> int main() { system("dirname $0"); return 0; } This is resulting in the output as . <single dot... (6 Replies)
Discussion started by: sreeharshasn
6 Replies

5. UNIX for Dummies Questions & Answers

finding a file in its path

I have a file that has multiple entries within the Unix system. Korn shell scripts are calling this file (also a ksh) with a . in front of it, and I'm trying to determine which file it is using based on the $PATH by finding where it is located first. Any suggestion on how to go about this?... (2 Replies)
Discussion started by: gavineq
2 Replies

6. Shell Programming and Scripting

Finding path of a running script

Hi, I just want to know any code by which i can get the path of the script which i am running. This is required to store the output in the same directory from where the script is running. pwd fails if I give absolute path of script from some other directory. Thanks in advance Puneet (3 Replies)
Discussion started by: puneet
3 Replies

7. Shell Programming and Scripting

Finding relative path of a file

I have to relatively get the path of a file to use it in the script. The directory structure is /export/opt/XTools/ and under this there are several version directories - 1.0_A0, 1.0_A1, 1.0_A2 etc.,. The actual file is under these directories: installscript.sh My script should pickup the... (4 Replies)
Discussion started by: chiru_h
4 Replies

8. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

9. IP Networking

finding the java path in AIX

How to retrieve the path of Java bin directory in AIX (or any unix OS)?? Actually my problem is, I have a jar file called App.jar. I want to execute it by calling the javaw executable. My command is "/usr/java130/javaw -jar App.jar". I've written this command into a shell script. Now,... (2 Replies)
Discussion started by: fermisoft
2 Replies

10. UNIX for Dummies Questions & Answers

Finding current working dir path

Hi Folks, In a Unix (ksh) script, is there a way to determine the current working directory path of another logged-in user? Of course, I can use "pwd" to find my own path. But, how do I find it for another active user? Thanks for any input you can provide. LY (6 Replies)
Discussion started by: liteyear18
6 Replies
Login or Register to Ask a Question