r_bash – Telegram
Store jquery value as variable in Bash noscript?

So I currently am attempting to create a bash noscript which stores the value of a jquery item and outputs it to a variable. What I am a bit confused about is how to do this. I am currently doing the following:


#!/bin/bash

#Initial Curl Request

GETREQUEST=$(curl --request GET \
--- url https://...... \
--- header '.......... \
--- header '.......... \

#Get value of json response

echo $GET
REQUEST | jq -r '.initial.value'

#Store Value as new variable
Value=$(jq -r '.initial.value')

echo $Value



Now the issue I believe here is when I echo $Value I get nothing because I believe that the new variable is not actually storing anything, am I trying to get this value incorrectly using the jquery?


Whereas with the echo of $GET_REQUEST I actually get the variable because it picks it out of the json response. Is there a way to maybe get that value and then store it as a variable then?


Let me know if any clarification is needed!

https://redd.it/w7l4xi
@r_bash
libby: a simple CLI tool to quickly download books from libgen (Library Genesis)

libby scrapes Library Genesis with curl and pup and gives you a list of downloads with fzf. I made this because I felt the alternatives were too slow. Feel free to add issues/feature requests/PRs!

The tool is available on the AUR as libby-git.

Demo

GitHub

Source

https://redd.it/w6rxrk
@r_bash
Why Does No One Use Inline Comments

In C, there are two ways of writing comments:

// Regular comment
/* Inline comment */


I always preferred the second type of comment because it allows you to make inline comments.
I know a lot of programmers who agree with this.
bash also has two ways of writing comments:

# Regular comment
`# Inline comment`


But I have never seen anyone ever use this.
I wonder why.
Is it not portable to every other shell?
I like to use it because it has a beginning and an end.
Are there any downsides to bash's inline comments?

https://redd.it/w8dy8d
@r_bash
Script with arguments with spaces in it.

Hello people...I have a situation that is driving me crazy and I can't solve it. I have a noscript with a function that takes 4 arguments (paths and names of a file) from a text file (I could do it with 3, but I got used to all 4).

It works great when there is no space in the file/folder name, but the moment the space appears, I think what it does is take the next word as a newline and unset everything.

This is the part of the noscript:

#!/bin/bash

driveplex="Plex:Plex/Peliculas"
drive
rtorrent="rtorrent:rtorrent"
localdostb=$"/home/user/2tb"

func () {
hora=$(date +'%T')
local
moviepath=$(cut -d '/' -f1-7 <<< "${1/\/data\/Plex/$localdostb\/archivos}")
plexmoviepath=$(cut -d '/' -f1-3 <<< "${1/\/data\/Plex\/Peliculas/$driveplex}")
file
name=$2
localsourcepath="${3/\/data/$localdostb}"
rtorrent
sourcepath="${3/\/data\/transmission/$drivertorrent}"
rtorrentsourcefolder="${4/\/data\/transmission/$drivertorrent}"
local
sourcefolder="${4/\/data/$localdostb}"
archivo="$localsourcefolder/$filename"

echo -e "localmoviepath: $localmoviepath
filename: $filename
localsourcepath: $localsourcepath
rtorrentsourcepath: $rtorrentsourcepath
rtorrentsourcefolder: $rtorrentsourcefolder
plexmoviepath: $plexmoviepath" >> /home/user/log.log
}


func "${1}" "${2}" "${3}" "${4}"

This is the input example:

/data/Plex/Peliculas/Divergent.(2014)/Divergent 2014 1080p BluRay DTS-HDMA 7.1 x264-RUXi.mkv
Divergent 2014 1080p BluRay DTS-HDMA 7.1 x264-RUXi.mkv
/data/transmission/radarr/Divergent 2014 1080p BluRay DTS-HDMA 7.1 x264-RUXi.mkv
/data/transmission/radarr

This is how i pass the file:

noscriptname $(<textfile)

This is what it returns:

localmoviepath: /home/user/2tb/archivos/Peliculas/Divergent.(2014)
filename: 2014
local
sourcepath: 1080p
rtorrent
sourcepath: 1080p
rtorrent
sourcefolder: BluRay
plex
moviepath: Plex:Plex/Peliculas/Divergent.(2014)

This is what it returns if there are no spaces:

local
moviepath: /home/user/2tb/archivos/Peliculas/Divergent.(2014)
file
name: Divergent.2014.1080p.BluRay.DTS-HDMA.7.1.x264-RUXi.mkv
localsourcepath: /home/user/2tb/transmission/radarr/Divergent.2014.1080p.BluRay.DTS-HDMA.7.1.x264-RUXi.mkv
rtorrentsourcepath: rtorrent:rtorrent/radarr/Divergent.2014.1080p.BluRay.DTS-HDMA.7.1.x264-RUXi.mkv
rtorrentsourcefolder: rtorrent:rtorrent/radarr
plexmoviepath: Plex:Plex/Peliculas/Divergent.(2014)

I've tried enclosing variables in quotes, in curly braces, tried escaping spaces with backslashes, even tried passing quoted arguments from the file directly, but still get the same behavior.

If you could give me a guide, I would really appreciate it.

Thanks!

https://redd.it/wapmlk
@r_bash
Better completion for bash?

Using zsh I like mostly the interactive auto-complete features: In zsh basically an interactive list of completions is shown, that can be navigated with the cursor keys or TAB, but first it completes to the longest beginning-of-a-completion, that matches the current input. This is basically a powerful combination of the default completion and bind 'TAB:menu-complete'.

&#x200B;

Path completion in zsh

Is it possible to obtain the same behavior for bash?

https://redd.it/wb37jw
@r_bash
Bash noscript to run a Python program in background

Hello,

I'm trying to write a bash wrapper noscript (in Linux) to run a python program in background, allowing the user to logout keeping the noscript running. I'm trying to do it with `nohup`. I'm also trying to support a proper `'` escape to support space in the arguments (see below).

My goal is that the bash noscript works like a bash command, passing to the python code all the command line arguments entered by the user.

Here is how I run the python code, without going to background:

$ python3 /noscripts/pyprog.py --arg1='argument 1','argument 2','argument 3' --arg2='argument 4','argument 5'

I did something like:

#!/bin/bash

NOHUP=$(which nohup)

ARGS=($*)

exec "${NOHUP} python3 /noscripts/pyprog.py ${ARGS[@]} &"

However, bash tells it cannot run that `exec` line. I'm clearly missing some point :-) I would very much appreciate any help.

Thanks!

https://redd.it/wdh3xk
@r_bash
Get the keyboard layout in bash

I started doing some noscripting, but I need to get the current keyboard layout. I use 3 keyboards: Russian, Portuguese and English (Colemak). I already have them mapped to alt space, but I can't seem to get the current layout. I am using DWM as my window manager. Void is my distro.

https://redd.it/wf4hty
@r_bash
Determine if the term/console supports UTF8?

I'm trying to automatically determine if a terminal supports UTF8 is order to pick the appropriate characters (i.e. using ASCII instead of UTF8).

The UTF8 increases readability, but on terms that don't support it it causes a mess and harms readability.

Right now I have a switch for picking utf8 or ascii, but I'd really like it to be automatic.

"Solutions" I've found that haven't helped:

- Check the locale command or the $LC_.... variables.
- This just shows environment variables and has nothing to do with terminal ability.
- Just use a different terminal
- The entire point is to have this work on different terminals / consoles.
- print something to the term and look at it.
- That's not automated.
- Testing echo supports utf8.
- Only testsecho and bash internals, not the terminal.

Hackishly works:

- Check $TERM
- If the $TERM is linux (local console) it uses ascii, else utf8.
- Does not explicitly list utf8 support.
- Does not take into account a variety of other terms than don't support utf8.

If there's a better way I'd love to know it. Otherwise it'll be just adding TERMs to ascii as I run across them.

Thanks,
Whale

https://redd.it/wfbf3w
@r_bash
What does this bit of code mean?

log () {
echo -e "$(basename $0) [$$]: $*" >&2 || :
}

This part: >&2 || :

https://redd.it/wfky2q
@r_bash
Can someone translate this line of code from BASH to Windows CMD

I need this line of code to run on Windows CMD (or powershell), preferably one line if possible. If anyone can help me please

LC_ALL=C </dev/urandom tr -dc 'A-Za-z0-9!"#$%&()*+,-./:;<=>?@[\]^_`{|}~' | head -c 50 > secret.txt


https://redd.it/wg2xyp
@r_bash
How to unify parsing arguments --key=value -key=value -k=value -k value in bash

Hi guys, i'm developing an toolkit for linux engineers and have some issues with auto completions due all my parsing logic depend on one string argument parsing and should contain "=" after key, i want to unify it such way it parse value after -k with space delimeter(or without delimeter at all) and also $ARG1 should not declaring if there "-k some_value" in arguments is there some simple and common way to achieve that?

this is my current implementation which works only -one_string=argument

cat > /tmp/testnoscript.sh << 'EOL'
#!/bin/bash
#Filter arguments by regex for security reasons
CLDOPTS="$(expr "$(tr '\n' ' ' <<< "${@}")" : '\(A-Za-z0-9/ :.,@_=+^*-\+\)' 2>/dev/null | tr ' ' '\n')"

#Simple parsing arguments
for i in ${CLDOPTS}
do
case $i in
-k=|-key=|--key=) KEY="${i#=}" ;;
-f=|-foo=|--foo=) BAR="${i#=}" ;;
-j|-json|--json) JSON=1 ;;
-) ;;
) let ii++; declare ARG$ii=${i} ;;
esac
done

