r_bash | Unsorted

Telegram-канал r_bash - r_bash

46

Credits: @r_channels & @reddit2telegram

Subscribe to a channel

r_bash

Running a task paralel with xargs or gnu paralel?

So i have this oneliner script:

https://github.com/moeC137/wiktionary-curl-wordtype-checker/blob/main/sorter.sh

#!/bin/bash/ while read -ra line;do for word in "${line@}"; do sh checker.sh -l German -w $word; done; done < uniquewords.txt

unique
words.txt is a list with about 500 words.
checker.sh has to do a curl and some sorting for each word.

right now this takes a few min to finish.
But i like to speed the whole thing up a bit by running it 5x paralel.

i tryed 'xargs -I % -P 8' but i dont get the syntax right.
same goes for gnu paralel.

i assume this isnt actually complicated for someone who knows bash a bit...sadly i am quite a noob :/

https://redd.it/ksmhyp
@r_bash

Читать полностью…

r_bash

Save a nested command to an alias

Hey guys, i need to save a simple oneliner to take a screenshot and pipe it into xclip

Here's the working example, you can test it for youself (needs scrot and xclip):

scrot '/tmp/%F\_%T\_$wx$h.png' -e 'xclip -selection clipboard -target image/png -i $f'

If you try to alias it to a variable it won't work anymore, the code doesn't exit out.

How to alias it to properly execute this snippet from a alias?

https://redd.it/ksewb4
@r_bash

Читать полностью…

r_bash

Updating git bash for windows broke global settings

Hello guys,

I have recently updated my git bash version to the latest one, and it seems to have reset the root directory to the user's home directory, which in turn broke everything else, since my .gitconfig file was in another directory. To make things worse, I can't even edit and save C:\\Program Files\\Git\\etc\\profile to set a new HOME path, because it gives me a "permission denied" error, even though I'm the only user and an administrator with full rights and I never had this problem prior to the update. Can someone help?

1) Why does updating git bash reset my settings like this? Shouldn't it keep everything as it was before?

2) How can I edit my profile file again to solve this annoying issue? I don't want to have to change my HOME environment variable and affect everything else in my PC just because of this.

Thanks in advance!

https://redd.it/ks9v63
@r_bash

Читать полностью…

r_bash

I'm trying to make a super simple Bash script to remove specified symlinks and re-link them. Any advice?

Hi, I am trying to make a super super simple Bash script that removes all specified symlinks and then re-links it all (Sorta like an update) for my dotfiles directory.

I've never really used Bash before, and I would like some guidance as to what I should do. This is what I currently have:

# This is a setup script for LiterallyJohnny's dotfiles repo.
# I will not take responsibility for any damage done to your computer when you run this script. Use it at your own risk.

# TODO: Finish the script.

# Removes existing config files, so that there are no conflicts.
rm -rf {
~/.bash_profile
~/.bashrc
~/.profile
~/.stignore
~/.config/dunst
~/.config/i3
~/.config/i3status
~/.config/kitty
~/.config/nvim
~/.config/picom
~/.config/xorg
}

# Symlinks the files to their correct places
ln -sf ~/dotfiles/.bash_profile ~/
ln -sf ~/dotfiles/.bashrc ~/
ln -sf ~/dotfiles/.profile ~/
ln -sf ~/dotfiles/.stignore ~/
ln -sf ~/dotfiles/dunst ~/.config
ln -sf ~/dotfiles/i3 ~/.config
ln -sf ~/dotfiles/i3status ~/.config
ln -sf ~/dotfiles/kitty ~/.config
ln -sf ~/dotfiles/nvim ~/.config
ln -sf ~/dotfiles/picom ~/.config
ln -sf ~/dotfiles/xorg ~/.config


It's probably super bad, but as I've stated, I know almost nothing about Bash. Is this good enough for what I am trying to do?

https://redd.it/kryxfv
@r_bash

Читать полностью…

r_bash

How can I run each bash script in each subdirectory of the current directory?

Let's say I'm in a certain folder. This folder contains several subfolders that may have one or more bash scripts in them. I would like to run every single one of them. What is the best way to do this?

https://redd.it/krrty9
@r_bash

Читать полностью…

r_bash

echo $LSCOLORS in tty2 is empty

I downloaded dir\
colors from https://github.com/seebi/dircolors-solarized
And it works inside konsole terminal but not for tty (ctrl+alt+f2)

I have pretty much customization in .bashrc file which is available with link http://ix.io/2G4W

