r_bash – Telegram
Access items in JSON using Bash

I have a json structure similar to the following (created using the AWS CLI)



[
{
"Instance":"10.120.100.234",
"Name":"Server A"
}
,

{
"Instance":"10.120.101.210",
"Name":"Server B"
}
,

{
"Instance":"10.120.100.175",
"Name":"Server C"
}

]

If I have this in a variable ($OUTPUT, for example), how can I access each item in turn? I need to run a command on each server in turn, then write a line with the server name and instance IP (and return status of the command) on it.


I'm working in bash, jq is available, and I could (if I needed to) write/run this in python also; whichever gives me the quickest way forward.

https://redd.it/y8bih3
@r_bash
Timeout command not killing Python noscript

Hey there,

I have a Python noscript that I need to kill after it's been running for 10 seconds.

I've tried both of the following commands but it doesn't kill the process for some reason I can't diagnose:

timeout 10 python example.py
timeout --kill-after=10 10 example.py

Thanks for your help.

https://redd.it/y8osfi
@r_bash
getting the month number by the month name

Hello, folks. I need your help. Is there a native method for getting the month number by the month name? for example: <some command> May. Answer 5.

https://redd.it/y8pnes
@r_bash
Newbie question. How to extract the first columm of a line containing a range of values?

Im trying to grep or awk the first word of each line that contains value between 1000-6000

I managed to extract the value it self or the first value of every columm regardless of value, but can't manage to do both at once.

https://redd.it/y8yibd
@r_bash
Can you help me with my homework?

I've just started uni and I don't understand noscripting at all. The lectures aren't really good so I only know the basics from online.

The homework is:

&#x200B;

There is an input.txt file I have to read from, it looks like this:

&#x200B;

James Smith, Mon 1 3 4 5, Tu 1 2 3, Wed, Thur 3 4 5 6 7, Fri 1 2 3 4

Larry Wheels, Mon 1 3 5, Tu 1 2 3 5, Wed 1 2, Thur 3 4 5 7, Fri 1 2 3

...

(Teachers names, Days and their classes)

&#x200B;

a) Get the name of the teachers who don't have classes on Wednesday, if everybody has then print "None"

b) Print the name of the Teachers and how many classes do they have.

c) Read a day from the terminal and print the names of the teachers who don't have first classes.

&#x200B;

I would really appreciate your help. I will learn a lot from this and I think I will be able to do the next homework on my own.

https://redd.it/y95rog
@r_bash
This media is not supported in your browser
VIEW IN TELEGRAM
How do i get history suggestion like this in my bash is there any plugin for oh my zsh. Not autocomplete but history suggestion.
https://redd.it/y9gja2
@r_bash
sed replace non-ascii chars in substrings, but only between double quotes

Hello,

&#x200B;

i have a lot of text files (*.cue files) which contain the following line among others:

FILE "hello - world..!!.flac" WAVE

What i want:

FILE "hello_-_world____.flac" WAVE

(replace all dots except last would be the luxus version, but not necessary)

&#x200B;

The problem:

I can't figure it out to get sed to replace every non ascii [\^A-Za-z0-9-_.\] by a underscore. What i found until now:

sed '/FILE /s/".*"/"_"/g' test.cue

This edits only the correct line (i want it that way) but it replaces the whole string hello - world !! by only one underscore. What im doing wrong ?

&#x200B;

https://redd.it/y9np6x
@r_bash
Help

Joined a course a bit late. Would really appreciate if someone could help me with some simple assignments of bash noscripts that I have to submit. DM me if you're kind enough. Would take hardly 5-10 mins if you're well versed. Thanks.

https://redd.it/y9opbc
@r_bash
How can I bind output to variable?

For example if I run

awk -F: '{ if ($3 >= 1000 && $3 <= 60000) { print $1 } }' /etc/passwd | nl

And it gives me a numbered list of users, next I want to put them into a variable so the user can choose which one by inputting a number.

Any help would be appricated!

https://redd.it/y9p9mw
@r_bash
Problem with sed when adding to end of line

Hello all,

I'm trying to add a string to the end of a line here (it's for a csv import):

address,denoscription,dnsname,status

192.168.62.1/24,Console aro,con-aro.e.retobu.com, 192.168.62.4/24,Console ski,con-ski.e.retobu.com,

I'm trying to add "active" to every line for the status field. I tried it like this:

sed 's/$/active/'

At least thats what the internet told me to do. But when I do this, this is the result:

actives,denoscription,dns
name,status
active8.62.1/24,Console aro,con-aro.e.retobu.com,
active8.62.4/24,Console ski,con-ski.e.retobu.com,

I don't get what I'm doing wrong...

https://redd.it/y9t1zk
@r_bash
variable value to create new variable

I'm new to bash and wondering whether it is possible to have the value stored in a variable to create another variable?

myvar=helloworld1

The value of myvar will be a dynamic, meaning that it will output helloworld1, or helloworld2 or helloworld3.

How can i achieve making the value of myvar as the name of a new variable?

https://redd.it/y9qo1l
@r_bash
increase the size of command history

I think this is a simple configuration using two environment variables. But, for some reason my system is honoring these variables. Does anyone have an idea why could this be?

$ echo $HISTSIZE 
10000
$ echo $HISTFILESIZE
20000
$ wc $HISTFILE
503 1549 14025 /home/someuser/.bash_history


thank you

https://redd.it/ya0hkh
@r_bash
Good Beginner Manual Recommendations?

I’m fairly new to bash noscripting and i’m currently working on a program for my bioinformatics class. I feel like my notes aren’t sufficient though for efficacy and I was wondering if anyone recommends an updated beginner manual that they liked.

Thanks :)

https://redd.it/yaekmr
@r_bash
Syntax error near unexpected token 'done'

Im sure its something embarrassingly simple, but i am new to this and ive been trying to get this to run for like an hour now. Can anyone help me understand why its not expecting done? This is on a chroot installation of fedora 35 on android 12 if it matters.

#!/usr/bin/bash
port=null
while :
do
wget -O ftp://192.168.1.3/port.txt
if $port == $(tail --lines 1 /home/port.txt | tr -d \\r);
then
echo "skipping service update and avahi restart"
cat >/dev/null << SKIPRESTART
else
echo "updating port, restarting avahi"
$port=$(tail --lines 1 /home/port.txt | tr -d \\r)
cat > /etc/avahi/services/oculus.service <<EOF
<?xml version="1.0" standalone='no'?>
<!--\*-nxml-\*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">oculus</name>
<service protocol="ipv4">
<type>_oculusal_sp._tcp</type>
<port>${port}</port>
<host-name>vr-pc.local</host-name>
</service>
</service-group>
EOF

servicectl restart avahi-daemon
fi
SKIPRESTART
sleep 10
done

Help plz

Edit: duh. When it skips to skiprestart the if is never closed nevermind

https://redd.it/yaf9zs
@r_bash
How to extract volume of active sink

Hi everyone

Trying to extract current volume of active sink. I use this to get active sink name

