You are not logged in.

#1 2019-09-26 19:54:07

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

[SOLVED] Why does it work in a terminal but not in a script

Please move this if required.

I'm not sure if I have a problem with my RAM (there are reasons I may think this that I won't go into), or a problem with conky, but the symptom is that conky's own memory bar will suddenly report either 0% used and the bar will show full, or it will work. As debugging, I decided to replace the internal function with a bash script and see how that goes, but here's the rub. If I enter:

TOT=`free | grep Mem | awk '{print $2}'` ; USED=`free | grep Mem | awk '{print $3}'` ; MEMUSED=`echo $USED/\($TOT/100\) | bc` ; echo $MEMUSED%

into a terminal, it works just fine, outputting the used RAM as a percentage of the total.

However, this simple script:

#!/bin/bash
TOT=`free | grep Mem | awk '{print $2}'` ; USED=`free | grep Mem | awk '{print $3}'` ; MEMUSED=`echo $USED/\($TOT/100\) | bc` ; echo $MEMUSED%

outputs diddly squat.

Why?

EDIT: even a script with just the #!/bin/bash header and

TEST=`free | grep Mem` ; echo $TEST

outputs nothing

Last edited by Roken (2019-09-29 16:26:46)


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#2 2019-09-26 20:02:11

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,709

Re: [SOLVED] Why does it work in a terminal but not in a script

Just a hunch, but I bet it has to do with the back-ticks.  Try replacing ` ... ` with $( ... )


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#3 2019-09-26 20:04:52

Swiggles
Member
Registered: 2014-08-02
Posts: 266

Re: [SOLVED] Why does it work in a terminal but not in a script

Maybe locale related? Try specifying it in your script:

#!/bin/bash
LANG=C
TOT=`free | grep Mem | awk '{print $2}'` ; USED=`free | grep Mem | awk '{print $3}'` ; MEMUSED=`echo $USED/\($TOT/100\) | bc` ; echo $MEMUSED%

Or simplify:

Print all with label:

free | awk 'FNR>1{print $1 $3/($2/100)"%"}'

Print only mem without label:

LANG=C free | awk '$1~/Mem/{print $3/($2/100)"%"}'

Last edited by Swiggles (2019-09-26 20:21:26)

Offline

#4 2019-09-26 20:39:46

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Why does it work in a terminal but not in a script

Also, awk and grep are redundant: eg.,

free | awk '/Mem/ {print $2}'

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2019-09-26 21:03:09

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

Re: [SOLVED] Why does it work in a terminal but not in a script

Nope. None of the suggested solutions works in a script (but work directly in a terminal).

This has had me stumped for a week. All of my other personal scripts work. I deleted this one and started again, but it just doesn't want to output anything (and since I'm planning on using it in conky, output is kinda required)


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#6 2019-09-26 21:05:47

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Why does it work in a terminal but not in a script

The script is executable, right?

set -x


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2019-09-26 21:11:26

Swiggles
Member
Registered: 2014-08-02
Posts: 266

Re: [SOLVED] Why does it work in a terminal but not in a script

Please give the output of

pacman -Qo /bin/bash
pacman -Qkk bash

Also try some other shell in your shebang.

Edit:

What is the output of "free"?

Last edited by Swiggles (2019-09-26 21:12:33)

Offline

#8 2019-09-26 22:18:29

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,369

Re: [SOLVED] Why does it work in a terminal but not in a script

EDIT: even a script with just the #!/bin/bash header and

TEST=`free | grep Mem` ; echo $TEST

outputs nothing

run only "free" to see what is actually printed, but I'm w/ Swiggles initial guess and would "export LC_ALL=C", also you should drastically simplify this (as mentioned by everyone ;-)

#!/bin/sh
LC_ALL=C free | awk '/Mem/ {print 100*$3/$2"%"}'

Offline

#9 2019-09-27 05:20:52

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

Re: [SOLVED] Why does it work in a terminal but not in a script

OK, in order:

yes, the script is executable:

-rwxr-xr-x   1 steve   65 Sep 27 06:13 memfree
$pacman -Qo /bin/bash
/usr/bin/bash is owned by bash 5.0.011-1
$pacman -Qkk bash
backup file: bash: /etc/bash.bashrc (Modification time mismatch)
backup file: bash: /etc/bash.bashrc (Size mismatch)
warning: bash: /usr/bin (UID mismatch)
bash: 241 total files, 1 altered file
$free
              total        used        free      shared  buff/cache   available
Mem:       16395012     1503444    14112220      154200      779348    14447412
Swap:      18754556           0    18754556

I've also tried with one of the simplified options (I would have gotten around to simplifying, the original script was whilst I was working on getting where I wanted to be) with the same result.
I'm not sure about the UID mismatch but other scripts work fine hmm


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#10 2019-09-27 05:31:32

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Why does it work in a terminal but not in a script

jasonwryan wrote:

set -x


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#11 2019-09-27 05:57:22

Awebb
Member
Registered: 2010-05-06
Posts: 6,688

Re: [SOLVED] Why does it work in a terminal but not in a script

jasonwryan wrote:
jasonwryan wrote:

set -x

This. Your one-liner works for me in a script.

Offline

#12 2019-09-27 06:55:24

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,369

Re: [SOLVED] Why does it work in a terminal but not in a script

When

seth wrote:

run only "free" to see what is actually printed

seth meant "in the script" - we know it works in the interactive shell.
We know

TEST=`free | grep Mem` ; echo $TEST

fails, we know the simplified version w/o variable assignment fail, if "free | grep Mem" produces no output, "Mem" is not in the output of "free" or grep is broken (but the awk only versions alos failed…)

