r_bash – Telegram
Weird Error in Sort

This is the weirdest thing I've experienced, and I'm half convinced I am going crazy.

I am using the sort command on a Linux Mint workstation:

sort (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.

I want to sort a text file based on the third column, and to use a generic numeric sort (since this has floats in scientific notation).

So I run:

sort -g -k 3 input_file.txt > output_file.txt

And the file it returns is sorted based on the 1st column, and not the third.

I have also tried "sort -k3g", "sort --key=3 --general-numeric-sort", etc. all with the same effect.

I can run "sort -k 3" and then it runs sort based on the third column, but using string logic rather than generic numerals.

I have transferred the same text file to my OSX machine "2.3-Apple (154)" -- and it works fine. I have transferred it to another linux machine, running sort 8.22

sort (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.

and it works fine again. I have literally copied the commands between the two machines, so I know I am running the same commands on the same files, and getting different answers.

What is going on? Did 8.32 break sort or change some structure for the commands in a way I don't know about?

https://redd.it/15ghwjh
@r_bash
can i call a function from inside an IF or while?

eg.

if [ (myFunction "enter your number" 1 5) == 3 \]; then

echo "u entered 3"

OR--------

while (myFunction "enter your number" 1 5) != 3; do

echo "incorrect"

done

https://redd.it/15gt5ho
@r_bash
Is it the most effective way to add new keys and sort them?

I have the following JSON input:

{
"copyToClipboardCmd": {
"type": "string"
},
"editPreset": {
"type": "string"
},
"edit": {
"type": "string"
},
"editAtLine": {
"type": "string"
},
"editAtLineAndWait": {
"type": "string"
},
"open": {
"type": "string"
},
"openLink": {
"type": "string"
}
}`

and my goal is to add `noscript` and `denoscription` properties to all objects and then sort keys in the following order: `noscript`, `denoscription`, `type`.

I've wrote the following jq noscript:

with_entries(.value.noscript = (.key | sub("(?<a>[A-Z])"; " \(.a)"; "g") | ascii_downcase)) |
with_entries(.value.denoscription = .value.noscript + "\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default") |
with_entries(.value = { noscript: .value.noscript, denoscription: .value.denoscription, type: .value.type })

It does what I want to do:

{
"copyToClipboardCmd": {
"noscript": "copy to clipboard cmd",
"denoscription": "copy to clipboard cmd\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default",
"type": "string"
},
"editPreset": {
"noscript": "edit preset",
"denoscription": "edit preset\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default",
"type": "string"
},
"edit": {
"noscript": "edit",
"denoscription": "edit\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default",
"type": "string"
},
"editAtLine": {
"noscript": "edit at line",
"denoscription": "edit at line\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default",
"type": "string"
},
"editAtLineAndWait": {
"noscript": "edit at line and wait",
"denoscription": "edit at line and wait\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default",
"type": "string"
},
"open": {
"noscript": "open",
"denoscription": "open\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default",
"type": "string"
},
"openLink": {
"noscript": "open link",
"denoscription": "open link\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default",
"type": "string"
}
}

, but I am curious whether it's the best (the shortest) way to do it. I had another idea to write something like this:

with_entries(.value.noscript = (.key | sub("(?<a>[A-Z])"; " \(.a)"; "g") | ascii_downcase)) |
.[] | .denoscription = .noscript + "\nhttps://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#default" |
{ noscript, denoscription, type }

But I have trouble in joining all those objects into a one big object: I've tried to wrap all code inside `{...}` but got an error:

jq: error: syntax error, unexpected '(', expecting '}' (Unix shell quoting issues?) at <top-level>, line 1:
{with_entries(.value.noscript = (.key | sub("(?<a>[A-Z])"; " \(.a)"; "g") | ascii_downcase)) |
jq: error: syntax error, unexpected '}', expecting $end (Unix shell quoting issues?) at <top-level>, line 3:
{ noscript, denoscription, type }}
jq: 2 compile errors

So... I guess my question is how to wrap several objects into a root one?

https://redd.it/15gwlbd
@r_bash
Started learning bash

Hi everyone today I started learning bash with no prior knowledge.I’m excited but also moving at a realistic pace so I don’t feel overwhelmed. I’m currently watching the Microsoft developer series (Bash for beginners)on YT.If you have any tips and advices for me I’ll be happy to accept them.

https://redd.it/15h1sd0
@r_bash
Github noobie (somewhat bash related)

I want to make my bash scrips available so I created a github profile and uploaded some noscripts to a public repo.

Here comes the issue:
I can't just git clone a noscript from inside the terminal without being asked for authentication. How do I taggle this? I'd like to be able to just git clone without any further ado. I don't mind to have the noscripts available for everybody.

I know, pretty noob question...

https://redd.it/15h8l4x
@r_bash
Array not populating in bash noscript ?

Here is a link to my bash noscript that I am working on , as long with a text file and awk file that I use with the noscript.

https://pastebin.com/xTGyk5su

For what ever reason my quotawheels array is not being populated and I cant figure out why ? If anyone can help , I would really appreciate it.

https://redd.it/15hhgek
@r_bash
need to extract stats from a webpage

ive used wget to download a url. so i have a html file. i need to extract all these stats fromthe text in it and tabulate them. i don't think the sed command can do that?

https://redd.it/15hl0v1
@r_bash
Bash History full of garbage

Hi All,

Linux Mint 21.2 Victoria -- Cinnamon

Below I copied a small chunk of the random stuff in my bash history, anyone know what is creating all this?

Thanks, Joe

===============
091 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim'`"

1092 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim\\0057lua'`"

1093 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim\\0057lua\\0057core'`"

1094 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim\\0057lua\\0057core\\0057plugin\\0137config'`"

1110 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1114 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056vim\\0057spell'`"

1115 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056vim'`"

1117 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1121 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82'`"

1130 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82\\0057spell'`"

1132 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82'`"

1133 cd "`printf '%b' '\\0057usr\\0057share\\0057vim'`"

1134 cd "`printf '%b' '\\0057usr\\0057share'`"

1135 cd "`printf '%b' '\\0057usr'`"

1136 cd "`printf '%b' '\\0057'`"

1137 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82\\0057spell'`"

1138 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1142 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056vim'`"

1789 cd "`printf '%b' '\\0057home\\0057joe\\0057VirtualBox\\0137VMs\\0057Debian12'`"

1790 cd "`printf '%b' '\\0057home\\0057joe\\0057VirtualBox\\0137VMs\\0057Debian12\\0057Logs'`"

1833 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1837 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057applications'`"

1838 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057doc\\0057kitty\\0057html'`"

1839 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057doc\\0057kitty'`"

1840 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057doc'`"

1841 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share'`"

&#x200B;

&#x200B;

https://redd.it/15hnoe4
@r_bash
Need to return the largest set

Hey all, I'm writing a noscript to query an endpoint that returns a list of groups and members. That's fine but is there a way that I can parse that list to just return the group with the largest membership?

https://redd.it/15hz5zf
@r_bash
Read function and many options

hey guys I am working lately on automated noscript that create file with variety extension and I came across with the needs to use read function with many option
# which one of these will work
A) read -are Pyfiles
B) read -a -r -e Pyfiles
C) or none of these and if so why?