Guessing that some of that settings conflict with tty LS_COLORS environment variable, because if I try to do echo $LS_COLORS there it returning no results.

https://redd.it/krky6r
@r_bash

Читать полностью…

r_bash

AWK equivalent of sed -q

I'm writing a "webscraping" script in AWK to return the text from bullet-point lists on Wikipedia pages, and it's working as I intended, with the caveat of it including some unwanted doubled results from that "Category" box at the end of the page.

&#x200B;

I figured the solution would be to "stop" the input that's being read at a point that matches the syntax that starts that block.

Doing it with sed '/regex/q' and piping it into awk worked, but I wanted to make this a part of the AWK script (with native syntax, that is).

I've tried /regex/ {exit} and variations of this syntax, but as I found out, that obviously just exits the script before doing any of the processing (mainly regex matches, sub and gsub to clean the HTML syntax), and AFAIK just passing all of this "processing" syntax to the END block wouldn't work.

&#x200B;

Any help will be really appreciated, thanks in advance for all of the replies.

https://redd.it/kraf8u
@r_bash

Читать полностью…

r_bash

Adding leading zeros to a counter

I have a counter in this command:

while read -ra line; do for word in "${line@}"; do COUNTER=$COUNTER + 1 && videogrep --input merz2.mp4 --output $COUNTER$word.mp4 --max-clips 1 --search "\b${word}\b"; done; done < texttestfile.txt


it creates:

0word.mp4
1word.mp4
2word.mp4

but i want:
00word.mp4
01word.mp4
03word.mp4

how do i add leading zeros to the counter so the amount of digets stays the same if i count over 9?

https://redd.it/kr40cb
@r_bash

Читать полностью…

r_bash

Understand Why Colors Disappear With Pipes
https://www.semicolonandsons.com/code_diary/bash/understand-why-colors-disappear-with-pipes

https://redd.it/kqw1hm
@r_bash

Читать полностью…

r_bash

How to append text in the beginning of the file by using sed and variable?



I found interesting discussion here and tried the solution given with sed

>How to insert a text at the beginning of a file?
https://stackoverflow.com/a/9533736/13353532


It works with normal text, but not when the text is saved in a variable.

wolf@linux:~$ cat file.txt
1
2
3
wolf@linux:~$

Add text <added text> to the beginning of the file

wolf@linux:~$ sed -i '1s/^/<added text>\n/' file.txt
wolf@linux:~$

It works!

wolf@linux:~$ cat file.txt
<added text>
1
2
3
wolf@linux:~$

However, when I tried it with variable it doesn't work anymore.

wolf@linux:~$ var='Text 1'
wolf@linux:~$ echo $var
Text 1
wolf@linux:~$

wolf@linux:~$ cat file.txt
1
2
3
wolf@linux:~$ sed -i '1s/^/"$var"\n/' file.txt
wolf@linux:~$ cat file.txt
"$var"
1
2
3
wolf@linux:~$

Instead of "$var", how do I call the actual value of $var in sed command?

https://redd.it/kquqc2
@r_bash

Читать полностью…

r_bash

Trying to make content searches easier from txt file of large ebook list i've created. (Title of this is vague, I know. Better details in description.)

Over the years i've amassed a massive collection of ebooks. So much so that I have trouble remember if I have a certain one or not whenever I run into a book recommendation either from a friend or online or somewhere else. So what I've done a long time ago is write a simple logfile script of every book title to a txt file. Then whenever I want to check to see if I have a book already i just simply open a terminal, type "egrep -ri 'book name' egreplist/*log" and can see instantly if i have said ebook or not.

But what I'd like to do is make this book checking process easier by creating a script where I only have to type something like '
ebook-script.sh nameofbookhere' and it would show me whether I have the book that way. I know it can be done as I do it with other CLI applications that I've downloaded but I just don't know how to go about creating it on my own. I assume it's rather simple, but i don't know where to start looking or what such a process would be called. Any help would be much appreciated. Thanks

https://redd.it/kqiv6i
@r_bash

Читать полностью…

r_bash

A little help for getopts

Hi guy, can you help me understang why if I run my script without arguments it doesn't print the usage?
I want it printed and it happens only if I use the -h option of with a wrong option:

usage() { print "\n$( basename $0 ) usage : \v\n\t\
-V blabla
-g blabla
-o blabla
-d blabla
-s blabla
-h print this help \n"
exit 1
}

while getopts ":V:g:o:d:s:h" opt
do
case $opt in
V) VPC=$OPTARG;;
g) GP="y";;
o) ORACLE="yes";;
d) RPM="rpm";;
s) IP=$OPTARG;;
h) usage;;
\?) usage;;
esac
done

