Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash variable ($HTTP_COOKIE) which contains a string. I want to check if this string matches the content of a given file (token). I'm working on a busybox linux.

How should I do?

share|improve this question
    
What do you mean by “matches”? That file contains a regular expression? –  manatwork Oct 14 '11 at 8:19
    
I mean that "match exactly byte per byte" the content of the file. –  michelemarcon Oct 14 '11 at 8:23

1 Answer 1

up vote 4 down vote accepted
 if [ "x$HTTP_COOKIE" = "x$(cat myfile)" ]
  then
    ....

keep the x in front in case the string may start with a minus sign (in that case it would be considered as an option to the command test ([ ... ]).

share|improve this answer
5  
There's a bug in that code: $() removes trailing newlines, so you should check "x${HTTP_COOKIE}x" = "x$(cat myfile; echo x)" –  l0b0 Oct 14 '11 at 8:54
3  
Valid if the string in question contains newlines, but most of the times one wants to get rid of newlines. –  ktf Oct 14 '11 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.