`pacmd list-sinks |awk '/* index:/{print $3}'

but now i don't know how to extract volume of active sink.

This is pactl list sinks output

Sink #0
State: SUSPENDED
Name: alsa_output.pci-0000_00_1f.3.analog-stereo
Denoscription: Built-in Audio Analog Stereo
Driver: module-alsa-card.c
Sample Specification: s16le 2ch 44100Hz
Channel Map: front-left,front-right
Owner Module: 6
Mute: no
Volume: front-left: 26071 / 40% / -24.02 dB, front-right: 26071 / 40% / -24.02 dB
balance 0.00
Base Volume: 65536 / 100% / 0.00 dB
Monitor Source: alsa_output.pci-0000_00_1f.3.analog-stereo.monitor
Latency: 0 usec, configured 0 usec
Flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY
Properties:
alsa.resolution_bits = "16"
device.api = "alsa"
device.class = "sound"
alsa.class = "generic"
alsa.subclass = "generic-mix"
alsa.name = "ALC257 Analog"
alsa.id = "ALC257 Analog"
alsa.subdevice = "0"
alsa.subdevice_name = "subdevice #0"
alsa.device = "0"
alsa.card = "0"
alsa.card_name = "HDA Intel PCH"
alsa.long_card_name = "HDA Intel PCH at 0xb273c000 irq 147"
alsa.driver_name = "snd_hda_intel"
device.bus_path = "pci-0000:00:1f.3"
sysfs.path = "/devices/pci0000:00/0000:00:1f.3/sound/card0"
device.bus = "pci"
device.vendor.id = "8086"
device.vendor.name = "Intel Corporation"
device.product.id = "9dc8"
device.product.name = "Cannon Point-LP High Definition Audio Controller"
device.form_factor = "internal"
device.string = "front:0"
device.buffering.buffer_size = "352800"
device.buffering.fragment_size = "176400"
device.access_mode = "mmap+timer"
device.profile.name = "analog-stereo"
device.profile.denoscription = "Analog Stereo"
device.denoscription = "Built-in Audio Analog Stereo"
module-udev-detect.discovered = "1"
device.icon_name = "audio-card-pci"
Ports:
analog-output-speaker: Speakers (type: Speaker, priority: 10000, availability unknown)
analog-output-headphones: Headphones (type: Headphones, priority: 9900, not available)
Active Port: analog-output-speaker
Formats:
pcm

Sink #2
State: SUSPENDED
Name: bluez_sink.20_1B_88_1C_97_D7.a2dp_sink
Denoscription: Mi True Wireless EBs Basic 2
Driver: module-bluez5-device.c
Sample Specification: s16le 2ch 44100Hz
Channel Map: front-left,front-right
Owner Module: 24
Mute: no
Volume: front-left: 13099 / 20% / -41.95 dB, front-right: 13099 / 20% / -41.95 dB
balance 0.00
Base Volume: 65536 / 100% / 0.00 dB
Monitor Source: bluez_sink.20_1B_88_1C_97_D7.a2dp_sink.monitor
Latency: 0 usec, configured 0 usec
Flags: HARDWARE DECIBEL_VOLUME LATENCY
Properties:
bluetooth.protocol = "a2dp_sink"
device.denoscription = "Mi True Wireless EBs Basic 2"
device.string = "20:1B:88:1C:97:D7"
device.api = "bluez"
device.class = "sound"
device.bus = "bluetooth"
device.form_factor = "headphone"
bluez.path = "/org/bluez/hci0/dev_20_1B_88_1C_97_D7"
bluez.class = "0x240418"
bluez.alias = "Mi True Wireless EBs Basic 2"
device.icon_name = "audio-headphones-bluetooth"
Ports:
headphone-output: Headphone (type: Headphones, priority: 0, availability unknown)
Active Port: headphone-output
Formats:
pcm


I trying to extract volume of Sink #2

https://redd.it/yanv10
@r_bash
need help plz i want to learn :P



Im Dono ! And i learn bash on linux. im a beginer and i need help for something pretty basic for someone already know bash linux.

I write a simple noscript and i got a problem.

I'm trying to set a variable that detects when I say "games" to tell me "Good games!" and if I say anything else he replies "Good day!"

However, when I try to set the ca variable doesn't work. I leave you screenshots of what I have as an answer.

Btw I don't know how to copy/paste on linux. (I want to specify that I am on windows with ssh access to a Linux Linode in Ubuntu 22.10 version)

Ps** The noscript is in French because I am French speaking. I do my best to translate for you, so be indulgent please.

there is a imgur link to see what happen : https://imgur.com/a/SLVOZoL

Thanks you in advance :P

https://redd.it/yat1ss
@r_bash
Help with custom bash prompt in Termux

Hi guys, I came back to my Termux setup after a year and remembered I had modified the PS1 to use unique colors. I decided to continue down that idea and modify it further, including to have it always print the working directory, like other shells do, since I never have any idea which directory I'm in. I got it working but the only problem is it is only set once when my bashrc runs initially, so to get it to update after cding into a different directory, I need to source it again. Obviously, this is less than ideal. But, I have no idea how to get the behavior I want (immediate updating after cding)

Here is my bashrc https://pastebin.com/eYjWrc4B

Any help would be greatly appreciated. Thanks!

https://redd.it/yaqkd6
@r_bash
How to check if user didnt input any charecters?
https://redd.it/yb0r26
@r_bash
Setting a GSettings parameter as root fails

Hi there,

I'm trying gsettings set org.gnome.desktop.session idle-delay 0 to set the idle-delay parameter (blank screen e.g. during system updates) to zero from a post-install noscript run as root. But the command still fails with the following error message:

>(process:34462): dconf-WARNING **: 02:11:01.329: failed to commit changes to dconf: Failed to execute child process “dbus-launch” (No such file or directory)

After some research, I understand the problem is somehow related to DBUS failing to launch for the root user. And the advice is to either install dbus-x11 (which I don't want to do just for the sake of this operation) or precede the above command line with sudo -u root dbus-launch. But it doesn't help either, still with the same error message.

Any help is highly appreciated! Thank you!

https://redd.it/yb0io6
@r_bash