This is it running:

root@localhost ansible# ./script.ksh
root@localhost ansible# ./script.ksh -h

script.ksh usage :

-V blabla
-g blabla
-o blabla
-d blabla
-s blabla
-h print this help

root@localhost ansible# ./script.ksh -b

script.ksh usage :

-V blabla
-g blabla
-o blabla
-d blabla
-s blabla
-h print this help

root@localhost ansible#

https://redd.it/kqjd7v
@r_bash

Читать полностью…

r_bash

Retrieving args not working when applying &&

Had a script to touch then apply chmod +x to a file named by argument. Wanting to make it an alias since its just two lines, alias mkexe='touch $1' works fine with creating a file, but with alias mkexe='touch $1 &&/; chmod +x $1' touch raises error "touch: missing file operand Try 'touch --help' for more information.", any advice?

https://redd.it/kqe7eq
@r_bash

Читать полностью…

r_bash

cesar: pre-commit hook to compress images
https://github.com/Kraymer/cesar

https://redd.it/kq7vgq
@r_bash

Читать полностью…

r_bash

Replace line of one file with line of another file

How to replace a line beginning with the string user_pref("extensions.webextensions.uuids" in one text file with the line beginning with the same string in another text file? Only one replacement should be done (first occurrence, but it can be assumed that the file only has a single line beginning with this substring). The replacement should be done over the existing line (as opposed to deleting the line being replaced and just appending the line to the end of the file).

Return an error if a replacement cannot be done because e.g. one of the files does not have a line beginning with this substring.

https://redd.it/kpxyha
@r_bash

Читать полностью…

r_bash

obfuscation

https://github.com/2421a4f1fd43dfb4f8225325c/asm_obfuscate

read README.md

https://redd.it/ksisis
@r_bash

Читать полностью…

r_bash

Question Install a new kernel, and then add modules/apply tweaks right away, or have to restart?

Hey, As the title says, I want to do both things automatically. It would be ideal if I could just do both in the same script/shell.

Another idea I had was to have the first script set up a part-two script to be executed after boot and then request a reboot, but for testing, development, and just all round usability, it would be great to do it all in one step.

https://redd.it/ksbgja
@r_bash

Читать полностью…

r_bash

Question Bash Script Loop File Size

Hi all,

I appreciate your help in advanced. I have a bash script that runs off a DD command.

I originally have 1 KB.bin file created already and as I loop through it it will keep creating a new file as long as long as I set the loop to go for. So I will have file names called file_1KB1.bin ,file_1KB2.bin, etc.

My question is the new files that are being created, why aren't they a 1024 byte size file? After I run the command ls -al it shows that they are 0 byte sized files. Any comments or help would be appreciated!

\#!/bin/bash

for i in $(seq 1 3); do

dd if=file_1KB.bin of=/dev/mmcblk0

touch file_1KB`ls file_1KB* | wc -l`.bin

done; wait

https://redd.it/ks17em
@r_bash

Читать полностью…

r_bash

An example DIRCOLORS file for monochrome or grayscale display

Recently, I've switched to displaying Xorg in grayscale (setting the DigitalVibrance parameter of my NVIDIA card to its minimum value if you are curious).
I had a complex `DIR
COLORS (or rather ~/.dircolors`) for fancy ls color output, but it expectly
didn't play nice combined with grayscale (some colors converted to gray not very readable).

Thus I wanted to disable all color in ls output, except directories in bold font.
After some tinkering, I came up with:

&nbsp;

BLK 0
CAPABILITY 0
CHR 0
DIR 1
DOOR 0
EXEC 0
FIFO 0
FILE 0
LINK 0
MULTIHARDLINK 0
NORMAL 0
ORPHAN 0
OTHER
WRITABLE 0
SETGID 0
SETUID 0
SOCK 0
STICKY 0
STICKYOTHERWRITABLE 0

&nbsp;

You'd think that it would be sufficient to set DIR 1 (bold), but nope as there are hardcoded defaults colors for other item types if you do not set them explicitly with 0.

With that file, only directories are made bold. It can be used as a base for customization (mostly not involving color), for example using underline or italic for some file types.

With that setup, it is also interesting to use the -F option of ls to display trailing characters to identify some file types (folders, executable, links, ...).

https://redd.it/kru2w5
@r_bash

Читать полностью…

r_bash

How to grep certain interface block in Cisco IOS configuration with specific criteria?

