I'm new to linux and particularly using shell scripts. On my android, i'm trying to use mem.sh to output total amount of free memory + swap. I think one error is how the variables are defined but I don't know how to rectify this.when run as a script, it outputs error in screenshot. I understand why, but can't figure out how to resolve it. Help from someone more intelligent than me please?
Nevermind then, sorry.
It's a syntax error. Problem is that you can't just do this:< (free -m)
Bash sees the ( token and doesn't understand what the fuck you mean. Perhaps you were trying to do shell interpolation, in which case you should have prefixed it with a $ character. So it should look like $(command args) instead of (command args)
>>61221402
Process substitution <(free -m) isn't available in sh.
Seeing the shebang, #!/bin/bash, you have to either execute it with ./mem.sh (if you enabled executable bit with chmod +x mem.sh) or do bash mem.sh instead of sh mem.sh
>>61221829
see http://tldp.org/LDP/abs/html/process-sub.html
>>61221402
You can also get around the issue by piping to awk instead of making it read from a file:
free -m | awk '/^Swap/ {print $4}'
>>61221854
Hrm... then maybe OP is using an outdated version of Bash that does not support this? I know Android ships with Busybox, which is pretty shitty. If you want a good shell on Android, you pretty much have to use Termux.
>>61221981
This is in Termux, I'm just not great at this. With >>61221847 solution, error message 'ambigious redirect' comes up.
Have not yet tried >>61221884
Thanks for your help, I've found a solution for what I needed using >>61221884 . I was trying to get the script to output total free memory for klwp, but could not use the code in screenshot to do so, so needed to use a script. Thankfully, >>61221884 s solution works fine in klwp :)
>>61222122
*in original screenshot
>>61221829
>>61221981
If you're uncertain about the advice you are giving, remove the trip. You have no notoriety.