LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-10-2017, 09:27 AM   #1
orri23
LQ Newbie
 
Registered: May 2017
Posts: 3

Rep: Reputation: Disabled
while [ "a$file1" != "a" ] What is this doing?


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

Thanks!
 
Old 05-10-2017, 09:47 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 '$'

Last edited by BW-userx; 05-10-2017 at 09:49 AM.
 
2 members found this post helpful.
Old 05-10-2017, 09:48 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
1 members found this post helpful.
Old 05-10-2017, 09:59 AM   #4
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
This seems like a horrible way of checking a variable is not empty/null, the '! -z' method given by suicidalggroll is definitely a better test.
 
1 members found this post helpful.
Old 05-10-2017, 10:03 AM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by BW-userx View Post
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.
 
2 members found this post helpful.
Old 05-10-2017, 10:05 AM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,069

Rep: Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444
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.
 
2 members found this post helpful.
Old 05-10-2017, 10:05 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,813
Blog Entries: 13

Rep: Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875
Hi orri23 and welcome to LQ.

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.

Last edited by rtmistler; 05-10-2017 at 10:07 AM.
 
Old 05-10-2017, 10:26 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by suicidaleggroll View Post
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.

Therefore the actual correct answer is:

Last edited by BW-userx; 05-10-2017 at 10:31 AM.
 
Old 05-10-2017, 10:31 AM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 05-10-2017, 10:39 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by suicidaleggroll View Post
*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.
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.

Last edited by BW-userx; 05-10-2017 at 10:46 AM.
 
Old 05-10-2017, 10:55 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 05-10-2017, 10:56 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by suicidaleggroll View Post
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.

Have a nice day.

Last edited by BW-userx; 05-10-2017 at 11:05 AM.
 
Old 05-10-2017, 11:24 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by suicidaleggroll View Post
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:
Code:
+ '[' a100 '!=' a ']'
+ echo 'what?'
what?
+ '[' a100 '!=' a ']'
+ echo 'what?'
what?
^C
on and on it goes caught up in a endless loop

chaning the code
Code:
#!/bin/bash

set -x

a="0"
file="100"

while [ "a$file" != a ] ;
do
echo "what?"

a="a100"

done
does not stop it.
Code:
+ '[' a100 '!=' a ']'
+ echo 'what?'
what?
+ a=a100
+ '[' a100 '!=' a ']'
+ echo 'what?'
what?
+ a=a100
^C
userx%slack
Because it is not matching an 'a'
remove a=a100
add file=a
and gets this
Code:
what?
+ file=a
+ '[' aa '!=' a ']'
+ echo 'what?'
what?
+ file=a
+ '[' aa '!=' a ']'
^C
still an endless loop.

what condition needs to be made to get "a$file" to equal 'a' ?

file=""

Code:
#!/bin/bash

set -x

a="0"
file="100"

while [ "a$file" != a ] ;
do
echo "what?"

file=""

done
Code:
userx%slackwhere ⚡ scripts ⚡> ./what
+ a=0
+ file=100
+ '[' a100 '!=' a ']'
+ echo 'what?'
what?
+ file=
+ '[' a '!=' a ']'
userx%slackwhere ⚡ scripts ⚡>

Last edited by BW-userx; 05-10-2017 at 11:35 AM.
 
Old 05-10-2017, 11:46 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,769

Rep: Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056Reputation: 7056
Quote:
Originally Posted by orri23 View Post
"a$file1" is not equal to "a"
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.
 
Old 05-10-2017, 01:47 PM   #15
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,069

Rep: Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444
Ok, bottom line here. The code
Code:
while [ "a$file1" != "a" ]
is an ancient test format. It still does work but should be replaced by
Code:
while [ -n "$file1" ]
or something more equivalent. All of these test that the string value of variable 'file1' is not null.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remapping a Northgate Omnikey "Omni" button to serve as a "Windows" or "Super" key. CVAlkan Linux - Hardware 6 01-07-2019 12:21 PM
[SOLVED] X: "loading extension glx" "no screens found" "fatal server error" (w/ nvidia driver) Geremia Slackware 7 12-29-2014 11:00 AM
[SOLVED] "net rpc" "failed to connect to ipc$ share on" or "unable to find a suitable server" larieu Linux - General 0 11-09-2014 12:45 AM
[SOLVED] Scripting Help: Using "File1" as list of objects to search for in "File2" Asharru Programming 10 09-05-2010 06:53 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration