r_bash – Telegram
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
How to get raw output of network list?

i making a bash noscript to control my wifi via dmenu and i use this command to get list of available networks:

iwctl station wlan0 get-networks

i am really new in bash, so i'd like to chop-chop the table i get using this command)

i don't know where to start, i don't really understand tools like cut/grep/awk( pls help

i want to output to be just list of names of this networks

​

sample:

Available networks
--------------------------------------------------------------------------------
Network name Security Signal
--------------------------------------------------------------------------------
> lll psk
eee psk
aaa psk

sample output

lll
eee
aaa

​

https://redd.it/xd9enh
@r_bash
Run commands in same Time

So I have multiple command that I want to run it in the same time what is best options ?

https://redd.it/xd760p
@r_bash
Help me figure out the right way to format this curl command? ("@" in URL password)

Edit: Solved using curl -u admin:p@ssword -H "Range: depth=1" -H "Accept: application/json" --digest "http://10.0.0.1/restapi/"

​

I'm pulling my hair out on this one and everything I find says to just put the URL in quotes and it'll be good. It is not good.

​

>curl -H "Range: depth=1" -H "Accept: application/json" \\
>
>\--digest "http://admin:p@ssword@10.0.0.1/restapi/"

The "@" in "p@ssword" is causing this command to be interpreted as the hostname of the base URL being "ssword@10.0.0.1", which is clearly not the intent. I cannot, for the life of me, figure out how to escape this character.

Thanks

https://redd.it/x7pd11
@r_bash
Two first lines of csv files using bash + general explanation

I want to find the first two lines of a csv file using bash in a jupyter notebook.

This is my code for the moment, what I should put in the X in order to obtain the first two lines of the csv file?

​

%%sh

head -X data/earthquakes.csv

​

I'm learning a bit of how to using bash with jupyter notebooks, in fact in don't understand a why is bash used in data science. Could somebody also explain this to me and why %%sh does not work in windows?

​

Thank you very much!

https://redd.it/x7e8a8
@r_bash
Not having success moving a file at the end of my noscript after transcoding completes

Hello! I have a noscript that I borrowed to help me post process recordings from Plex to A) remove commercials with an app called Comcut and B) transcode it with Handbrake CLI.

The problem I am running into is that once the noscript removes the commercials and then transcodes the file, it is not successfully replacing the newly transcoded file at the end of the noscript...

I even tried to embed the users password and run the "mv -f" command with sudo, but that did not seem to work either. Here is the noscript, and the command to move the newly transcoded file is the 7th line to the end, marketed with "# \*\*\*\* OVERWRITE HERE \*\*\*\*"

Any suggestions would be greatly appreciated! Thank you!

#! /bin/bash
#
# Plex DVR Postprocessing
# Version 0.0.1
# twitter.com/thatvirtualboy
# www.thatvirtualboy.com
#

# FIRST, RUN COMCUT TO REMOVE COMMERCIALS, THEN TRANSCODE AND COMPRESS

lockFile='/tmp/dvrProcessing.lock'
inFile="$1"
tmpFile="$1.mp4"
dvrPostLog='/tmp/dvrProcessing.log'
time=`date '+%Y-%m-%d %H:%M:%S'`
handbrake=/opt/homebrew/Cellar/handbrake/1.5.1_1/bin/HandBrakeCLI
cut=/opt/homebrew/Cellar/comskip/bin/comcut

echo "'$time' Plex DVR Postprocessing noscript started" | tee $dvrPostLog

# Check if post processing is already running
while [ -f $lockFile ]
do
echo "'$time' $lockFile' exists, sleeping processing of '$inFile'" | tee -a $dvrPostLog
sleep 10
done

# Create lock file to prevent other post-processing from running simultaneously
echo "'$time' Creating lock file for processing '$inFile'" | tee -a $dvrPostLog
touch $lockFile

# Run comcut
echo "'$time' Comcut started on '$inFile'" | tee -a $dvrPostLog
$cut "$inFile"

# Encode file to MP4 with handbrake-cli
echo "'$time' Transcoding started on '$inFile'" | tee -a $dvrPostLog
$handbrake -i "$inFile" -o "$tmpFile" --preset="Apple 1080p30 Surround" --encoder-preset="veryfast" -O

# **** OVERWRITE HERE ****
# Overwrite original ts file with the transcoded file
echo "'$time' File '$tmpFile' move started" | tee -a $dvrPostLog
echo "REDACTED" | sudo -S mv -f "$tmpFile" "$inFile"

#Remove lock file
echo "'$time' All done! Removing lock for '$inFile'" | tee -a $dvrPostLog
rm $lockFile

exit 0

https://redd.it/x7dtah
@r_bash
How to use sed command to replace values in a list & dictionary in a yaml file


I want to change the value of the seeds here.


seed_provider:

\- class_name: org.apache.cassandra.locator.SimpleSeedProvider

parameters:

\- seeds: "127.0.0.1:7000"


And want to change value of this :

data_file_directories:
\- /etc/test/config.yaml

https://redd.it/x7176q
@r_bash
Parameter Expansion for building cmd in for loop

Regarding building the source and target variables why does this work:

declare -a elemens=(
# hostname user filetotransfer file_suffix
"hostname1 username1 /path/to/file/on/remote suffix1"
"hostname2 username2 /path/to/file/on/remote suffix2"
)

for elem in "${elemens[@]}"
do
read -a strarr <<< "$elem"

source="${strarr[1]}@${strarr[0]}.hostname:${strarr[2]}"
target="./filename_${strarr[3]}"

scp -p $source $target
done


...and not just building them in the same line as scp as so:

scp -p "${strarr[1]}@${strarr[0]}.hostname:${strarr[2]} ./filename_${strarr[3]}"

With the later I just get the scp help message.
I realize it has something to do with variable expansion, but I do not understand why.

https://redd.it/x78gny
@r_bash
Count totals and correlate to $1

Hi all, I'm stumped by a problem and would love if I could get some help. I have a txt with lines and lines of data like this:

xxx.xxx.xx.xxx ftp ssh
yyy.yyy.yy.yyy ssh
zzz.zzz.zz.zzz smtp ftp

I need to count and correlate each service to the IP address, so the output would be similar to:

ftp count: 2
xxx.xxx.xx.xxx
zzz.zzz.zz.zzz

ssh count: 2
xxx.xxx.xx.xxx
yyy.yyy.yy.yyy

smtp count: 1
zzz.zzz.zz.zzz

I've been trying tons of stuff with awk but I'm getting nowhere and am afraid I'm deep down a rabbit hole. I think I need someone else's perspective on this one.

Anything you could give me to point me in the right direction would be awesome! Thanks!

https://redd.it/x6l7t5
@r_bash
Grepping for whitespace and double quotes as part of an if-then statement

I am attempting to create a noscript that will eventually run as a cron to check a conf file, and if it does not have a critical setting within it, to execute another noscript that regenerates the conf file.

The critical setting is: SecRuleEngine "On"

I can get the grep on my bash prompt just fine:

user@host ~# grep --color -h 'SecRuleEngine "On"' /path/to/conf-file.conf
SecRuleEngine "On"

And what I was wanting to do was to have a simple 1 for yes, 0 for no, like:

user@host ~# grep -c -h 'SecRuleEngine "On"' /path/to/conf-file.conf
1

Which I was hoping to feed into an if then statement in the noscript to test it, like:

if grep -c -h 'SecRuleEngine "On"' /path/to/conf-file.conf = "1" ; then
echo "all good"
fi

But on the line that starts with "if" I get:

: too many arguments

What I was hoping to do was get the noscript to see the grep as intended, then reverse it by putting a `!` after the `if` and then issuing the command to run the configuration noscript. What corrections do I need to make to my syntax?

[https://redd.it/xdmqhu

@r_bash
How do you export a mongo db into a JSON?

mongoexport --uri="mongodb://localhost:27017/" --username admin --password root --authenticationDatabase "admin" --db products --collection shoes --out dump.json

What's wrong with this command?

2022-09-02T18:00:24.492-0400 could not connect to server: connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.

There shouldn't be any authentication. Looking inside Studio 3T, I see Authentication Mode: None.

https://redd.it/x6zz1k
@r_bash
Parsing a csv with Unix time and an integer on each line.

Hello,

My csv looks something like this:

timestamp,Value

1662353808788,5

1662354108786,12

.

.

.

Hundreds of lines, ordered by time.

My goal is for the noscript to return the biggest value recorded in the last 24hours and ideally if given a parameter should be also able to return the biggest value for a specific day. I imagined something like placing a 0 as a parameter for today, 1 for the day before, 2 for the day before that, but not set on that.

Any help appreciated, even if you could just tell me what I will need to accomplish it, some algorithm, pseudo code, commands I should look at. Never worked with unix time before.

Thank you

https://redd.it/x6cacz
@r_bash