r_bash – Telegram
Running Multiple Python Scripts

I have multiple python noscripts that need to run, consecutively. I think the easiest way to implement this is using a cron job to run a bash noscript that runs the python noscripts one after another.

The python noscripts use argparse to take arguments from the cli and are reused in other places, so I'd like to be able to implement this without needing to change the python noscripts themselves.

Something like

#!/bin/bash
/venv/bin/python3 mynoscript.py somearg -u 'anotherarg' -p 'athirdarg'
/venv/bin/python3 mynoscript1.py somearg -u 'anotherarg' -p 'athirdarg'

https://redd.it/xpi4xs
@r_bash
Creating a magic file.

Hello,

I look to create a magic file that can be used with the command
file
to detect
School
data files.
School
data files always contain the string
SCHOOL
at offset 0.
My solution has been:

 1: 0 string SCHOOL School data


 2: !:mime School


compile


My problem is that when I run the file, the output is incorrect and I don't know why or where my blunder is. The output is:

 1: Warning: offset `�' invalid


 could not find any valid magic files!


https://redd.it/xq5ur4
@r_bash
Tmux Issues

So i have this command and when i launch it from tmux it executes more than 8 times. But on regular terminal screen it executes one time with no issues.

Basically what it does it monitors a file and if that file has changed it executes the program.

btcusd1.sh

And sends the data to Screen named minute.

So in tmux terminal i've tried.

bash -c 'inotifywait -m -e modify /tmp/btcusd_min.txt | while read -r dir; do tmux send-keys -t minute /files/ifvalueminute/btcusd1.sh ENTER; sleep 2; /files/ifvalueminute/ifvaluebtcusdminute.sh; break; done' &

And

inotifywait -m -e modify /tmp/btcusd_min.txt | while read -r dir; do $(tmux send-keys -t minute "/files/ifvalueminute/btcusd1.sh" ENTER; sleep 2; "/files/ifvalueminute/ifvaluebtcusdminute.sh"); done

This works with no issue in the regular terminal screen window. Not in tmux though

inotifywait -m -e modify /tmp/btcusd_min.txt | while read -r dir; do /files/ifvalueminute/btcusd1.sh; done &

Help or guidence is appreciated.

https://redd.it/xqf0bu
@r_bash
Check if each row in a file exists in a set of files, and if it does, update the last column in the first file with a value from the set of files

Hello!

There is a file A.tsv, which has these columns:
a b c d

With certain values in those columns. What I want to do, is for all rows, check if a row in this file exists in around 50 or so files that fall in different locations. The columns that those 50 files have are:
a b c d e f g h (first 4 are the same as above)


I'm trying to do:

for file in /my/location/50/files

for each row in A.tsv

if row exists in file1

A.tsv$5=corresponding value in file1$g

A.tsv$6=value in file1$h

if row exists in file2

A.tsv$7=file2$g

A.tsv$8=file2$h

And so on. Hence, the columns in A.tsv in which the data has to be stored will be changing for each file

The final file format for A.tsv is:

a b c d file1$g file1$h file2$g file2$h file3$g file3$h

and so on.

Now, I'm not sure what the best way to go about it is, and I'm having trouble coding it. Any help would be appreciated
Thank you!

https://redd.it/xqkakr
@r_bash
How to keep hold of another directory's path whilst using mv command?

Hi, I am getting used to bash but not there yet to be intermediate.

I would like to mv <my_folder> <destination> // #how do I find my destination file path whilst keeping this command alive? Use variables or am i missing something much simpler?

Now I could manually just find the file path of the directory I would like to move this folder to, however I might not an easier way of doing this in bash. What would you suggest?

https://redd.it/xqs2bn
@r_bash
One-liner Script Issue

Hello, this is my first post here. I have a question about noscripting, not specifically bash noscripting, but more general noscripting, which I hope is allowed here. I'm trying to get the api output using curl, but I need to read from a text file and pass it to the curl. Here's an example:

CURL command:curl --location --request GET '`https://conversations.messages.com/v1/messages/d909afaec435463aa433899dc00111b4`' --header 'Authorization: AccessKey 123AbcDefgh456' | python3 -mjson.tool