This is sample of network interface configuration in Cisco IOS. Let's name it as cisco.txt
.

!
interface FastEthernet0/1
shutdown
!
interface FastEthernet0/2
switchport access vlan 20
!
interface FastEthernet0/3
switchport mode access
!
interface FastEthernet0/4
!
interface FastEthernet0/5
switchport access vlan 50
switchport mode access
!

What I want to do is to grep the interfaces that do not have the following keyword:

shutdown|switchport access vlan \d+|switchport mode access

However, I do not really know how to do this at the moment. The only thing I can think now is to grep those matches the pattern like this.

grep -P 'interface|shutdown|switchport access vlan \d+|switchport mode access' cisco.txt

E.g.

$ grep -P 'interface|shutdown|switchport access vlan \d+|switchport mode access' cisco.txt
interface FastEthernet0/1
shutdown
interface FastEthernet0/2
switchport access vlan 20
interface FastEthernet0/3
switchport mode access
interface FastEthernet0/4
interface FastEthernet0/5
switchport access vlan 50
switchport mode access
$

I've also tried -v, --invert-match to select non-matching lines, but the only output that I've got is this.

$ grep -vP 'interface|shutdown|switchport access vlan \d+|switchport mode access' cisco.txt
!
!
!
!
!
!
$

I was hoping to get the following output only based on my critera above.

interface FastEthernet0/4

Also, if grep is not the proper tool for this, please let me know the alternative.

https://redd.it/krpku0
@r_bash

Читать полностью…

r_bash

Is this explanation of the code right?

while read -r titel verfasser schriftreihe kategorie jahr verlag seiten isbn
do
...
done < $datei

The standart input of the while loop is $datei. The while loops works itself throught the file input in which he assigns each field to one of the variables 'titel','verfasser',... . This happens until he reaches the end of file, then the while loops ends.

https://redd.it/krkcpk
@r_bash

Читать полностью…

r_bash

Pipe into less but let it keep scrolling automatically as the characters come and also let it close automatically?

I love how less shows you text and then you can go back to your terminal like nothing happened as your old text is still there. But can you use less or a similar program to keep scrolling automatically without required user input on each page, and also let it automatically close when the piping is done? Without destroying your old terminal text state.

https://redd.it/kr54hn
@r_bash

Читать полностью…

r_bash

Trying to run multiple commands in a Synology DSM scheduled task, but it only runs the first.

To wit,

#! /bin/sh
folderpath="/volume1/Emby Libraries/Movies/C/Clue (1985)/Clue (1985) - Ending"
extension=".mkv"
endingpicked=(1 + RANDOM % 3)
normsubs = ".en.srt"
hohsubs = ".English SDH.srt"
ln -f "$folderpath $endingpicked$extension" "/volume1/Emby Libraries/Movies/C/Clue (1985)/Clue (1985) - Random Ending.mkv" ; ln -f "$folderpath $endingpicked$normsubs" "/volume1/Emby Libraries/Movies/C/Clue (1985)/Clue (1985) - Random Endling.en.srt" ; ln -f "$folderpath $endingpicked$hohsubs" "/volume1/Emby Libraries/Movies/C/Clue (1985)/Clue (1985) - Random Ending.English SDH.srt"

Only creates the MKV file, it doesn't create either of the two SRT files.

https://redd.it/kr1otp
@r_bash

Читать полностью…

r_bash

Git Bash (Win10)'s 'Git Bash Here' always opens in root git directory

Hi guys, I have an annoying problem with git bash. No matter which project folder I'm in, using 'Open Git Bash Here' with right-click always takes me to my root git directory. Is this by design or am I doing something wrong?

https://redd.it/kqufv2
@r_bash

Читать полностью…

r_bash

A utility to inspect exactly which characters are sent from stdin (via piping or typing on the keyboard)
https://github.com/Joedang/dotfiles/blob/master/bin/charinspect

https://redd.it/kqrhxh
@r_bash

Читать полностью…

r_bash

any way to echo this parameters expanded values?

#!/bin/bash
a=1;b=2;c=3;d=4;e=5
for v in {a..e}; do echo $v ; done
$bash thisfile
a
b
c
d
e

\#my expectations were:

1
2
3
4
5

any pointer to a solution will be appreciated

https://redd.it/kqkhes
@r_bash

Читать полностью…

r_bash

rpid: Get the PID of an ancestor process by name

