Linux one-liners: Using the built-in Find command to replace 3rd party fuzzy finders
https://www.elsewebdevelopment.com/linux-one-liners-using-the-built-in-find-command-to-replace-3rd-party-fuzzy-finders/
I hope this is the right place to post, it is very much about shell scripting.
https://redd.it/kvsww3
@r_bash
How do you quickly find something in man bash?
For instance, if I want to lookup the read built-in, when I first stared, I would I do /read.
But that was a nightmare because there are so many occurrences of the word "read."
I guess if you happen to know which section it's under, you could do /SHELL BUILTINS.
Lately, I've been doing /^ *read (a line that starts with any number of spaces, followed by "read."
But even with that, I have to wade through a bunch of false-positives. Do you have any alternatives?
https://redd.it/kvhg7n
@r_bash
Can someone help me understand what "cat file.txt | sed 's@./@@' " is doing?
Specifically the @ signs in the sed command
From my understanding, we are taking the content of file.txt and sending it to sed. The file contains lists of directories and when ran it displays just the last portion of it. For example
/opt/something/test. Will show "test".
Sed is performing a substitution "sed 's" the . is a wildcard.
I cannot seem to figure out exactly what the @ sign represents, I just see that it works and what it is used for. Thanks!
https://redd.it/kv7bgn
@r_bash
BATS in pipeline /usr/bin/env
I get below error:
/usr/bin/env: bats: No such file or directory
I can't install BATS on building machine so I can only keep BATS data in repository only. It means that "#!/usr/bin/env bats" will not be working.
May I point directly to 'bats' file? Which one it is (in which directory)? Or maybe I can use simple 'bash' instead?
https://redd.it/kv2xyc
@r_bash
print string right aligned with fill characters
hi!
I have 30 characters space in a line and want to output a text right-aligned with remaining fill characters
var="I am text"
->
..................i am text
in python is very easy: print("{0:.>30}".format(var)
Is it possible to output in oneliner at bash?
https://redd.it/kv06xz
@r_bash
Multiline fixed string search and replace with cli tools
https://learnbyexample.github.io/multiline-search-and-replace/
https://redd.it/kv0fib
@r_bash
Source once
Is there an idiomatic way to source a file only once, sort of the equivalent of this in C:
#ifndef FOO
#define FOO
// Contents of foo go here
#endif
This is what I've been doing in bash:
if [ ! -v IS_SOURCED_FOO ]; then
ISSOURCEDFOO=1
# Contents of foo go here.
# I don't indent a level here because I don't want all the code to be indented
# just for the source guard.
fi
The reason I'm interested in this is because I have a script that sources some other scripts (modules). However, each of those modules source another core module that I don't want to be sourced repeatedly. This method seems to work fine. I was just wondering if you see any pitfalls with it, or if there is a better way to do this.
https://redd.it/kutig0
@r_bash
A question of overwriting files from a newbie
Hey, I am learning bash since 4 days I think I got some of the basics.
Anyway, for the wallpapers downloaded from Variety I've written two scripts. One of them moves downloaded photos older than 12 days to a folder and renames them all as "Aday 1,2,3..." and the other lets me select these and moves them to another folder and removes photos I didn't select.
I think I should write scripts to better explain my problem
Script 1:
#!/bin/bash
#Move automatic downloaded WALLPAPERS older than 12 days to 'Seçme-Eleme'
#Move WALLPAPERS downloaded from nasa_apod
find /home/eurydice/.config/variety/Downloaded/nasa_apod -mindepth 1 -maxdepth 1 -type f -mtime +12 \
-exec mv '{}' /home/eurydice/Bulunur\ Bir\ Şeyler/Dosyamsılar/Seçme-Eleme ';' -prune
#Move WALLPAPERS downloaded from Unsplash
find /home/eurydice/.config/variety/Downloaded/Unsplash -mindepth 1 -maxdepth 1 -type f -mtime +12 \
-exec mv '{}' /home/eurydice/Bulunur\ Bir\ Şeyler/Dosyamsılar/Seçme-Eleme ';' -prune
#Move WALLPAPERS downloaded from Wallhaven
find /home/eurydice/.config/variety/Downloaded/wallhaven_wallhaven_cc_search_categories_110_purity_100_atleast_2560x1080_topRange_1y_sorting_views_order_desc -mindepth 1 -maxdepth 1 -type f -mtime +12 \
-exec mv '{}' /home/eurydice/Bulunur\ Bir\ Şeyler/Dosyamsılar/Seçme-Eleme ';' -prune
#Now rename files in 'Seçme-Eleme' like "Aday X"
cd /home/eurydice/Bulunur\ Bir\ Şeyler/Dosyamsılar/Seçme-Eleme; \
i=1
for fi in *.png; do
mv -i "$fi" "Aday $i.png" ;
for fi in *.jpg; do
mv -i "$fi" "Aday $i.jpg" ;
i=$((i+1))
done
done
Script 2:
#!/bin/bash
#Move victors of 'Seçme-Eleme' to 'Kazananlar'
cd /home/eurydice/Bulunur\ Bir\ Şeyler/Dosyamsılar/Seçme-Eleme
echo "Select victors"
read vct
for i in $vct; do
mv -i "Aday $i.png" /home/eurydice/"Bulunur Bir Şeyler"/Dosyamsılar/Kazananlar/"Bahar $RANDOM.png" ;
mv -i "Aday $i.jpg" /home/eurydice/"Bulunur Bir Şeyler"/Dosyamsılar/Kazananlar/"Bahar $RANDOM.jpg" ;
done
#Now let's remove the rest
rm /home/eurydice/Bulunur\ Bir\ Şeyler/Dosyamsılar/Seçme-Eleme/*
So, Script 1 works just as I wanted except for a small issue which it keeps telling me "/home/eurydice/.config/variety/Downloaded/Unsplash/(name).jpg" isn't found or something but in Script 2 I originally intended to define another variable just like "i=1" in the first one (let's call this "n") and so did I with copying and changing the variable from Script 1. it was something like that
for i in $vct; do
n=1
mv "Aday $i.png" /home/eurydice/"Bulunur Bir Şeyler"/Dosyamsılar/Kazananlar/"Bahar $n.png" ;
mv "Aday $i.jpg" /home/eurydice/"Bulunur Bir Şeyler"/Dosyamsılar/Kazananlar/"Bahar $n.jpg" ;
n=$((n+1))
done
but when i do that for the first time the script worked as I intended. However, in my 2nd test run this script overwrite the files that already existed. I mean, for example in 1st run i had 5 files whose names are "Bahar 1,2,3,4,5" and the 2nd time I chose 3 files to add. I wanted their names to be "Bahar 6,7,8" but instead, my script made them to be the new 1,2 and 3. I tried many solutions and when I couldn't fix that I just assigned random numbers to them.
Is there a way to make Script 2 work as I intended? Also, is there a way to mute that output I said about Script 1?
https://redd.it/ku615l
@r_bash
Hi everyone - noob question about ls command
Hi everyone,
I'm currently learning bash commands with freecodecamp and there was this one command that worked, but I didn't understand it.
The task was about first creating some fake *.JPG and *.RAW files with touch and interacting with them.
So I had used this command: ls [\^R\]*raw
I didn't really get what "[\^R\]*raw" did with my command.
Can someone explain it to me?
Any help is appreciated :)
https://redd.it/ktt7ug
@r_bash
Best/most performant way to trigger the environmental variable COLUMNS to update?
I use $COLUMNS in my script to make sure not to output more characters than the terminal currently is wide, however it seems the value of COLUMNS would only update when/after a command is run. A real command, no shell-builtin. So I wonder, what would be the most performant way to trigger a COLUMNS update? I thought of running a command and send its output to /dev/null. If I go with that approach, I'd need to find the most performant command (present on typical systems) that does nothing. Which command would you use? Or would you recommend a different solution?
https://redd.it/ktri1j
@r_bash
Need help with changing basic file structure. (need to append every odd line to every even line after it.)
i basically have a file that contains the following
line-one
line-two
line-three
line-four
line-five
line-six
and i would like append every odd numbered line to the even numbered line after it and adding a seperator between the two, like a "/" to have the output look like
line-two/line-one
line-four/line-three
line-six/line-five
https://redd.it/ktgnpe
@r_bash
Building a general wrapper for bash scripts
Some days past I was pointed to the bash repo, a wrapper for btrfs that with a bit more than 100 lines of code implements some docker functionality: bocker. I thought the general setup of the script was good, and I decided to generalize that syntax to be used by any bash script.
In short it's an opinionated (is it a framework?) way of handling properties, command invocation, and a format for help-messages.
`wash`
I don't know if I will ever use the script really, but it was great fun building, and it taught me a lot about bash.
Do anyone of you use wrappers/frameworks like this for your scripts to help with all the boilerplate etc.? Or is it generally frowned upon for some reason I have yet to think of? Do you have any hints for testing bash code? (I know I'm missing unit tests using source wash) what do you do to test your bash code?
Have a great day!
https://redd.it/ktcjut
@r_bash
Template config build
I'm writing a script to automate creating config files for myown. I don't want to use any GUIs.
How can i replace ${} placeholders in a text file?
.templatefile.txt
///....\\\I am ${info}.
It works for me :
rendernfo() {
eval "echo \"$(cat $1)\""
}
info="speechless"
rendernfo templatefile.txt configfile.ini
well.. but not if i have quotes and special characters in the templatefile.txt
the ${VAR} i want to replace are changing periodic
Any Idea ?
https://redd.it/kt1wum
@r_bash
Question bash script on ssh login for MFA
Hello all,
I am trying to secure my hosts with Okta MFA. I was able to put together a bash script where user/password will authenticate with okta and wait for a MFA to be provided. IF successful the scripts exits 0.
How can I force all users who ssh into the box to execute this script, where 1.) they can not do ctrl+d, ctrl+c, force close etc without it forcing the session closed 2.) after the script returns 0 for authenticating the MFA put the user in the shell?
I played around with using bashrc but need more guidance. It also looks like Okta decided to never provide a ssh PAM module which would solve all this.. as they are trying to sell advanced server access instead.
Thanks for any tips.
https://redd.it/ksqu6r
@r_bash
tmpsms - A temporary SMS utility right from your terminal written in POSIX sh
https://redd.it/kvsh3y
@r_bash
Any suggested improvements on my i3 blocks bash script?
Hi, I have just written the following bash script to display the song and artist of the currently playing Spotify song in the i3 blocks bar. It does work but I was wondering if anyone could suggest any improvements I could make to improve my bash skills?
#!/bin/bash
metadata=$(dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata') || exit 0
if [ "$(dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'PlaybackStatus')" =~ "Playing" ];then
song=$(grep xesam:title -A 1 <<< "$metadata" | tail -n 1)
song=${song#\"}
song=${song%\"}
artist=$(grep xesam:artist -A 2 <<< "$metadata" | tail -n 1)
artist=${artist#\"}
artist=${artist%\"}
# full text, short text (colour and bg colour omitted)
echo "♫ $song/$artist"
echo "$song"
fi
https://redd.it/kvamb4
@r_bash
Help using SED to format a text file
I have a text file that contains strings formatted as follows:
{
"[0 ValueA1 " : 1,
"1 ValueB2 " : 2,
"2 ValueC3 " : 3
}
]
and
{
"[ 1 - ValueA1" : 1,
" 2 - ValueB2" : 2,
"12 - ValueC3" : 3
}
]
How can I format these using SED so that the output is as follows (note the first example has a trailing whitespace character in the key string, while the second example does not):
{
"ValueA1" : 1,
"ValueB2" : 2,
"ValueC3" : 3
}
Unfortunately, I'm not an SED or Regex wiz and am having difficulty constructing this pattern.
https://redd.it/kv51aa
@r_bash
BATS how to mock script execution
I wanted to test below script:#!/usr/bin/env batsname=Johnecho "test"/home/john/extras.sh --key=$nameecho"end test"
​
How can I check if extras.sh was called and how to check what parameters was used?
https://redd.it/kv30jb
@r_bash
PSA: Difference between Trap ERR Vs Trap EXIT
https://www.semicolonandsons.com/code_diary/bash/trap-ERR-vs-trap-EXIT
https://redd.it/kv1fo4
@r_bash
"echo $(< file)" or "cat file"?
They both do the same thing. Any reason for why you would use one over the other?
The only points I can come up with is that the former is faster and might make you look like pro when the average user takes a look at your code as it is not as straightforward as the latter.
https://redd.it/kuyu56
@r_bash
I made a raytracer in pure bash! (inspired by the pure cmake raytracer)
https://github.com/aneeshdurg/bash-raytracer
https://redd.it/kur2nf
@r_bash
How do I properly do for i in "${var$@}"
So I essentially am using
>var=("$@")
>
>for i in "{var[$@\]}"
I was going through Pitfalls of bash and it stated that this was the wrong way to do this. It went on to describe the correct way to do other examples. But I could not see how to apply the same fixes to this.
​
Can anyone explain to me the proper way to do this, with an explanation. Much appreciated!
https://redd.it/kullm8
@r_bash
Is there a way to auto generate folder name from wildcard without writing full script?
I have a folder full of .rar files, each of which I want to expand into its own directory, and ideally then delete the .rar.
Is there something I can do on the command line without creating a full on script for it?
https://redd.it/kty476
@r_bash
Building React folder structure CLI tool using Bash shell
https://youtube.com/watch?v=f7SBhNblPso&feature=share
https://redd.it/ktsfm8
@r_bash
Pomodoro timer using figlet, cowsay, and optionally lolcat
https://github.com/meribold/muccadoro
https://redd.it/ktn3p5
@r_bash
'File.txt'$'\r' instead of File.txt
I have a script that passes through a couple of programs before it outputs. The issue is its output looks like this 'File.txt'$'\\r' I have no idea what I have done to cause this. Any help much appreciated as my google fu has failed me.
https://redd.it/ktfrjx
@r_bash
tmpmail v1.1.5 now supports receiving attachments
https://redd.it/ktbqhh
@r_bash
parallel-bash - Parallel processing of commands in pure bash.
https://github.com/Akianonymus/parallel-bash
https://redd.it/ksudh8
@r_bash
Where is the *, /, ~, and other built-in commands defined?
https://redd.it/ksrotf
@r_bash