and I am sure it C but why ?

https://redd.it/15i8omu
@r_bash
What's wrong with my noscript?

I am not ashamed to say that I am a bit of a noscript monkey when it comes to writing noscripts for bash; so I am probably doing something simple wrong.

I want to have a noscript that does a simple daily backup and then remove backups that are older than seven days old. I can see that the backup element works, but the removal of existing backups does not.

#!/bin/bash -e

# backup folder
destination_folder="/home/[folder]/backup/drupal/$(date +%F)"
archive_file="backup-$(date +%H%M).tar.gz"
rotate=7
mkdir -p "$destination_folder"

# backup

/bin/tar --exclude='/home/[folder]/backup' -Oczf "$destination_folder/$archive_file" /var/www/html/[site folder]/

# attempt delete older than 7 days
find /home/[folder]/backup/drupal/ -name '*.tar.gz' -mtime +6 -exec rm -rf {} \;

Would appreciate a fresh pair of eyes - I even tried to move the find and remove to a separate file (and tried rewriting it) to try and fix it, without success.

https://redd.it/15jib6f
@r_bash
Script to Automatically Start a glassfish domain once it's down?

Steps to reproduce this issue:

1) Glassfish domain called domain1 goes down.

2) Now, start that domain again.

Algorithm like this should exist:

if(domain1 is down)

{
path/to/asadmin start-domain domain1
}

How do I check if domain1 is down or not?
Assume there are multiple domains, around 8 of them And different servers have varying amount of domains.

My pseudocode:

domainarray= get list of all domains in an array

for(i=0;i<domain
array.length;i++)

{
if(path/to/asadmin stop-domain domainarray[i]==0) //assuming exit code 0 returns if the domain is already stopped
{
path/to/asadmin start-domain domain
arrayi;
}
}

Problems with my code:

I might be attempting to stop an unstopped domain which is very bad.

Is there an equivalent of systemctl is-active servicename for glassfish domains?

Sources: https://glassfish.org/docs/5.1.0/administration-guide/domains.html#giurs

https://www.freedesktop.org/software/systemd/man/systemd.service.html

https://redd.it/15jl0cu
@r_bash
Script to send an alert mail once the disk space is above 90% sends disk full even when the disk isn't full, how to resolve this issue?

#!/bin/bash

df -m > myfile
server_ip_address=$(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1)

if awk '$2 > 10000 && $5 > 90' myfile ; then
echo "Disk Full in $server_ip_address"
else
echo "Nothing wrong with the server"
fi


When I execute the noscript, always get disk full as output?
Output of df -m looks like this:

Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 949305 512827 436479 55% /
devtmpfs 3811 0 3811 0% /dev
tmpfs 3823 0 3823 0% /dev/abc
tmpfs 3823 18 3806 1% /run
tmpfs 3823 0 3823 0% /sys/fs/cgroup
/dev/sda2 1014 175 840 18% /boot
/dev/sda1 1022 12 1011 2% /boot/efi
tmpfs 765 0 765 0% /run/user/2000