`d909afaec435463aa433899dc00111b4` is the ID of the message I want to retrieve and the result is as follows:

{"id": "d909afaec435463aa433899dc00111b4","name": "Alex Baldwin",}

And I have a msg.csv file with, say, 1,000 message IDs that I want to pass to curl. Here is my one-liner and the result.

$ cat msg.csv | while read line; do curl --location --request GET '`https://conversations.messages.com/v1/messages/$line`' --header 'Authorization: AccessKey 123AbcDefgh456' | python3 -mjson.tool; done

{"errors":[{"code":20,"denoscription":"Message not found"}]}

Any suggestions or pointers on what's wrong with the above one-liner would be greatly appreciated. Thank you very much.

https://redd.it/xqzop5
@r_bash
how to share a variable between function calls

hi guys, hope you all doing well. i have two functions f1 and f2

those look like this

f1 () {
var=0
f2 &
echo $var # prints 0
}

f2 () {
var=1
sleep 10
echo $var # prints 1
}


I want the echo in f1 to notice the change in the $var how can i do this while having f2 run in

the background (or at least have the sleep function run in the background while f1 continues running )

https://redd.it/xr212s
@r_bash
Examples of cool || non-standard || "simply insane" bash stuff?

Hi guys.

So recently I encountered following project: https://github.com/Jack000/Expose

Basically, it is bash noscript which makes sort of photo-based static blog/site. Got me curious about shell noscripting in general (although I am Linux user little less than 2 decades), because most of my "shell" noscripts are actually written in Ruby.

Do you have other examples where size or purpose of Bash/shell project is unusual or non-standard? Simply something where "sane" people would use a general dynamic programming language?

Also, I found out that Chet Ramey is sole maintainer. Anyone knows what he uses to develop bash? I assume it is not Mac. :P

Thank you.

https://redd.it/xr1vce
@r_bash
Viewing rows based on condition with awk ?

Hi,

I'm getting disk information using df -kh

Filesystem Size Used Avail Use% Mounted on
devtmpfs 5.7G 0 5.7G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 818M 7.0G 11% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 4.0G 2.6G 1.5G 64% /
/dev/sda2 4.0G 33M 4.0G 1% /media/sda2
/dev/sda5 509M 26M 483M 6% /media/sda5
/dev/sda6 18G 5.6G 12G 33% /media/sda6
/dev/sda3 4.0G 33M 4.0G 1% /media/sda3
tmpfs 1.6G 0 1.6G 0% /run/user/0

But I only want to see rows with storage space 30% or more used. How can I add condition to awk to get this information along with Headers

Expected Output

Filesystem Use%
/dev/sda1 64%
/dev/sda6 33%

How can I achieve this.

I have this so far but no header noscripts

df -kh | awk 'FNR == 6 {print $1" "$5}'
/dev/sda1 64%

Thank You

https://redd.it/xr1925
@r_bash
Different Oout of same Script ?

Hi,

I have the following noscript:

# Text Color
RED='\0330;31m'
BLUE='\033[0;34m'
NC='\033[0m'