#Print variables
echo key: $KEY
echo foo: $BAR
echo json: $JSON
echo ARG1: $ARG1
EOL
chmod 755 /tmp/testnoscript.sh

how it works:

$ /tmp/testnoscript.sh -k=testkey -f=bar -j arg1
key: testkey
foo: bar
json: 1
ARG1: arg
1

how i want it work

$ /tmp/testnoscript.sh -f=bar -k testkey -j arg1
key: testkey
foo: bar
json: 1
ARG1: arg
1

any ideas how to refactor argument parsing but save the current use cases logic? Thank you!

https://redd.it/whvgpt
@r_bash
How can I create window-like GUIs without Gtk? Like raspi-config. I mean, this isn't all echos and colors, right?
https://redd.it/wjclpb
@r_bash
making folders from read var with spaces in them

Hi, I have this snippet, and I can't figure out how to avoid creating several folders when the input i give has spaces in it.

Ie. I type in "house one on housestreet" and I get four folders rather than just one.

I guess that the var created is probably just a string and thusly is treated as such, getting split and all but I've tried to "isolate" in various ways putting (),{},'' around the var in all kinds of crazy ways, but no such luck...

How do I make the bloody thing understand that I just want a single folder with spaces in it's name? :)

#!/bin/bash
echo 'enter folder name:'
read -e thisnewfolder
mkdir -vp $thisnewfolder