The outut of df -m differs from server to server. I am thus comparing if Size>10GB and Use%>90%. As some disk with size<10GB may not be important to be under 90% disk usage.


**Update 1:**

GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

OS is centos 7

Problem with the current code:

I need to compare the second row's columns, but my current code is comparing the first rows columns, that's why I suppose it's not working. How to resolve the issue?

https://redd.it/15jkztg
@r_bash
Script to send an alert mail once the disk space is above 90% sends disk full even when the disk isn't full, how to resolve this issue?

#!/bin/bash

df -m > myfile
server_ip_address=$(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1)

if awk '$2 > 10000 && $5 > 90' myfile ; then
echo "Disk Full in $server_ip_address"
else
echo "Nothing wrong with the server"
fi


When I execute the noscript, always get disk full as output?
Output of df -m looks like this:

Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 949305 512827 436479 55% /
devtmpfs 3811 0 3811 0% /dev
tmpfs 3823 0 3823 0% /dev/abc
tmpfs 3823 18 3806 1% /run
tmpfs 3823 0 3823 0% /sys/fs/cgroup
/dev/sda2 1014 175 840 18% /boot
/dev/sda1 1022 12 1011 2% /boot/efi
tmpfs 765 0 765 0% /run/user/2000



The outut of df -m differs from server to server. I am thus comparing if Size>10GB and Use%>90%. As some disk with size<10GB may not be important to be under 90% disk usage.


**Update 1:**

GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

OS is centos 7

Problem with the current code:

I need to compare the second row's columns, but my current code is comparing the first rows columns, that's why I suppose it's not working. How to resolve the issue?

https://redd.it/15jmqf4
@r_bash
Is there any dmenu alternative configured in Lua/YAML/JSON/TOML?

I need a such application launcher which supports such configuration file format there is an already existing LSP and linting for. For instance, I've found projektor launcher, but it seems it's dead, no recent commits exist.

It would be nice to have config in Lua format as I plan to use Awesome window manager which uses Lua for setup. But at the same time I love YAML for it's simplicity because not always I need rich customization capabilities.

https://redd.it/15jx9yn
@r_bash
I created a noscript to start or stop an Aria2 downloader daemon.

For those of you who aren't familiar, aria2 has over 30,000 stars on GitHub because of its usefulness to people who are looking for one of the best command line download softwares on the internet.

I have included info and instructions at the top of the noscript for the user to read.

I hope someone finds this useful!

Cheers!

Aria2 Repo

GitHub Script

https://redd.it/15k2q0q
@r_bash
awk Match everything between two patterns, but ignore the first occurrence of the end pattern

# Overview

I'm hacking old Chromeboxes to be digital signage for the school district I'm working at over the summer. The functional needs are working, but I discovered that the Chromeboxes can't drive 4K displays without a significant performance hit.

I'm modifying the runtime noscript to check for available resolutions below 4K (or QHD if the Chromebox is using two monitors, just to be safe), and pick the highest supported resolution that preserves the aspect ratio of the current resolution if possible. Yeah, it's a bit overengineered, but I'm not going to be there if something goes wrong, so I want to make this as functional as possible.

# Problem

To get available resolutions for each monitor (iterated in a for loop), I'm parsing xrandr -q, which outputs the list of available resolutions in a nice, indented list like this:

Screen 0: minimum 320 x 200, current 3280 x 1080, maximum 16384 x 16384
HDMI-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm
1920x1080 60.00+ 50.00 59.94
1680x1050 59.88
1600x900 60.00
1280x1024 60.02
1440x900 59.90
1280x800 59.91
1280x720 60.00 50.00 59.94
1024x768 60.00
800x600 60.32
720x576 50.00
720x480 60.00 59.94
640x480 60.00 59.94
720x400 70.08
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-2 connected 1360x768+1920+0 (normal left inverted right x axis y axis) 410mm x 230mm
1360x768 60.02
+
1920x1080i 60.00 59.94
1280x720 60.00 59.94
1024x768 75.03 70.07 60.00
1440x480i 59.94
800x600 75.00 60.32
720x480 60.00 59.94
720x480i 60.00 59.94
640x480 75.00 60.00 59.94
720x400 70.08

The command I have written to parse this information is

DISPLAY=:0 xrandr | awk -v mon="$MONITOR" '$0 ~ mon, $0 !~ /^ /{print $1}'

I want awk to print everything between line with the monitor's name (eg, HDMI-1) and the end of the indentation block, excluding the headings themselves (some help on that would be cool as well). With MONITOR = "HDMI-1"

1920x1080
1680x1050
1600x900
1280x1024
1440x900
1280x800
1280x720
1024x768
800x600
720x576
720x480
640x480
720x400

However, this only returns

HDMI-1

I think I understand the issue. The line that matches the start pattern also matches the end pattern, so awk only prints that line and calls it a job well done. How do I tell awk to ignore the line with the start pattern and stop at the next line that matches the end pattern?

https://redd.it/15k3fjk
@r_bash