if [ $reqSpace -lt $availableSpace
then
echo -e "${BLUE}Backup Possible, Log Data size $result MB, Free Space $availableSpace MB${NC}"
else
echo -e "${RED}Backup Not Possible, Log Data size $result MB, Free Space $availableSpace MB${NC}"
fi

This noscript when run once shows as expected result

https://preview.redd.it/p5ks73exgtq91.jpg?width=697&format=pjpg&auto=webp&s=649e36d797013fdf319c49319d58f0cf1eabe974

But when run with watch command does not parse the color variables.

https://preview.redd.it/ukm9f2d2htq91.jpg?width=701&format=pjpg&auto=webp&s=c83a7ae79f05902b3507725b6f9e259290f87de8

Any idea what's going on ?

https://redd.it/xravbu
@r_bash
Printing out command to terminal but not executing

Hi everyone. How do you print out a command to terminal without executing it in bash? For example, fzf's CTRL-R command will print it out to terminal before executing. How does one do that for arbitrary command?

https://redd.it/xrjtxf
@r_bash
exiftools, bad substitution error when trying to modify Content Create Date metadata

I used this command last year to remove the timezone from the "Content Create Date" field in a MP4 file. But now when I run this command, I get the following error...

$ exiftool -overwrite_original "-ContentCreateDate#<${ContentCreateDate; s/[+-].*//}" "file.mp4"

bash: -ContentCreateDate#<${ContentCreateDate; s/[+-].*//}: bad substitution

Not sure why this is the case now. Is there a way to get around this? Thanks in advanced.

https://redd.it/xrpux1
@r_bash
How to redirect stdin from an Interactive program to a File

I'm hoping to redirect the inpit of the python repl to a file

For example; would it be possible to set exec 3>&1 file denoscriptor somehow pipe stdin from the python repl to stdout into a log file?

my first attempt was doing exec 3>&1

and then python 2>&1 | >&3 tee -a SomeFile.log

The issue there is it's grabbing only the output from the repl and the output of the interpreter, now the i/o stream of reading the commands.

Any ideas are welcome..

https://redd.it/xrsdjv
@r_bash
FuzzyFind with Array

### I want to pipe the content of a directory(both files and directories) to fzf
this is my attempt so far. it's not functional
#!/usr/bin/env bash
content_dir="$HOME/DevRepo"
### this is my array
arr=$(ls "$content_dir")
fzf --preview "printf '%s\n' \"\${arr[{n}]}\""

### the expected output:
fzf the content of the specified directory
### the current ouput:
fzf the content of the current directory===>$PWD


Thx in advance

https://redd.it/xs1egg
@r_bash
Bash Newbie - How to do a foreach in bash?

Hello! This is my first time writing in bash, and I am having troubles understanding something basic. I would like to use a for each on a .txt file that consists of multiple commands to check machine information.

&#x200B;

In powershell, I would do something like this:

&#x200B;

$commandlist = get-content 'c:\commandlist.txt'
foreach ($command in $commandlist)
{Write-host "Running the command $command"
invoke-expression $command}

In bash, I am doing something like this:

#!/bin/bash
commandlist='commands.txt'

for command in $commandlist

do
echo "Running the command $command"
bash "$command"
done

however my output is like this:

Doing command commands.txt
MyHostName
IFconfigDetails

It seems that I am not looping correctly. What am I missing?

&#x200B;

I also read about this and this seems to work:
#!/bin/bash
cat commands.txt | while read -r line
do
echo "$line"
"$line"
done


Is this the only way I can do a foreach on a .txt file? thank you!

thank you in advance!

&#x200B;

PS. commands.txt contains only 2 lines

Hostname

IFconfig

https://redd.it/xs47yz
@r_bash
Reversing order of input

How do I do this? I want to take an input say, "Input Number One", and print back "One Number Input". I've been able to reverse the entire thing, but not the order. All online I've seen talks about reversing files.

https://redd.it/xs7dym
@r_bash
trying to find files by time signature

I want to find out the last two files added to the folder.

The files are .csv files that I scp from a remote machine to local.

I want to find and move these two files to a new folder.

To find the files I issued this command `:\~$ find . -maxdepth 1 -newermt "2022-09-30" | grep .csv | ls -al --time-style=+%D`

I expected the first pipe to print only the .csv files (which it did) then I added the second pipe thinking it will list the csv files according to their date. Which did not happen. What happend was the listing of all files and folders together with their date.

What did I do wrong?

&#x200B;

edit

This did succeed find . -maxdepth 1 -newermt "2022-09-30" | ls -al --time-style=+%D | grep .csv

https://redd.it/xs6yul
@r_bash
Piping multiple commands

So while I am interested in my concrete issue, I am also interested in how I can pipe multiple functions into one command in bash. What I want to do is compare two sorted files using git diff

If the lines are not sorted I can easilly do this using

git --no-pager diff --no-index oldfile.txt newfile.txt

However, when my files needs to be sorted, I run into issues. I do not know if / how I can properly pipe them into git diff without saving them first as tempfiles.

git --no-pager diff --no-index | sort oldfile.txt | sort newfile.txt

https://redd.it/xsdlqt
@r_bash
Working with Indexed Arrays

# Introduction

I decided to write this to share what I've learned about arrays in bash. It is not complete, and I expect to learn a lot, if I get any replies to this.

I also fully expect to screw up the formatting, and will probably be sweating profusely while trying to fix it. Please bear with me.

# What are Arrays

Arrays are a method for storing lists and dictionaries of information. There are two types of arrays supported by Bash, indexed and associative arrays. Indexed arrays have numeric indices and values associated with the indices. Associative arrays have key/value pairs. I'll be focusing on indexed arrays here.

With indexed arrays, you can store data in the array, iterate over the array, and operate on the each element in the array. For example:

cdickbag@dickship:~$ indarr=(apple orange banana)
cdickbag@dickship:~$ for fruit in "${ind
arr@}"; do echo "${fruit}"; done
apple
orange
banana
cdickbag@dickship:~$ echo "${indarr[0]}"
apple
cdickbag@dickship:~$ echo "${ind
arr1}"
orange
cdickbag@dickship:~$ echo "${indarr[2]}"
banana

