Skip to main content
14 events
when toggle format what by license comment
Feb 18, 2021 at 21:19 comment added Andrew Newby @CharlesDuffy haha I'm used to using backticks in Perl code =) (only really dabble in bash for the very rare job like this). I'll remember that for next time
Feb 18, 2021 at 21:16 comment added Charles Duffy @AndrewNewby, you can use triple-backticks to make single-backticks work in SO, though it's always better to use the POSIX-specified $(...) syntax instead; the standard-compliant syntax is easier to nest, doesn't make surprising changes to how backslashes behave within them, etc. -- and the POSIX sh standard has been out since 1992, so you don't need to worry about finding modern shells that don't support it.
Feb 18, 2021 at 21:08 comment added Andrew Newby @CharlesDuffy thats because I had to remove the backticks from it, otherwise SO got confused with the code format ;)
Feb 18, 2021 at 17:22 comment added Charles Duffy Eh? Your free=free -m | grep Mem | awk 'print $4 code sample certainly does not work as expected; it doesn't even run free at all (it tries to run a command named -m, and then presumably fails because that command doesn't exist), nor does it persistently assign to a variable named free (as the variable free is given the value free only for the duration of that immediately-failing -m command). Now, if what you meant was free=$(free -m | awk '/Mem/ { print $4 }'), then yes, that certainly works; but you need a command substitution on the right-hand side of your assignment.
Feb 18, 2021 at 15:41 comment added Andrew Newby @CharlesDuffy thanks The if [] statement was just an example free=free -m | grep Mem | awk '{print $4} works as expected (it holds the amount of ram available as an int). It seems like the issue was the & where it was spawning a sub-process. I didn't realise that was the behaviour.
Feb 18, 2021 at 15:35 vote accept Andrew Newby
Feb 18, 2021 at 15:34 comment added Charles Duffy Anyhow -- the two duplicates this is closed with explain why using & causes the assignment to be ineffective.
Feb 18, 2021 at 15:34 history duplicates list edited Charles Duffy duplicates list edited from '&&' vs. '&' with the 'test' command in Bash to '&&' vs. '&' with the 'test' command in Bash, Assign variable in the background shell
Feb 18, 2021 at 15:32 history closed oguz ismail
Charles Duffy bash
Duplicate of '&&' vs. '&' with the 'test' command in Bash
Feb 18, 2021 at 15:30 comment added Charles Duffy Also, the only standard comparison operator inside [ is =, not ==. In some shells, using == will cause an error.
Feb 18, 2021 at 15:29 comment added Charles Duffy FYI, [ is not part of if syntax. It's just one of the infinite number of commands you can use anywhere, including in an if, but you can use if without it; for example, if grep -qe pattern file; then...
Feb 18, 2021 at 15:27 answer added Bayou timeline score: 3
Feb 18, 2021 at 15:25 comment added markp-fuso what is the value of free? are you sure it's getting set to something -gt 0? is the echo firing? & sleep 5 should probably be && sleep 5 ... but I'm also wondering about the placement of this clause (ie, when exactly do you want the script to sleep?); as alluded to in Bayou's answer ... a single & says to spawn a sub-process and put said process in the background in which case the assignment (performed in a sub-process) is not seen by the parent process
Feb 18, 2021 at 15:19 history asked Andrew Newby CC BY-SA 4.0