r_bash – Telegram
1-3 vs {1..3}

I have a question about the difference between these wildcards and how they function.

When I run “touch file{1..3}” I create three files: file1 file2 file3

But when I run “touch file1-3” I create one file named “file1-3”

Running either of these wildcards with rm however does delete all relevent files.

Why does this happen? What is the difference in the way these wildcards function?

https://redd.it/x8emnf
@r_bash
Is there a better way to write this if statement?

Hi, I'm making a bash noscript for personal use, and I wrote this, it does work as expected but I feel it's not the proper way to do it since it has nested if. Just curious to learn the best practices.
I hope the code is clear enough, thanks in advance.

Edit: the code is to update an SQLite table, the target represents the row and the action whether it should add or subtract.

action= #add or subtract
target= #a or b

if [[ "$action" == "add" ]]; then
if [[ "$target" == "a" ]]; then
# some code
else
# some other code
fi
else
if [[ "$target" == "a" ]]; then
# code
else
# other code
fi
fi


https://redd.it/xc0z3c
@r_bash
Linux/Bash/Software Help

For some reason I can't run Linux/bash commands, I downloaded Linux, I have powershell, I also got Ubuntu, allowed my computer to for the creator mode. I ran a code that tells me if I'm running bash and I'm not. The point is I scoured the internet and nothing worked, I'm drawing to the conclusion that I may have a virus and contemplating if I should have a specialist check it out. Any advice helps.

P.S I also thought about wiping my hardrive, besides my files and reinstall any needed programs.

https://redd.it/xc22sz
@r_bash
gh-f stable release 1.0.0

I have worked on gh-f for about one year and I have now reached the point where I consider it to be stable and robust enough to award it a full 1.0.0 release.

## What is it?

gh-f is a gh CLI extension that dreams to be the ultimate fzf git integration. There are many git-fzf projects out there and we all have our own shell functions to do this and that: I wanted to unify most of it and create a one-stop shop that might address the vast majority of daily usage of git and GitHub: whilst of course it may not fulfill all needs, I found myself fulfilling most of my git needs through it.

## Is it better or worse than the others?

There are many git-fzf projects out there and they are all very good: so why should you install this other one? Because it's all-in-one place, compact, with one line installation (as gh extension) and with no need to set-up (and remember) new aliases or learn to move around some complicated TUI. It really is just git and fzf, but hopefully complete enough to replace 90% of your daily git commands. Moreover, it is easily extensible as it is just bash code that you can fork and make your own, if you want it to behave differently (no need to learn anything new if you want to make a change)

## What about this latest release?

This 1.0.0 contains no major features or improvement. Only I took the time to go through most bugfixes with many intermediate releases, often incorporating previous feedback received here on reddit/discord/GitHub and having my friends and coworkers use it for long enough to unearth major errors.

Have a look: link to the repository - there are demos, gifs and full documentation with examples. Feel free to comment, open issues and provide suggestions.

https://redd.it/x8a32w
@r_bash
set -x is your friend

I enjoy looking through all the posts in this sub, to see the weird shit you guys are trying to do. Also, I think most people are happy to help, if only to flex their knowledge. However, a huge part of programming in general is learning how to troubleshoot something, not just having someone else fix it for you. One of the basic ways to do that in bash is `set -x`. Not only can this help you figure out what your noscript is doing and how it's doing it, but in the event that you need help from another person, posting the output can be beneficial to the person attempting to help.

Also, writing noscripts in an IDE that supports Bash. syntax highlighting can immediately tell you that you're doing something wrong.

If an IDE isn't an option, https://www.shellcheck.net/

https://redd.it/xcejrb
@r_bash
Having issues with editing strings

Hello, i am very new to bash noscripting and i want to remove a part of a string and save into a variable to later use in the noscript

i have a noscript that goes like this

fullString="file/filename/fizz/buzz"

cutString=${fullString} | cut -d/ -f3-

echo ${cutString}

i want to output fizz/buzz only, but i dont get anything at all

https://redd.it/xcdybx
@r_bash
Imagemagick annotate noscript, almost but not quite...

What I want to do..

> convert dragon.gif \
> -gravity North -background YellowGreen -splice 0x18 \
> -annotate +0+2 'Faerie Dragon' anno_splice2.gif

What I have...

>\#!/bin/bash
>echo
>echo -n "Image to use: "
>read -e infile
>echo
>echo -n "Enter text: "
>read -e text
>echo
>convert $infile -gravity North -background black -fill green -pointsize 28 -splice 0x35 -annotate +0+2 $text out.gif

Works fine with one word text but I want to enter multi word text.

Tried single and double quoting when running noscript. Nope.

Tried entering word\nword just to see what would happen, though I don't want two lines. Nope.

Tried quoting '$text' and backslashing the quotes. Nope.

*halp*

https://redd.it/xcjbie
@r_bash
Relative path Script and file in the same directory, trying to ma