https://redd.it/wjjprd
@r_bash
Whats the easiest way to pull this idea off ? sed ? is it even possible ?
https://redd.it/wlr9m6
@r_bash
splitting a string that contains single and double quoted strings without eval, possible?

I don't want to used eval to split a string as it may have errors in it and I don't want it to be run. The code below exemplifies what I want.


Any idea? other pitfalls I should think about?


direct=("abc" 'single ss quoted' "cd - e" not_quoted) ;

declare -p direct

gives: declare -a direct=([0\]="abc" [1\]="single ss quoted" [2\]="cd - e" [3\]="not_quoted")

&#x200B;

s='"abc" '"'"'single ss quoted'"'"' "cd - e" not_quoted' ;

test_1=($s)

declare -p test_1

gives erroneous : declare -a test_1=([0\]="\\"abc\\"" [1\]="'single" [2\]="ss" [3\]="quoted'" [4\]="\\"cd" [5\]="-" [6\]="e\\"" [7\]="not_quoted")

eval "test_2=($s)"

declare -p test_2

gives: declare -a test_2=([0\]="abc" [1\]="single ss quoted" [2\]="cd - e" [3\]="not_quoted")

https://redd.it/wn83rz
@r_bash
bash help: open the installed text editor

Hi, fairly new to bash here. need to open a plaintext file depending upon the installed editor.

if gedit is installed - open gedit, otherwise open the available editor, e.g. xed

which of the following would be the best practice (for the .bash_aliases file)

if -f /usr/bin/xed ; then
alias gedit="xed"
fi

or

if command -v xed &> /dev/null
then
alias gedit="xed"
fi

or better- open among the first available editor ("gedit" or "xed" or leafpad ..)

any room for improvements ? thanks (System is Linux mint cinnamon)

https://redd.it/wo7jr3
@r_bash
output hex data from xattr to create icns file

I'm trying to extract the icon from an xattr of a file. Using xattr to get the "com.apple.ResourceFork" in hex format i use:

xattr -px com.apple.ResourceFork file

and save the output to a variable

var="$(xattr -px com.apple.ResourceFork file)"

then using variable expansion i remove the first bytes until i reach 69 (icns magic number is 69 63 6E 73)

var=${var#*69 63 6E 73}

next i output the variable and append "69 63 6E 73" to the beginning to restore the magic number.

echo "69 63 6E 73$var" > output.txt

if i take the hex data from the output.txt and insert it into a hexeditor to save it then it works and the .icns is created.

i want to do this programmatically in bash/zsh without having to save it manually.

tried using

touch icon.icns

to create an empty file then

echo "69 63 6E 73$var" > icon.icns

just transforms the output file into an ASCII file.

i'm not stuck to my method, any working method is acceptable to me.

&#x200B;

Edit: ive posted the same question on [stackoverflow](https://stackoverflow.com/questions/73354927/output-hex-data-from-xattr-to-create-icns-file) and including the link here for future users.

https://redd.it/wogoce
@r_bash
Running Script As Other User - Inside Script

I have a bash noscript that I wrote that is designed to be run as a user, but that will be executed as root (on a Mac via MDM). Running this noscript using sudo -i -u user bash --rcfile /Folder/noscript.sh works and runs all aspects of the noscript as the user. But my preference would be to not actually run it like that but to have everything, including switching to the user, within the noscript. I've done some searching and found instructions on the opposite problem, being a user and trying to run a noscript as root, but not my issue of being root and wanting to run as a user. Ideally, I would detect first if a user is logged in (using $( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )) and then run the noscript as that logged in user. I played around with things like sudo -i -u user without success. Hoping that someone here might have a better idea. Thank you!

https://redd.it/wqxoxe
@r_bash
I want real world examples of what do you actually use Bash for.

Hi, it seems like everyone is recommending me to learn Bash but no one really explain me why.

When I asked them what do you use it for, they said "everything" and that's it.

&#x200B;

Could you guys give me 1-2 examples of what do you use it for, in your daily life.

How does it make you life easier?

&#x200B;

P.S. Sorry for my English.

https://redd.it/wrb25x
@r_bash