Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I saw this in a book. I realize that within the clause it is saying:
"a$file1" is not equal to "a"
But what is the "a" doing? And what does it mean?
Is it another name of a file? Is it an operator? I thought for operators to be called operators, a " - " is needed? I'm probably wrong.
#meganewbie
If you where to write that out in a Bash script then run it - you'd see what it does.
Code:
userx%slackwhere ⚡ ~ ⚡> file1="20"
userx%slackwhere ⚡ ~ ⚡> a="0"
userx%slackwhere ⚡ ~ ⚡> while [ "a$file1" != "a" ] ; do ((++a)) ; echo "this is a: $a" ; done
this is a: 25325
this is a: 25326
this is a: 25327
this is a: 25328
this is a: 25329
this is a: 25330
this is a: 25331
this is a: 25332
this is a: 25333
this is a: 25334
...
this is a: 27351
this is a: 27352
^C
Absolutely nothing but screw up your code. Along with 'a' is missing '$'
I'm probably wrong, because this would be an absolutely idiotic way of doing it, but to me it looks like it's looping until the variable "$file1" is empty. "a$file1" will prepend the letter "a" to the contents of the variable "$file1". If "$file1" is empty, the result will simply be "a", if "$file1" is not empty, say it contains the string "blah", then the result would be "ablah". It's then comparing this to "a" and looping until they're equal, meaning "$file1" is empty.
If this truly is the intent, it is MUCH MUCH MUCH better to use the bash test designed for this kind of thing, "-z" or "-n":
Code:
while [ ! -z "$file1" ]
or
while [ -n "$file" ]
Last edited by suicidaleggroll; 05-10-2017 at 09:52 AM.
If you where to write that out in a Bash script then run it - you'd see what it does.
Code:
userx%slackwhere ⚡ ~ ⚡> file1="20"
userx%slackwhere ⚡ ~ ⚡> a="0"
userx%slackwhere ⚡ ~ ⚡> while [ "a$file1" != "a" ] ; do ((++a)) ; echo "this is a: $a" ; done
this is a: 25325
this is a: 25326
this is a: 25327
this is a: 25328
this is a: 25329
this is a: 25330
this is a: 25331
this is a: 25332
this is a: 25333
this is a: 25334
...
this is a: 27351
this is a: 27352
^C
Absolutely nothing but screw up your code. Along with 'a' is missing '$'
Except no. You changed the test, made up some random contents for the loop that were never posted, and then used that to claim that the test will do "absolutely nothing but screw up your code". That was not the test that he posted, and he never posted any contents for the loop...so you can say nothing of the sort.
This was a test technique from WAY back when test "[" did not properly handle empty strings.
I may be the last living geek that remembers using that for cause. That is an OLD technique, and should have been dropped about two decades ago.
If you are using bash (or ksh/Korn) that is anywhere NEAR current you should use the correct syntax demonstrate by previous posters. Bash has both [ and [[ tests than handle strings properly and extend the test syntax wonderfully.
As you explore bash scripting, perhaps along with reading you should be trying the code out. Are you already doing some of this?
There are a number of things you can do to debug and understand what code is doing for you, I have some bash script debug suggestions in a blog here.
And for questions like these where the code seems incorrect, perhaps you can cite the source you are finding it from.
An additional suggestion is that while there may be a number of responses here in Linux-Newbie, we also have a Programming forum at LQ where the main focus is programming questions. Therefore worth a look over there for some bash script ideas.
Best Regards, and I agree with the comments already made by BW-userx, suicidaleggroll, wpeckham, and r3sistance, there's something wrong with this code. That's not the end of the world, coders do manage to write incorrect code, self included. My biggest defense or feedback about that is that the code should be clear and maintainable. I always say that while it's great someone can write "cool" code that does 1000 things using one character (gross exaggeration), I feel it is not effective to do that in reality because it may be unreadable, hard to maintain, as well as not modular/portable.
Except no. You changed the test, made up some random contents for the loop that were never posted, and then used that to claim that the test will do "absolutely nothing but screw up your code". That was not the test that he posted, and he never posted any contents for the loop...so you can say nothing of the sort.
But I did say something of a sort, as well you did the very same thing you just told me that I cannot do to to get your explanation of what it MIGHT mean.
You do realize that don't you?
"say it contains the string "blah", then the result would be "ablah"."
You yourself "made up some random contents for the loop that were never posted", to try and make sense to it.
The very same thing I did. Then tell me I cannot do that but you can. isn't that funny how your mind works?
I used integer logic - and you used a string logic as your random contents to the the loop that was never posted -- making a different sense out of what looked to be non-sense to me.
But I did say something of a sort, as well you did the very same thing you just told me that I cannot do to to get your explanation of what it MIGHT mean.
You do realize that don't you?
"say it contains the string "blah", then the result would be "ablah"."
You yourself added some random contents for the loop to try and make sense out of it.
The very same thing I did. Then tell me I cannot do that but you can. isn't that funny how your mind works?
I used integer logic - and you used a string logic as your random contents to the the loop that was never posted -- making a different sense out of what looked to be non-sense to me.
Therefore the actual correct answer is:
It all depends on what the data type is being used within that loop test.
*facepalm*
You can put ANY string or number in place of where I wrote "blah" and the result is the same. Want to use "20"? Fine, replace "blah" with "20", nothing changes. I didn't add any extra behavior to the test he posted, and I didn't change the test he posted. You did both. The fact that you apparently can't even see that is very illuminating.
Last edited by suicidaleggroll; 05-10-2017 at 10:33 AM.
You can put ANY string or number in place of where I wrote "blah" and the result is the same. Want to use "20"? Fine, replace "blah" with "20", nothing changes. I didn't add any extra behavior to the test he posted, and I didn't change the test he posted. You did both. The fact that you apparently can't even see that is very illuminating.
as this example too shows you using random values.
I am not arguing that point with you.... it is all still random data that was not added to the question asked that one came up with to use to try and make sense of it. you did it, I did it. then you said I cannot but you can. you did not properly correct in my error of thought in how make sense out of that argument that was presented to try and make sense out of it for another. that is the point.
you can't see how illuminating the point I was making.
I'm out of this post it has already gone off topic enough.
as this example too shows you using random values.
I am not arguing that point with you.... it is all still random data that was not added to the question asked that one came up with to use to try and make sense of it. you did it, I did it. then you said I cannot but you can. you did not correctly properly in my error of thought in how make sense out of that argument that was presented to try and make sense out of it for another. that is the point.
you can't see how illuminating the point I was making
I never said anything about you assigning "20" to "file1" or "0" to "a" (a variable which does not even exist). That's not the problem. The problem is you then changed the test from
Code:
while [ "a$file1" != "a" ]
to
Code:
while [ "a$file1" != "$a" ]
(which you later removed in an edit) and then actually made up the contents of the while loop, incrementing a variable that is not even in the test, and used the resulting non-functionality to claim that the test did nothing. It's equivalent to somebody asking what 'while [ "$a" != "$b" ]' does, and you responding:
well, run it yourself:
Code:
a=5
b=7
c=50
while [ "$a" != "$b" ]; then c=$(date); echo $c; done
Wed May 10 09:57:22 MDT 2017
Wed May 10 09:57:22 MDT 2017
Wed May 10 09:57:22 MDT 2017
see, the test doesn't do anything, it will just break your code.
THEN, when called out on it, instead of accepting that you made a mistake and your post is incorrect, you decided to ignore that and instead lash out at the person who DID actually answer the question correctly, as if they did something wrong. It boggles the mind.
Last edited by suicidaleggroll; 05-10-2017 at 10:59 AM.
I never said anything about you assigning "20" to "file1" or "0" to "a" (a variable which does not even exist). That's not the problem. The problem is you then changed the test from
Code:
while [ "a$file1" != "a" ]
to
Code:
while [ "a$file1" != "$a" ]
(which you later removed in an edit) and then actually made up the contents of the while loop, incrementing a variable that is not even in the test, and used the resulting non-functionality to claim that the test did nothing.
I ran it both ways and it did the same thing. so go call the "test my logic before answering a question" police on me then.
I never said anything about you assigning "20" to "file1" or "0" to "a" (a variable which does not even exist). That's not the problem. The problem is you then changed the test from
Code:
while [ "a$file1" != "a" ]
to
Code:
while [ "a$file1" != "$a" ]
(which you later removed in an edit) and then actually made up the contents of the while loop, incrementing a variable that is not even in the test, and used the resulting non-functionality to claim that the test did nothing. It's equivalent to somebody asking what 'while [ "$a" != "$b" ]' does, and you responding:
well, run it yourself:
Code:
a=5
b=7
c=50
while [ "$a" != "$b" ]; then c=$(date); echo $c; done
Wed May 10 09:57:22 MDT 2017
Wed May 10 09:57:22 MDT 2017
Wed May 10 09:57:22 MDT 2017
see, the test doesn't do anything, it will just break your code.
THEN, when called out on it, instead of accepting that you made a mistake and your post is incorrect, you decided to ignore that and instead lash out at the person who DID actually answer the question correctly, as if they did something wrong. It boggles the mind.
as that code is not the same as
Code:
while [ "a$file" != a ]
this is.
Code:
#!/bin/bash
set -x
a="0"
file="100"
while [ "a$file" != a ] ;
do
echo "what?"
done
Because it has to do something in order to see what that test does to get that while loop to act on the test.
results:
Another approach would be to try to think about what will happen during execution. left side is a letter a and a string stored in a variable named file1. right side is a single letter a.
When will be the two strings (left and right side) equal to each other? The answer is simple, the right side is fix, so we can say the left side should be equal to a single letter a.
And how can you construct a single a using a$file1? I think you know the answer already: $file1 should be empty.
So this is the way we can check if $file1 contains a string. The reason was already explained by wpeckham in post #6.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.