Bash condition test at the end of string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash condition test at the end of string
# 1  
Old 05-19-2014
Bash condition test at the end of string

I want to check (using bash condition test function) if string contains three spaces, ignoring last three spaces at the end of string.
Code:
string_to_report='foo bar   foo   bar   '
string_to_ignore='foo bar   '


Last edited by useretail; 05-20-2014 at 07:29 AM..
# 2  
Old 05-20-2014
Your question doesn't make any sense.
If statements don't read data from pipes.
You have shown us four echo statements (each of which prints at least three consecutive spaces piped into if statements with comments saying two of them don't contain three spaces???
Then you have shown us a conditional expression that performs two pathname expansions and performs logical and and not operations on those expansions in ways that might or might not be syntax errors depending on what files exist in the current directory.

If you want to write a function, the first thing you need to do is clearly explain what input that function is going to receive and what output it is supposed to produce.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-20-2014
You're right, I provided bad examples. See updated question.
# 4  
Old 05-20-2014
Try
Code:
TMP="${string%   }"
[ "${TMP}" = "${TMP/   /}" ] && echo TRUE || echo FALSE

This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-20-2014
Hmm. Do you mean something like:
Code:
[ '   f f'~='[ ]{3}' ] && echo hi

This purposely does not directly answer your question, but shows what I think you mean.
By ignore - does that mean spaces at the end do not matter or you skip the comparison?

Thanks.
This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 05-20-2014
@jim: my bash man page talks of =~ within [[ ]]
And the ERE must be unquoted...
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 05-20-2014
The updated spec is still ambiguous. The phrase: "ignoring three spaces at the end of the string" can be interpreted several ways. Any of them can be handled using standard variable expansions or conditional expressions in bash or any other shell that handles basic POSIX-mandated variable expansions.

Does it mean that the test fails if there aren't at least three spaces at the end of the string? And, then the test passes only if three spaces appear anywhere in the remainder of the string? If so, try:
Code:
[ "${var%   *   }" != "${var}" ] && echo pass || echo fail

Does it mean that the test passes if three spaces appear anywhere in the string other than the last three characters in the string? Try:
Code:
[ "${var%   ?*}" != "${var}" ] && echo pass || echo fail

Does it mean that the test passes if there are three spaces followed by a non-space character anywhere in the string? If so, try:
Code:
[ "${var%   [! ]*}" != "${var}" ] && echo pass || echo fail

Does it mean that the test passes if there are three spaces followed by any character (including another space) anywhere in the string, but if the string ends with three spaces none of those three spaces can be used in the match? If so, try:
Code:
tmp="${var%   }"
[ "${tmp%   *}" != "${tmp}" ] && echo pass || echo fail

Obviously, any of the above can be changed from using single square brackets:
Code:
[ "${var%pattern}" != "${var}" ] && echo pass || echo fail

to:
Code:
[[ "${var%pattern}" != "${var}" ]] && echo pass || echo fail
                  or
test "${var%pattern}" != "${var}" && echo pass || echo fail
                  or
if [ "${var%pattern}" != "${var}" ]
then    echo pass
else    echo fail
fi
                  etc.

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Test program running taking much more time on high end server T5440 than low end server T5220

Hi all, I have written the following program and run on both T5440 and T5220 on same OS version. I found that T5540 server takes more time than T5220. Please find below the details. test1.cpp #include <iostream> #include <pthread.h> using namespace std; #define NUM_OF_THREADS 20... (17 Replies)
Discussion started by: sanjay_singh85
17 Replies

2. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

3. Shell Programming and Scripting

BASH: remove digits from end of string

Hi there, im sure this is really simple but i have some strings like this e1000g123001 e1000g0 nge11101 nge3and i want to create two variables ($DRIVER and $INSTANCE). the first one containing the alpha characters that make up the first part of the string, e.g. e1000g or nge and the... (9 Replies)
Discussion started by: rethink
9 Replies

4. Shell Programming and Scripting

Test of more than one number on the end of a string

Hi there, I have a bunch of interface names like e1000g0 nge1 dmfe3 I also have some that have longer (vlan tagged) names like e1000g123001 nge23003 e1000g999002 I need to determine whether the interface is one of the former or latter types and I would do that by seeing... (7 Replies)
Discussion started by: rethink
7 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Shell Programming and Scripting

test condition

Hi there, I tried to search for this almost everywhere, but didnt get any proper information on it. What is the difference between ] Some of the code works when I have only single condition i.e. ] && $dothis1 || $dothis2 But if i try to include another testcondition to the... (1 Reply)
Discussion started by: tostay2003
1 Replies

8. Shell Programming and Scripting

Test condition

Hello, what is the better and correct way to perform a comparison: I have been using the following with no problems: if ] then .... fi I have seen this also used : if then .... fi When I try : if then .... fi I get an error like .... the test condition expects a... (4 Replies)
Discussion started by: gio001
4 Replies

9. UNIX for Dummies Questions & Answers

Condition test

Hi there, When I try to do a condition on test: $ str1=abcd $ test $str1 $ echo $? 0 Is there anyway to display the answer to be 'TRUE' or 'YES'? rather than 0? If so, how can I do it without using awk or sed. (2 Replies)
Discussion started by: felixwhoals
2 Replies

10. Shell Programming and Scripting

test and if condition

Guys look at this: i have to write a script that takes a file as an argument. The script should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this. READ WRITE EXECUTE OWNER LEE.BALLANCORE YES YES NO... (9 Replies)
Discussion started by: ciroredz
9 Replies
Login or Register to Ask a Question