r_bash – Telegram
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
Need Help with Removing Files Using Bash Script

if [[ $# -eq 0 ]]; then # if user doesn't enter anything, default to pwd

dir=$(pwd)

elif [[ -d "$1" ]]; then #if the user enters a directory that exists assign to dir

dir=$1

else

echo "usage: bash_file.sh <directory>" #otherwise, tell the user to enter a valid directory

exit 1 # exit program

fi

&#x200B;

for x in "$dir"/*; do #goes through and finds the files of size 0

if [[ -s "$x" ]]; then

rm "$x"

fi

done

For some reason, this removes the bash_file when I run bash_file itself.

I'm trying to remove files in a directory that have a size of zero. I'm not sure why it doesn't work properly.

https://redd.it/17kagkx
@r_bash
Minimalistic Bash noscript to fuzzy search and open chrome bookmarks from cli

Hello bash fellows,

I was a bit annoyed by reopening my frequently used bookmarks in Chrome. I added an extension called Quickmarks but don't like it that much. I want to be faster. And I want to have fuzzy search.

Hence, I decided to create a noscript that fetches all my bookmarks from the file system and opens a fuzzy search session on the bookmark noscripts. On selection, it opens the related url in Chrome.


https://preview.redd.it/27veeg13pixb1.png?width=2002&format=png&auto=webp&s=01c3597ecbb7e8a981fae8723c44c9250fc2a600

It works pretty well. What do you think? What is your workaround for this?

Link to the noscript 👉 https://github.com/fynnfluegge/mydotfiles/blob/main/.noscripts/fzfbookmarks.sh

https://redd.it/17ki4lp
@r_bash
Custom waybar module for controlling gamma

Howdy folks! I'm very new to bash noscripting and just wrote a couple noscripts to control the temperature of my laptop's display from my bar. I wanted to put this somewhere in case it was useful to someone or there was a better way to do it. Right now there are a couple values that I want to be stored between instances of the noscripts and I'm doing it in files. I wonder if there's a better way to do this that doesn't require the full absolute path of the files? Like if I can refer to the noscript's location in the file system?

Sorry if this question is super noobish, even a project this simple that's just a wrapper around someone else's code wasn't easy for me! So I appreciate any help I can get.

https://github.com/frolickingpotato/waybar-gammastep/blob/main/README.md

https://redd.it/17kqxsy
@r_bash
Help creating shell noscript for removing embedded subnoscripts from video files

I have a series of video lessons and part of the software leverages an AI assistant to automatically create and embed subnoscripts to the spoken parts. These subnoscripts are not captured and translated perfectly, so I'd like to be able to proofread and update these text files manually.

I have a file (queue.txt) where I list the full path to the video file, such as this:

/Volumes/Lessons/101/lesson1.mkv
/Volumes/Lessons/101/lesson2.mkv
/Volumes/Lessons/101/lesson3.mkv

I'd like to be able to run a shell noscript which takes this list of files and runs the following command against it:

mkvmerge -o <outputfilename> -S <inputfilepath>

The hope is that it outputs the following files in my current directory like this:

lesson1.mkv
lesson2.mkv
lesson3.mkv

Thanks so much in advance. This is not my area of expertise but I have a very large number of video lessons that I'm trying to make available for these students and ensure that subnoscripts are available for accessibility reasons.

https://redd.it/17krvyj
@r_bash
Sed noscripting help

I want to modify paths explicitly written in files to remove one case of ../

For example:

Somedir/../../hw
Somedir/../../../sw

now becomes after using the sed

Somedir/../hw
Somedir/../../sw

I know for certain the ../ will be in between a known directory name (Somedir in this ex) and a folder with some name (hw and sw in this ex).

Please help anyone with this.

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