This becomes more useful when you want to do things like iterate over text from a file, pattern match, and maybe go back to the previous line which contains unknown text, modify it, then write the contents to a file. If you work with lists of almost anything, arrays can be helpful to you.

# Finding Your Version of Bash

There are lots of different versions of bash in the wild. For example, macOS ships with bash 3.2.7, released in 2007. It lacks very handy features, like mapfile/readarray. Knowing your bash version is important to determine which features are available to you.

Find the bash version in your running shell.

echo $BASH
VERSION

Find the version of bash in your path.

bash --version

# Creating Indexed Arrays

There are a variety of ways to create indexed arrays.


## Manually

Declare an array.

cdickbag@dickship:~$ declare -a indarr

Simply start assigning values. You don't have to use `declare` to do this. Bash is very forgiving in that way.

cdickbag@dickship:~$ ind
arr=(apple orange banana)

If you have long lists you want to populate manually, you can reformat them so they're easier to read.

indarr=(
apple
orange
banana
)

## Automatically

Creating arrays by hand is tedious if you have a lot of objects. For example, if you want to pull data from a database, and store it in an array for processing, or want to read a text file into memory to process line by line, you would want to have some way to automatically read that information into an array.

### Using Loops and mapfile/readarray

In this example, I'll use a text file called `input.txt` with the following text.

line
onehasunderscores
line two has multiple words separated by spaces
linethreeisoneword

Reading a file into an array is easiest with mapfile/readarray. From the GNU Bash Reference Manual:

> Read lines from the standard input into the indexed array variable array, or from file denoscriptor fd if the -u option is supplied. The variable MAPFILE is the default array.

cdickbag@dickship:~$ mapfile -t indarr < input.txt

In older shells, such as bash 3.2.7, your options are more limited. Mapfile isn't available, so you need to do something else. A `while` loop works well here. Note the use of `+=`, which adds an element to an array. The use of parentheses is also important. Without them, `+=` concatenates a string to a variable.

cdickbag@dickship:~$ while read line; do ind
arr+=("${line}"); done < input.txt

But what if you want to populate an array from a process instead of a file? Process substitution makes this easy. Process substitution allows a process's input or output to be referred to using a filename. /dev/fd/63 is where bash will read from. Our input is the command ip addr show.