Hi. I'm fairly new to bash scripting, but I made a status monitor for the window manager dwm. The status monitor consists of a daemon and a scheduler which communicate via a named pipe. To make a long story short, I needed to give each pipe a unique name that indicated which login process it was created under, and then I had to figure out what that name was from each component
of my application that needed to connect to the pipe.

The pipe name is made unique by appending the PID of the current login process to it. Then each component under the same login process can use that PID to find the pipe. So, the problem reduces to, *from a given process, find the PID of the login process that this process is descended from*. To do that, I wrote the little script below, which I called `rpid` for root PID. It takes two arguments:

- The PID of the process whose ancestor you want to find. This is usually the current process, so `$$`.
- The name (not the command line) of the ancestor, or root whose PID you want to find. This parameter is optional and defaults to `login`.

I would appreciate your thoughts and comments on the code, since as I've said, I'm a bash novice. And if you would like to checkout the git repo for the status monitor that contains this script, it's on [gitlab](https://gitlab.com/narvin/avd).

#!/bin/bash

USAGE='
USAGE: rpid <pid|-h|-[-]help> [<root_name>=login]

pid The PID of the process for which you want to find the root PID.

root_name
The name (not command line) of the root process, for which
to find the PID. Defaults to login.

EXAMPLES:

Any of these will display this help message.

rpid -h
rpid -help
rpid --help

Get the PID of the login process that is the ancestor of the current
process.

rpid $$
'

# Validate the arguments
if [[ "$#" -lt 1 || "$#" -gt 2 ]]; then
printf '%s' "${USAGE}" 1>&2
exit 128
fi

pid="$1"

# Check if the user needs help
if [[ "${pid}" =~ ^(-h|-(-)?help)$ ]]; then
printf '%s' "${USAGE}" 1>&2
exit 0
fi

root_name="${2:-login}"

get_info() {
local -ri current_pid="$1"
if [[ ! -r "/proc/${current_pid}/status" ]]; then exit 1; fi
mapfile info < \
<(grep --null -E -m 2 '^(Name|PPid):' "/proc/${current_pid}/status" \
| sort | cut -f 2)
name="${info[0]##[[:space:]]}"
name="${name%%[[:space:]]}"
ppid="${info[1]##[[:space:]]}"
ppid="${ppid%%[[:space:]]}"
}

next_pid="${pid}"
while [[ "${name}" != "${root_name}" && "${ppid}" -ne 1 ]]; do
get_info "${next_pid}";
name_pid="${next_pid}"
next_pid="${ppid}"
done

if [[ "${name}" != "${root_name}" ]]; then exit 1; fi
printf '%s\n' "${name_pid}"

https://redd.it/kqe4z0
@r_bash

Читать полностью…

r_bash

Converting txt to json

I'm completely new to Linux (primarily a Windows Dev) and am looking for a solution that'll help me convert a text file formatted as follows into JSON

SomeVal1
SomeVal2
.
.
.
SomeValn
------------------------------
1 2 3 . . . n
10 11 12 . . . n
.
.
.
n

It needs to look as follows:

{ "SomeVal1" : "1"
"SomeVal2" : "2"
"SomeVal3" : "3"
.
.
"SomeValn" : "n" }

&#x200B;

In C# I'd probably create a dictionary to hold the variable column names and loop over everything past the `---------------------------` characters using the whitespace as my column delimiter and the newline as my loop iterator. Is there an easier approach using bash?

https://redd.it/kqa26t
@r_bash

Читать полностью…

r_bash

sed and urls

i wrote a bash script that i supply with DOI number and it grabs an artical from sci-hub but the problem sci-hub and there URL scheme is terrible some times they include https and some time not so i wrote sed sub that sees if it does not have https, it will include it but if the url has a HTTPS it will fail and will not download can you help me





 
#!/bin/bash

cachedir="$HOME/.cache/sci-hub"
doi="$1"
url=(https://sci-hub.se/"$doi")


curl -s "$url" > "$cachedir"/tmp.html

grep -o '<iframe src = ".*//.*></iframe>' "$cachedir"/tmp.html
| sed 's/<iframe src \= \"/https\:/g'
| sed -e 's/" id = "pdf"><\/iframe>//g' > $cachedir/tmp.txt

wget -i "$cachedir"/tmp.txt


https://redd.it/kq3wrc
@r_bash

Читать полностью…

r_bash

I need to move argument from the end line to the middle

I want to type something like ffmpeg -i -c copy filename.mp4 mediaURL but the mediaURL have to be between -i and -c. Is there any line trick to move the mediaURL there?

https://redd.it/kpthe7
@r_bash

Читать полностью…
Subscribe to a channel