Offline

#13 2019-09-27 13:54:49

eda2z
Member
From: Woodstock, IL
Registered: 2015-04-21
Posts: 71

Re: [SOLVED] Why does it work in a terminal but not in a script

If I take the original one-liner, place it in a file (broken.sh), change it to executable (chmod +x broken.sh) and execute it from the command line, I get the same results as if I execute the one-liner directly from the command line. I don't think my environment is much different from yours but if I execute 'pacman -Qkk bash' I do not get any errors. Also, my system shows a bash in both /bin and /usr/bin with the same inode number (hard link). Have you tried a shebang of '#!/usr/bin/env bash'?

Offline

#14 2019-09-28 09:58:14

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

Re: [SOLVED] Why does it work in a terminal but not in a script

Thanks, all. I took a different approach. Since this is to replace the conky builtin memory bar, I simply built it in to conkyrc directly, and it's working fine that way. For info:

${color5}RAM:$color8}${goto 70}${execbar free | awk '$1~/Mem/{print $3/($2/100)}'}${alignr}${color}${exec free | awk '$1~/Mem/{printf "%0.0f\n",$3/($2/100)"%"}'}%

Last edited by Roken (2019-09-28 09:58:55)


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#15 2019-09-28 12:19:14

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,529
Website

Re: [SOLVED] Why does it work in a terminal but not in a script

I'm glad you have a solution that worked for you.  But you were given several suggested diagnostic tests in this thread, some of them repeated multiple times and you ignored almost every single one of them.  Do not ask for help here if you don't want help.  If you only intend to track your independent progress ignoring almost all input, then do so on a blog.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#16 2019-09-29 06:29:00

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

Re: [SOLVED] Why does it work in a terminal but not in a script

Trilby wrote:

But you were given several suggested diagnostic tests in this thread, some of them repeated multiple times and you ignored almost every single one of them.

My apologies.

ALL of the diagnostic tests had the same result. Absolutely nothing.

Do not ask for help here if you don't want help.

Nice attitude.


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#17 2019-09-29 06:41:13

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,369

Re: [SOLVED] Why does it work in a terminal but not in a script

You got "Absolutely nothing" with either running "free" or "set -x" on top of the script?????
Nothing like is "Absolutely no output"???

Offline

#18 2019-09-29 10:12:25

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

Re: [SOLVED] Why does it work in a terminal but not in a script

Absolutely nothing. i.e. it returned to the prompt with diddly squat.

steve@Tex:(11:13)~$ cat scripts/memfree 
#!/bin/bash
set -x
free
steve@Tex:(11:13)~$ memfree 
steve@Tex:(11:13)~$ 

Oh, and before you ask, the path is in $PATH, and in fact, all the other scripts there work.

Last edited by Roken (2019-09-29 10:15:33)


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#19 2019-09-29 11:19:00

Swiggles
Member
Registered: 2014-08-02
Posts: 266

Re: [SOLVED] Why does it work in a terminal but not in a script

What is

whence memfree

?

Offline

#20 2019-09-29 12:35:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,529
Website

Re: [SOLVED] Why does it work in a terminal but not in a script

And the output of `pacman -Qo bash` and `pacman -Qkk bash` returned absolutely no results?  I highly doubt that.  And you tried another shell in the shebang?  I'm doubting that too.  In any case, there's nothing more to be done here so I'll leave it be.

@swiggles, isn't `whence` a zsh-only command?

Last edited by Trilby (2019-09-29 12:35:58)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#21 2019-09-29 12:55:32

Swiggles
Member
Registered: 2014-08-02
Posts: 266

Re: [SOLVED] Why does it work in a terminal but not in a script

Trilby wrote:

@swiggles, isn't `whence` a zsh-only command?

ksh is the origin I think, but you might be right, I am so used to zsh myself. Good catch, sorry!

Use "type" instead for bash (is the same as "whence -v"):

type memfree

Offline

#22 2019-09-29 15:44:52

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,709

Re: [SOLVED] Why does it work in a terminal but not in a script

my $0.02

ewaller@odin/home/ewaller % ls -l test
-rwxr-xr-x 1 ewaller ewaller 24 Sep 29 08:40 test
ewaller@odin/home/ewaller % cat test
#!/bin/bash
set -x
free
ewaller@odin/home/ewaller % test
ewaller@odin/home/ewaller[1] % which test
/usr/bin/test
ewaller@odin/home/ewaller % ./test 
+ free
              total        used        free      shared  buff/cache   available
Mem:       16328056     1836048     1050568      332740    13441440    13817324
Swap:      46283772       26112    46257660
ewaller@odin/home/ewaller % 

Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#23 2019-09-29 16:12:11

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

Re: [SOLVED] Why does it work in a terminal but not in a script

@Trilby

Output of -Qo and -Qkk in post #9

Honestly, whilst I would like to know the reason for this, if people are going to criticize me for not responding to tests, please check that I haven't, first. I was brought up in an age when we learned it's nice to be nice, and to check.

Last edited by Roken (2019-09-29 16:14:23)


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#24 2019-09-29 16:14:41

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,369

Re: [SOLVED] Why does it work in a terminal but not in a script

Check what memfree is actually resolved to because there's no way the script is executed at all.
You may also try to invoke it by its full path.

Offline

#25 2019-09-29 16:26:13

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,368

Re: [SOLVED] Why does it work in a terminal but not in a script

Seth, you may have it. Naming it memfree seems to be the problem, and there is a library by the same name. Renaming gets it going.

I have no idea what memfree actually is, but gonna make this solved.


Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

Board footer

Powered by FluxBB