r_bash – Telegram
This Is My Most Complex Bash Script To Date

I made an Arch install noscript fully in Bash that sets up my system and my dot files with only three simple commands.

The whole thing is hosted on gitlab at https://gitlab.com/mclartydan0505/savageos and the idea is that now anywhere in the world I can get my full system setup from the command line to my custom desktop in minutes.

I know this is kind of moot as an Arch install noscript considering the one that comes by default is pretty good, but this noscript also downloads my Awesome config and all the apps I like with all the configs I like.

It is rather minimal, only installing about 600 packages by default. I am working on adding one file section the chroot to allow the user to add any additional apps they want to install, it is easy to do I just need to find the time to write it and commit it.

​

If you would like to run it in a vm or on hardware I could take the input for any errors, I did a sanity check in a QEMU VM with UEFI and it worked as planned, but I would like to know how hardware responds.

The hardest part is that the archlinux-keyring always seams to be breaking in the live iso, but I think I found a setup that can get the keyring to work 99% of the time so the noscript can download git and run the pacstrap.

https://redd.it/17g6hmw
@r_bash
Sticky shift key or one time caps lock

Hello, I'm trying to modernize a bit the input methods in my computer since we've (or at least I) been suffering for decades the same falencies.

What I want is a method for activating caps lock until a letter is entered and then deactivate it.

**O**btaining this result, without the need to press any combination of keys.

But I don't want sticky shift all the time I just want to be able to choose betwwen different methods.

***E***ven a different implentation of the same code, could work... ***L***ike this. ***I*** mean turning on and off capslock automatically each time that the '. ' sequence is entered.

I wrote a bash noscript to do so that works well in the terminal but when associated with a keyboard shortcut gets totally lost. I think that read has no idea where it is, here is my noscript:

!/bin/bash
echo $caps_state
while true; do
read -rsn1 input
for letter in {A..Z} {a..z}; do
echo $input = $letter
if [[ "$input" == $letter ]]; then
caps_state=$(xset -q|grep Caps|cut -d: -f3|tr -d ' ')
echo $caps_state
if ! [[ $caps_state == off01 ]]; then
echo caps off
xdotool key Caps_Lock
exit
fi
fi

done
done

What keylogger should I use?

https://redd.it/17ge8a1
@r_bash
Sourcing dynamic color in PS1 from noscript (details in comments)
https://redd.it/17gfd86
@r_bash
at plays command immediatley

any one shed any light as to why bash plays the alarm sound immediately rather than waiting for the specified time ?

​

\#!/bin/bash

​

​

strHour=$(zenity --forms --noscript "simple alarm clock" --text "Combo Hoour" --add-combo "Select Hour." --combo-values "00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24") && echo $strHour

​

​

​

strMinute=$(zenity --forms --noscript "simple alarm clock" --text "Combo Hoour" --add-combo "Select Minute." --combo-values "00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59") && echo $strMinute

if zenity --question --text="Set Alarm for $strHour:$strMinute ?"; then

zenity --info --text="You pressed \\"Yes\\"!"

ffplay /home/dunryc/Wip/alarmclock/alarm.wav | at $strHour:$strMinute

else

zenity --info --text="You pressed \\"No\\"!"

fi

​

​

https://redd.it/17gjexs
@r_bash
Question about à Regular Expression

Hi,

I got a question regarding Regular expressions on Bash.
Currently doing the LPI exams, and one of the question is:

- What extented regular expression would match any mail address, like info@example.org ?

So came out with the following regex:

^[[:alnum:]]{1,}@([[:alnum:]]|-|\.){1,}\.[[:lower:]]{2,6}$


- Starts with alphanumeric char (at least 1)

- Followed by @

- Followed by at least one alphanumeric char or an hyphen or a dot

- Ending with 2 to 6 lower cases to match all kinds of domains (.xyz,.de,.com,.org,.museum and more)

In the given solution, here's the regex used:
"^\S+@+\S+\.\S+"


Both regex works, but just by curisoty, what is the most efficient one ? the simplest or the more complex ?

As far as I understood
 