I've got a file called 1.txt as well as a noscript in the same directory. Now, I'd like to run the noscript in order to copy 1.txt to another directory. However, I'd like to do that using a relative path.

I've tried:

cp ./1.txt /whatever/folder

... but to no avail as I'm getting the following error:

cp: can't stat './1.txt' : No such file or directory.

https://redd.it/x87lne
@r_bash
use sed for add a txt file in another

Hello, i can't understand how to do this command.

​

I have an output file, and input file.

the output file is already write.

i need in this output file to add a new line in the top of the file, with the record inside input file.

​

i try multiple times but i can't understand how.

https://redd.it/x83ypl
@r_bash
Changing environment variable after running a certain command.

In my bashrc I currently have this line:

export PROMPTCOMMAND="export PROMPTCOMMAND=echo"

What this does is, it will print a new line after every command EXCEPT the first one. it helps with readability and my terminal does not have an awkward space on the first line, it would look like this:

Prompt-$: Sample command
Command output

Prompt-$:

This is the expected behaviour. However, after clearing my terminal, the environment variable for PROMPT_COMMAND will still be "echo", so it will leave a gap over the prompt:

(After running clear)

(reddit won't let me add blank lines, but this line is blank in my terminal haha)
Prompt-$: Sample command
Command output

Prompt-$:

I have tried to fix it by creating a custom noscript that exports PROMPT_COMMAND to "export PROMPT_COMMAND=echo" and then runs clear. But again that is no good. Is there any way I can change it when I run ls?

https://redd.it/xcqa68
@r_bash
is There a way to run a Function in a subshell so it won't leave the current directory?

SOLUTION

GOAL:

1. Go to ANIMES folder
2. Use the noscript vf Piece.02 to search the episode two of One Piece and launch it on VLC.
3. All of this in a sub-shell so you're won't leave the current directory

OBS: Change "Piece.02" for whatever anime/episode I want.

CODE (provided by spizzike in the comments):

vf() (
cd /folder/you/want
find . -name "$1" -and '(' -iname '.mp4' -or -iname '.mkv' -or -iname '.avi' ')' -exec vlc '{}' +
)

1. The noscript search for the word I provided
2. Check if it's a video file (.mp4 or mkv or avi)
3. And launch it in VLC

All of this without leaving the directory you're in.

.

.

.

ORIGINAL POST

Hi bash ninjas!

Question 1 - How to run a function in a sub-shell.

My goal is to launch an episode from an anime from any place, using the find command and providing the number of the episode I want to watch, all of this in a sub-shell.

So, I basically want a function to:

1 - Open a specific directory

2 - Search a file inside the directory based on a number I'll provide.

3 - Get the file found and launch it on VLC.

4 - Do all this in a sub-shell so I will remain in whatever directory I'm in before use the function/noscript.

I came up with this:

vf() {
(var=$(find /directory/I/want -wholename "
$1" -and -wholename ".mkv")
vlc "$var")
}

The function works but I end up in the directory I provided to find; the (), that runs aliases in a sub-shell doesn't seem to work in functions.

Question 2 - How to search for many different file formats at once?

I'm searching for .mkv files, but if I want the function to work with other video formats, like .mp4 or .avi, how would I do that?

https://redd.it/x7qmw1
@r_bash
How to avoid duplications using sed

I am trying to append some lines using sed and if I run the sed twice it will duplicate the lines. How to avoid this like if I mistakenly run same sed command it should not duplicate.

https://redd.it/x7tcyu
@r_bash
Remove a function when vim or any other program opens

So, I have a function in my bashrc which displays the time in my terminal, however I don't want the function to run when vim or some other program is opened is this possible?

https://redd.it/x7xkpy
@r_bash
wild card works in shell but not in noscript

So I'm writing a noscript for my church and I'm trying to use a wild card to copy some files from one directory to another. It's noteworthy to mention that it's on a flash drive. So when I run the command the the terminal it works just fine, but when I run it as a noscript it doesn't work. Here's an example, and I'll post the actual noscript if that would be helpful

Cp /run/media/username/church/noscript\ resources/ hymnal/hymns/tlh"$hymn1"*.mid /run/media/username/church/"$datefolder"

I should note that the wild card is to fill in the unknown parts of the file name, things like \_5 for 5 verses

Also both of the variables are defined already

I will probably be posting the noscript because I realize that will be helpful

Here is the noscript:

#!/bin/bash
user=$(pwd | cut -d / -f 4)
distro=$(distro | grep Fedora)
if [ $? != 0 ]
then
distro=/media/
else
distro=/run/media/
fi
fdrive="$distro$user/church"
hymnpath="$fdrive/Script\ resources/hymns/Lutheran\ hymnal/"
hymn_1=$1
hymn_2=$2
hymn_3=$3
hymn_4=$4
date=$5
date_folder="$fdrive/$date"
litpath="$fdrive/Script\ resources/Litergies/"
if [ "$1" == "-n" ]
then
read -p "Enter the first hymn " hymn_1
read -p "Enter the second hymn " hymn_2
read -p "Enter the third hymn " hymn_3
read -p "Enter the fourth hymn " hymn_4
read -p "Enter the date " date

fi
if [ "$1" == "-h" ]
then
echo "Syntax:"
echo "./hymnnoscript [hymn 1] [hymn 2] [hymn 3] [hymn 4] [date]"
echo "enter n for no parameters"
echo "enter h for help"
fi
regular (){
echo "regular"
cp "$hymnpath"tlh"$hymn_1"*.mid $date_folder
cp "$litpath"revisedPAGE5best*.mid $date_folder
cp "$hymnpath"tlh"$hymn_2"*.mid $date_folder
cp "$litpath"OFFERPG5*.mid $date_folder
cp "$hymnpath"tlh"$hymn_3"*.mid $date_folder
cp "$litpath"2collectbendpg14*.mid $date_folder
cp "$hymnpath"tlh"$hymn_4"*.mid $date_folder
}
mkdir $date_folder
ls /run/media/user-0/church/Script\ resources/*/
regular

Here is the error:
cp: cannot stat '/run/media/user-0/church/Script\ resources/hymns/Lutheran\ hymnal/tlh123*.mid': No such file or directory
cp: cannot stat '/run/media/user-0/church/Script\ resources/Litergies/revisedPAGE5best*.mid': No such file or directory
cp: cannot stat '/run/media/user-0/church/Script\ resources/hymns/Lutheran\ hymnal/tlh125*.mid': No such file or directory
cp: cannot stat '/run/media/user-0/church/Script\ resources/Litergies/OFFERPG5*.mid': No such file or directory
cp: cannot stat '/run/media/user-0/church/Script\ resources/hymns/Lutheran\ hymnal/tlh124*.mid': No such file or directory
cp: cannot stat '/run/media/user-0/church/Script\ resources/Litergies/2collectbendpg14*.mid': No such file or directory
cp: cannot stat '/run/media/user-0/church/Script\ resources/hymns/Lutheran\ hymnal/tlh127*.mid': No such file or directory


​

https://redd.it/x7t7f8
@r_bash
If I can use bash on Windows, why bother using Linux at all?

I know this might be a noob question for you people, however, I think there are few other noob people like me wondering about this :)

I have been using Linux for 3 months now for work (we use an application that only works on Linux). My boss has taught me some Linux tricks and he is a long-time Linux user. All the things he showed me are just Bash commands.

It seemed like it was all about the command window and how powerful Linux is because of that only.

Then I asked, "Wait for a second, it turns out I can use bash on Windows too, then why Linux?"

Is it bash that makes Linux powerful, or something else?

https://redd.it/x89c1l
@r_bash
Is This Geocode Script Broken?

Hi, I have an address string, ex: "2240 Saint Claude Avenue, New Orleans, LA 70117"


And have been searching for a noscript to convert this type of input into a pair of longitude and latitude coordinates. A noscript I found on [Webgears](https://www.webgears.com/blog/simple-noscript-to-get-latitude-and-longitude-from-an-address/) seems perfect/practical for that purpose and appears as follows

#!/bin/sh

url='http://www.mapquestapi.com/geocoding/v1/address?key='

args='&location='

key='YourMapQuestAPIKey'

converter="$url$key$args"

addr="$(echo $* | sed 's/ /+/g')" curl -s "$converter$addr" | \

cut -d\" -f117,119 | \

sed 's/[^0-9\.\,\-]//g;s/,$//' exit 0

Except it doesn't seem to work at all. Apparently searching for files etc instead of performing any function. The author says input should appear as such:

>./geocode.sh 8865 SE Bridge Road, Hobe Sound, FL

Does anyone have an idea of why this might be? I've tried to execute this on both Win10 and Ubuntu.

https://redd.it/x7ohgn
@r_bash
Having Issues Comparing Strings

In part of my noscript I am trying to check if the string variable is equal to an uppercase B, but the check doesn't seem to return true with the variable is B. Does any part of this code look incorrect?

i = 0
if [ $f0 == `B` ];
then
((i++))
fi

Prior to the code snippet above, my code gets f0 by finding a file path and removing the all parts of the path and file extension except for the name - which is B (i.e. path/to/file/B.png gets turned into B)

https://redd.it/xd7a3p
@r_bash
Download root-owned files via scp or rsync as a sudo user

How to download the file which requires root permission from a remote server (via scp or rsync) as a sudo-user (non-interactive way)?

Note that this sudo-user is not configured to not require the password for sudo commands.
And sshpass is installed on both local and remote systems.


And download command may look like this
sshpass -p {sudo-user-pass} scp {sudo-user}@{host}:/path/to/some/root/owned/file.tar.gz ./

This command gives a "permission denied" error as file.tar.gz is owned by the root.

In order to download this file, sudo-user should be switched to root sudo -su

So is there a way in scp or rsync where sudo-user can switch to root and download the root-owned files (non-interactively)?

https://redd.it/xd7nml
@r_bash