How do I do if(string.endsWith("/")) in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I do if(string.endsWith("/")) in shell
# 1  
Old 01-15-2009
MySQL SOLVED: How do I do if(string.endsWith("/")) in shell

I have a string that may or may not end with a '/' and would like to do something if it does not end in a slash. I have tried:

Code:
if "/home/test/blah/blah" | sed 's/\/$//'
then
    echo "OK"
fi

but I think sed works on input streams only and not files. I am not a shell programmer of any kind and am having difficulty in finding infomation on such things.

Last edited by klogger; 01-16-2009 at 08:44 AM.. Reason: Added SOLVED to title
# 2  
Old 01-16-2009
Quote:
Originally Posted by klogger
I have a string that may or may not end with a '/' and would like to do something if it does not end in a slash. I have tried:

Code:
if "/home/test/blah/blah" | sed 's/\/$//'
then
    echo "OK"
fi

but I think sed works on input streams only and not files. I am not a shell programmer of any kind and am having difficulty in finding infomation on such things.
Code:
echo "/home/test/blah/blah"|grep '/$'
if [$? -eq 0];then
  echo "Ends with /"
fi

# 3  
Old 01-16-2009
Quote:
Originally Posted by klogger
I have a string that may or may not end with a '/' and would like to do something if it does not end in a slash. I have tried:

Code:
if "/home/test/blah/blah" | sed 's/\/$//'
then
    echo "OK"
fi

but I think sed works on input streams only and not files. I am not a shell programmer of any kind and am having difficulty in finding infomation on such things.
I think there are multiple ways to do this. Here is one using grep.
You could use grep to find out the last character and act accordingly.

See this
Code:
[/tmp]$ echo "/a/b/c/" | grep -qE "/$"
[/tmp]$ echo $?
0
[/tmp]$ echo "/a/b/c" | grep -qE "/$"
[/tmp]$ echo $?
1

$? holds the exit status. If the match was found, then the exit status is 0.
The -q flag is required so that in case of success, nothing is printed out.
The -E flag is allow regex matching. The $ denoted the end of the string. So "/$" denotes the end of string is a "/"
# 4  
Old 01-16-2009
Quote:
Originally Posted by klogger
I have a string that may or may not end with a '/' and would like to do something if it does not end in a slash.

You don't need an external command for this; the shell's case statement does the job:

Code:
string=qwerty/
case $string in
     */) echo doing something;;
     *) echo not doing anything;;
esac

# 5  
Old 01-16-2009
MySQL

That's great thank you. I am a Java programmer and find shell scripting very difficult to get my head around. I couldn't get the solution provided by skar_a to work and so went with the one given by cfajohnson but thanks to all who replied.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Shell Script to identify the line number containing a particular "string"

Hi, I have a log file, where i am required to identify the line number, where a particular string/line appears in the log file. And then copy 200 lines above that line number to a new file. Can someone provide pointers on how to write this script or what command to be used ? Any... (2 Replies)
Discussion started by: kk2202
2 Replies

8. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies

10. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question