is used to match any non-whitespace character but does it not includes specials chararacters such as question mark, exclamation mark and so on ? that are forbidden in a mail address ?

https://redd.it/17gv8zy
@r_bash
Learning more

I know the basic stuff (variables, if else, read,echo,etc) how would I learn more and where can I?

https://redd.it/17gxcb2
@r_bash
Copy and rename file from shell noscript

This is a pretty basic question but I’ve been struggling getting this working for some reason. I am trying to copy a file from one directory to another and renaming it along with the copy. This is being done inside of a shell noscript, and I have a variable called $filename that stores the NEW file name. Here is the code snippet:

filename="IRcamfpgacksm${checksum}ver${version}.pdb"
#filename="Thisisafile.txt"
echo "filename: ${filename}"
cp ./par/ircamfpga/designer/impl1/*.pdb output/pl/$filename


The output of the echo command on the console is:

.pdbname: IRcam
fpgacksmA415ver0x0081

 
But the file that gets copied to the new directory does not have the correct name. When I use the version of $filename that is commented out, it works perfectly fine.

https://redd.it/17gylgx
@r_bash
cURL: Need to make host server think I am a browser and not cURL.

I found a website that posts stats every month that are useful to my business. They post them for free.

The link to download a csv file, which is the format I need, looks like an API call:

https://gs.statcounter.com/os-market-share/tablet/chart.php?device=Tablet&devicehidden=tablet&statTypehidden=oscombined&regionhidden=ZA&granularity=monthly&statType=Operating%20System&region=South%20Africa&fromInt=202209&toInt=202309&fromMonthYear=2022-09&toMonthYear=2023-09&csv=1

The problem I have, is if I paste that link in any browser, I get a CSV download. If I access it with wget or curl, I get a bit of useless XML data.

I suspect they are detecting client type to stop people doing this.

I simply want to write a noscript that pulls down certain datasets, then processes that so I can store the final data in a specific folder on my Nextcloud server. I want to use it for internal use (decision-making), but I want the data to be updated each month automatically, rather than me sit and manually download it each month.

I know cURL is super powerful and flexible, so can someone explain to me how I would get cURL to tell the host server that it is Firefox or Chrome or whatever?


Edit:


The problem I had was caused by a really stupid but easy to make mistake.


I ran the following:


curl https://gs.statcounter.com/os-market-share/tablet/chart.php?device=Tablet&devicehidden=tablet&statTypehidden=oscombined&regionhidden=ZA&granularity=monthly&statType=Operating%20System&region=South%20Africa&fromInt=202209&toInt=202309&fromMonthYear=2022-09&toMonthYear=2023-09&csv=1

That output the following:

1 11976
2 11977
3 11978
4 11979
5 11980
6 11981
7 11982
8 11983
9 11984
10 11985
11 11986
2 Done devicehidden=tablet
[3] Done statType
hidden=oscombined
[4] Done region
hidden=ZA
5 Done granularity=monthly
6 Done statType=Operating%20System
7 Done region=South%20Africa
8 Done fromInt=202209
9 Done toInt=202309
10- Done fromMonthYear=2022-09
<chart caption='StatCounter Global Stats' subCaption="Top 5 Desktop Browsers in from - , 1 Jan 1970" anchorAlpha='100' showValues='0' bgColor='FFFFFF' showalternatevgridcolor='0' showalternatehgridcolor='0' bgAlpha='0,0' numberSuffix='%' canvasBorderAlpha='50' bgImage='https://www.statcounter.com/images/logogschartfadedpadded.png' bgImageDisplayMode='fit' canvasBgAlpha='0'
exportEnabled='1' exportAtClient='0' exportAction='download' exportFormats='PNG' exportHandler='https://gs.statcounter.com/export/index.php' exportFileName='StatCounter-browser--all--'
legendBorderAlpha='0' legendBgColor='000000' legendBgAlpha='0' legendPosition='RIGHT' legendShadow='0'
canvasBorderThickness='1' canvasPadding='0' showBorder='0' labelDisplay='Rotate' slantLabels='1'><categories></categories><styles>
<definition>
<style name='myCaptionFont' type='font' size='14' bold='1' isHTML='1' topMargin='14' />
</definition>
<application>
<apply toObject='Caption' styles='myCaptionFont' />
</application>
<definition>
<style name='myLegendFont' type='font' size='11' color='000000' bold='0' isHTML='1' />
</definition>
<application>
<apply toObject='Legend' styles='myLegendFont' />
</application>
<definition>
<style name='myHTMLFont' type='font' isHTML='1' />
</definition>
<application>
<apply toObject='TOOLTIP' styles='myHTMLFont' />
</application>
</styles>
</chart>


I forgot to put quotes around the url.


I do this:


curl
"https://gs.statcounter.com/os-market-share/tablet/chart.php?device=Tablet&devicehidden=tablet&statTypehidden=oscombined&regionhidden=ZA&granularity=monthly&statType=Operating%20System&region=South%20Africa&fromInt=202209&toInt=202309&fromMonthYear=2022-09&toMonthYear=2023-09&csv=1"

and then I get this:


"Date","Android","iOS","Unknown","Windows","Linux","Other"
2022-09,61.01,38.46,0.33,0.18,0.01,0
2022-10,59.53,40.21,0.15,0.09,0.02,0.01
2022-11,60.19,39.64,0.1,0.06,0.01,0
2022-12,59.12,40.73,0.1,0.04,0.01,0
2023-01,56.26,43.52,0.16,0.05,0.01,0
2023-02,57.23,42.55,0.12,0.08,0.01,0
2023-03,58.79,41.02,0.16,0,0.02,0
2023-04,58.72,40.99,0.28,0,0.02,0
2023-05,56.79,42.68,0.48,0,0.04,0
2023-06,60.21,39.1,0.67,0,0.02,0
2023-07,60.21,39.07,0.62,0,0.09,0
2023-08,60.1,39.14,0.72,0,0.03,0
2023-09,59.13,39.94,0.9,0,0.03,0.01


The lesson here is always use quotes. Make it a habit, or special characters will make things frustrating...

https://redd.it/17h2xpp
@r_bash
Flushing the read buffer, or something similar

Howdy, I've a tiny noscript which i've just updated to use inotifywait to block until a file is opened or closed, replacing a dumb periodic poll. Nice, like that. But the file is opened and closed a few times when it does, meaning when the access happens, the loop is executed (differently) a few times in a second or so. Is there any way I can purge the pending reads, and instead wait for a few seconds to let things settle and then see what's what?

inotifywait /dev/video? -e open,close -m | while read LINE
do
sleep 2 # wait for steady state
do_something_to_clear_any_backlog
lsof /dev/video? 2>&1 | grep -q zoom \
&& echo -e "\xA0\x01\x01\xA2" \
|| echo -e "\xA0\x01\x00\xA1"
done > /dev/ttyUSB1

TBH I guess I could just NOT use the -m and restart inotifywait each loop, but I'm mostly curious to know if there's a good solution this way.

\[BTW, as I think it's funny, this is a noscript that "puts on a red light" in another room when I'm on a zoom video call to stop my kids bugging me when I can't reply... so I called it "[roxanne.sh](https://roxanne.sh)" :D \]

https://redd.it/17hipg3
@r_bash
is there any way I can only parse a single command line argument without bothering with others

I have a command that I want to wrap, it doesn't take an argument for output file but I want to wrap it in a function to achieve that, I'm trying to use `getopt` and `getopts` and both require that I know all the arguments I'm going to give to the command beforehand. for example: I have a function like this

wrapper_command () {
echo $(getopt "o:")
while getopts "o:" opt; do
case "${opt}" in
o) output=$OPTARG;;
esac
done

my_command $* > $output
}


then I invoke the command using `wrapper_command <args> -o outputfile` but `getopts` returns `--` for the output and `getopts` complains when the other arguments are not included in the optstring.

https://redd.it/17ht40g
@r_bash
Why is my terminal not redrawing the screen when I reach two lines of input?

https://imgur.com/a/hIsrUSy

Kitty is shown in the GIFs, but it just mirrors the output from bash. Here is entirety of my .bashrc configuration:

export PS1="\e[34m\]\e[32m\]\\u\e[31m\] \-> \e[33m\]\\W \e[37m\]$ "

Commenting out the entire line doesn't fix the issue. Entering and exiting full-screen will properly refresh bash.

https://redd.it/17i3asu
@r_bash
shell noscript to control spotify

https://github.com/umtksa/spoty
I'm using my old computer to run daily apps over the corner while I'm working like playing music from spotify, watching a folder to print every pdf file created inside a dropbox folder
this is the noscript I use over ssh to make my life easier


with this noscript I can play a spesific playlist
play/pause music
play next song
control volume with an integer


https://redd.it/17igwap
@r_bash
Daily note noscript

Under the ~/Desktop/Journal/ I have my journal pages, e.g 28-10-23.

I tried to write a noscript that would compare the current date with the file name under that directory.

If there's no file with the current date, it would create it, if there is, then it would sleep until there isn't.

The noscript goes like this:

d=$(date "+%d-%m-%y")
f=$(ls $HOME/Desktop/Journal | grep $d)
while true;
do
while [ "$d" == "$f" ];
do
sleep 1
done
touch $HOME/Desktop/Journal/$d
done

If the file is already there and I run the noscript, then it just sleeps, but if it isn't, then it creates it as well as doesn't go into the nested while loop for sleep, meaning it keeps creating it.

I don't understand why it doesn't go into the nested while loop for sleep when the file is created, but doesn't created it while in the nested while loop sleeping.

https://redd.it/17iltpo
@r_bash
Trouble making shortcut for CUDA_VISIBLE_DEVICES=

Hi, I got tired of typing `CUDA_VISIBLE_DEVICES=` so in my .bashrc, I put: `export cuda="CUDA_VISIBLE_DEVICES"`. but it keeps giving me the error below for some reason. why is this so? i thought creating a variable would work.

$ time $cuda=3 python inference.py
-bash: CUDA_VISIBLE_DEVICES=3: command not found

chatGPT suggested:

function set_cuda() {
export CUDA_VISIBLE_DEVICES=$1
}

which i guess works, but aesthetically, something close to `cuda=3` would be rly nice haha

&#x200B;

edit: welp I tried `set_cuda 3` and:

Traceback (most recent call last):
File "/netmnt/vast01/cbb01/lulab/andrew/similar_articles/baselines/inference.py", line 147, in <module>
run(args)
File "/netmnt/vast01/cbb01/lulab/andrew/similar_articles/baselines/inference.py", line 83, in run
model.to(args.device)
File "/netmnt/vast01/cbb01/lulab/andrew/modern/lib/python3.9/site-packages/transformers/modeling_utils.py", line 2014, in to
return super().to(*args, **kwargs)
File "/netmnt/vast01/cbb01/lulab/andrew/modern/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1145, in to
return self._apply(convert)
File "/netmnt/vast01/cbb01/lulab/andrew/modern/lib/python3.9/site-packages/torch/nn/modules/module.py", line 797, in _apply
module._apply(fn)
File "/netmnt/vast01/cbb01/lulab/andrew/modern/lib/python3.9/site-packages/torch/nn/modules/module.py", line 797, in _apply
module._apply(fn)
File "/netmnt/vast01/cbb01/lulab/andrew/modern/lib/python3.9/site-packages/torch/nn/modules/module.py", line 797, in _apply
module._apply(fn)
File "/netmnt/vast01/cbb01/lulab/andrew/modern/lib/python3.9/site-packages/torch/nn/modules/module.py", line 820, in _apply
param_applied = fn(param)
File "/netmnt/vast01/cbb01/lulab/andrew/modern/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1143, in convert
return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)
RuntimeError: CUDA error: out of memory
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.

&#x200B;

https://redd.it/17irid3
@r_bash
Can someone help me with a task?

I am using Kali VM and i need to write a BASH noscript that will scan my VMware NAT network via ARP, collect those “live” IPs, place the IPs into a file, then run the following scans based on those “Live” IPs.

https://redd.it/17iwctu
@r_bash
Need help - (List the folders that contain the specified file and select from the list and move)

Hi,

I'm new to bash noscripting, just starting to automate my linux journey. :)

I've been trying to find solutions to this, without any luck.

&#x200B;

What the noscript should do:

\- cd to the main folder (Application)

\- Find which profile folders contain a specific file (File_X) and list them

\- The selection is made with a number, after which the noscript will move to that folder.

&#x200B;

File structure:

/Application/profile 1/File_X

/Application/profile 2/

/Application/profile 3/File_X

/Application/profile 4/File_X

&#x200B;

"These profile folders contain the necessary file you want to update:"

1. profile 1
2. profile 3
3. profile 4

"Give number to select" -> 2

\-> move to that selected folder -> profile 3

&#x200B;

I hope my explanation is clear on some level. If you have a solution to this, I'd love to see it in the answers. If possible, could you also explain a bit what the different commands do. Like I said, I don't have much experience. However, I don't just want to copy/paste finished code, I also want to learn at the same time.

&#x200B;

https://redd.it/17iyx9p
@r_bash
Split string to an array with IFS by linebreak

Hello,

I am struggling with a simple task. I'm coming from python and decided to write a noscript with bash. For now I regret this, but try to accomplish it to learn. With python I would have finish this hours ago.

Here is the problem: I want to covert to following string to an iteratable array:
[ "Hello, hello1", "World", "How", "Are", "You" ]


The result array should contain:
"Hello, hello1", "World", "How", "Are", "You"

Note, I cannot split by comma, since the elements itself can contain commas.
I tried:
the_string=$(echo $the_string | tr -d '[' | tr -d ']' | sed 's/\", \"/"\\n"/g')


When I echo the output, I see:
"Hello, hello1"
"World"
"How"
"Are"
"You"


But if I use
IFS=$'\n' read -r -a string_array <<< "$the_string"

it just doesn't work whatever I try. I only get a single elements back in $stringarray. The newlines are somehow ignored.

Any ideas?

Many thanks in advance!

https://redd.it/17izwtn
@r_bash
how do i fix command nit found error after running noscript
https://redd.it/17jvmld
@r_bash
Create an If/Else Statement based on user input

I'm trying to add a portion to a noscript I'm making to just verify if something is installed, in this case I just want to verify if Flatpak is already installed on the computer, and if not, give the user the option to install Flatpak if they want to or not.

This is what I have so far but I'm unsure what command to use for user input. I did some Googling and found that the read command might work, but when I look up the man page on it on my machine, it looks different than what i'm seeing on sites like stackoverflow or AskUbuntu:

dpkg -s "flatpak" &> /dev/null

if $? -ne 0
then
echo "Flatpak is not currently installed on your system."
echo "Would you like to install Flatpak? (y,n): "
NOT SURE WHAT TO PUT HERE
else
echo "Flatpak is installed. Starting download shortly. . ."
fi

https://redd.it/17k1yg2
@r_bash
JQ - filter datasets in array based on index value within each dataset of array.

I have a very large JSON file.

Inside that JSON file I am only interested in the data that exists with a certain value in one of the indexes within that array.

I am trying to figure out how to use JQ to export the complete datasets where object_type="deckCard" in each dataset of the array.

Example output desired:


{
"id": "651",
"parent_id": "0",
"topmost_parent_id": "0",
"children_count": "0",
"actor_type": "users",
"actor_id": "alec",
"message": "Please advise on whether I may need to edit down these bios.",
"verb": "comment",
"creation_timestamp": "2023-10-11 12:52:56",
"latest_child_timestamp": null,
"object_type": "deckCard",
"object_id": "77",
"reference_id": null,
"reactions": null,
"expire_date": null
},
{
"id": "652",
"parent_id": "0",
"topmost_parent_id": "0",
"children_count": "0",
"actor_type": "users",
"actor_id": "alec",
"message": "There images have been attached to this card.",
"verb": "comment",
"creation_timestamp": "2023-10-11 12:53:15",
"latest_child_timestamp": null,
"object_type": "deckCard",
"object_id": "77",
"reference_id": null,
"reactions": null,
"expire_date": null
}


&#x200B;

https://redd.it/17kf8mk
@r_bash