r_bash – Telegram
Convert / compress all videos in folder

Hey guys,
i have a bunch of videos (197GB), mixed up as MKV, M4V, MP4, WEBM... different resolutions (1080, 720, ...).

My goal is to compress them all as a one-line format.

480p should be the final resolution, what could you recommend as file type/codec?

x265, webm, mp4... ?

It should be the best quality/best file size in the end :)

Thank you!

https://redd.it/xmq2uz
@r_bash
!! in if statement

I can't seem to get this noscript working and I don't know how to google this one.

I want to run multiple commands, for every command I want to output that it failed without repeating the if statement over an over again, so I made the if statement at the beginning.

checklastcommand() {
if $? -eq 0 ; then
echo "!! OK"
else
echo "!! failed" >> ~/Desktop/testlog
fi
}

and i.E.

echo "Test"
checklastcommand

the output is always

!! OK

or

!! failed

instead of

echo "test" failed

https://redd.it/xmxreg
@r_bash
This vim/fzf alias in my bashrc isn't working, thoughts?

Hi ~

I need a alias to quickly type and open up a file in vim via fzf. I have gotten to the point where I can do this from the terminal via typing vim $(fzf). However, if I add an alias like ...

// bashrc file
alias vimf="vim $(fzf)"

... then this results in some weird behavior where opening a new shell causes fzf to automatically come up. Also typing vimf has some really weird behavior as if it's opening up some cache'd file.

What is the proper way here to create this alias?

https://redd.it/xn7sln
@r_bash
Help required improving an SFTP RPA noscript

Hey Folks,

Newbie Bash Scriptor here, I was able to collate a SFTP RPA for our setup. However there seems to be two issues that I have detected with the logic for which I have been unable to find better solutions for

1. First is the timestamp variable - It is updating the file metadata for tracking the latest files added to the folder, the issue that I have noticed(edge case)is if multiple files are written in the same minute marker as the noscript is running, it seems to miss out on files, I think tracking it to even keep track for seconds should be fine. (Rsync would have been better but the remote site actually moves the file away on a per second basis)
2. Verbosity of the SFTP piece - Is there a way to write the SFTP output to the file as well, right now it is only able to redirect the output of the noscript but not the SFTP module, the SFTP module showcases the progress bars that I want to feed into my list but have been unable to do so far.

​

#!/bin/bash
#
# Use batch file with SFTP shell noscript
#
##################################################################
# Create SFTP batch file
tempfile=$(mktemp -t sftptmp.XXXX)
count=0
archive="/SFTP/ARCHIVE/"
currenttime=$(date)
trap "/bin/rm -f $tempfile" 0 1 15

if [ $# -eq 0 ] ; then
echo "Usage: $0 user host path_to_src_dir remote_dir" >&2
exit 1
fi

# Collect User Input
user="$1"
server="$2"
remote_dir="$4"
source_dir="$3"
identity="$5"

timestamp="$source_dir/.timestamp"

# Without source and remote dir, the noscript cannot be executed
if [[ -z $remote_dir ]] || [[ -z $source_dir ]]; then
echo "Provide source and remote directory both"
exit 1
fi

echo $currenttime
echo "cd $remote_dir" >> $tempfile
# timestamp file will not be available when executed for the very first time
if [ ! -f $timestamp ] ; then
# no timestamp file, upload all files
for filename in $source_dir/*
do
if [ -f "$filename" ] ; then
# Place the command to upload files in sftp batch file
echo "put -P \"$filename\"" | tee -a $tempfile
# Increase the count value for every file found
count=$(( $count + 1 ))
fi
done
else
# If timestamp file found then it means it is not the first execution so look out for newer files only
# Check for newer files based on the timestamp
for filename in $(find $source_dir -newer $timestamp -type f -print)
do
# If found newer files place the command to upload these files in batch file
echo "put -P \"$filename\"" | tee -a $tempfile
# Increase the count based on the new files
count=$(( $count + 1 ))
done
fi
# If no new files found the do nothing
if [ $count -eq 0 ] ; then
echo "$0: No files require uploading to $server" | tee -a /var/log/rpa
exit 1
fi
# Place the command to exit the sftp connection in batch file
echo "quit" | tee -a $tempfile
echo $tempfile
echo "Synchronizing: Found $count files in local folder to upload."
# Main command to use batch file with SFTP shell noscript without prompting password
sftp -i $identity -P 10022 -b $tempfile "$user@$server" | tee -a /var/log/rpa
echo "Done. All files synchronized up with $server"


# Create timestamp file once first set of files are uploaded
touch $timestamp


# Remove the sftp batch file
rm -f $tempfile
exit 0

https://redd.it/xnzqvu
@r_bash
Vertical to Horizontal Output in Bash ?

Hi,

I have a command giving the following output which is stored as-is in a text file and then displayed on screen:

Connected to 172.16.1.6.

Port Power(dBm)
------------------
0 31.10
1 31.06
2 27.75
3 28.60

Connected to 172.16.1.7.

Port Power(dBm)
------------------
0 30.51
1 30.92
2 30.06
3 28.85

Connected to 172.16.1.8.

Port Power(dBm)
------------------
0 30.95
1 30.29
2 30.18
3 32.07

How can I format this output into a table with 3 columns horizontally as follows:

Connected to 172.16.1.6. | Connected to 172.16.1.7.| Connected to 172.16.1.8.

Port Power(dBm) | Port Power(dBm) | Port Power(dBm)
------------------ | ------------------ | ------------------
0 31.10 | 0 30.51 | 0 30.95
1 31.06 | 1 30.92 | 1 30.29
2 27.75 | 2 30.06 | 2 30.18
3 28.60 | 3 28.85 | 3 32.07

Thanks..

https://redd.it/xod9s0
@r_bash
Getting a substring in PS1?

Could anybody give me some hits as how to elegantly get a substring some elements in my prompt?

For instance I'd like just the first letter of the username and host:

Host: Bar
User: Foo

F@B:~$

Do I have to stick a full cut or awk command in PS1 or is there a slick way to get a substring of \u and \h ?


Thanks

https://redd.it/xofz2z
@r_bash
Struggling with a oneliner for disk usage

Hey All,

Hoping someone could help, So i have this oneliner which calculates disk space and gives a nice output in terms of largest directories and largest files, all you have to do is substitute in a dir path.

FS='/data';NUMRESULTS=20;resize;clear;date;df -h $FS; echo "Largest Directories:"; du -x $FS 2>/dev/null| sort -rnk1| head -n $NUMRESULTS| awk '{printf "%d MB %s\n", $1/1024,$2}';echo "Largest Files:"; nice -n 19 find $FS -mount -type f -ls 2>/dev/null| sort -rnk7| head -n $NUMRESULTS|awk '{printf "%d MB\t%s\n", ($7/1024)/1024,$NF}'

I am looking to expand this to include where the most files originate as a whole count and then as a count in say the top 10 directories and provide this as a listing along with another expansion with also the number of inodes being used per directory.

​

Any help would be greatly appreciated.

https://redd.it/xoj51q
@r_bash
Leading Characters In Auto-Generated Directory Names

I have a noscript that takes a large directory of files and moves those files into auto-numbered subdirectories, 1000 at a time. It works fine.

**My question is:** Now that I have over 100 sub-directories created when I do this, how do I add leading zeroes to one- and two-digit numbers? e.g., 001, 002, 012, 037, etc.

Existing noscript is below. Your expertise is most appreciated.

#!/bin/bash
# outnum generates the name of the output directory
outnum=1
# n is the number of files we have moved
n=0

# Go through all JPG files in the current directory
for f in *.jpg; do
# Create new output directory if first of new batch of 1000
if [ $n -eq 0 ]; then
outdir=$outnum
mkdir $outdir
((outnum++))
fi
# Move the file to the new subdirectory
mv "$f" "$outdir"

# Count how many we have moved to there
((n++))

# Start a new output directory if we have sent 1000
[ $n -eq 1000 ] && n=0
done

https://redd.it/xomgnz
@r_bash
[Help] Parsing password to openssl OCSP

Hello everyone,

Hope everyone is doing great.

I'm implementing an OCSP server in my job and I ran into an issue.

While running \`openssl req/ca\` we can use the flag \`-passin\` or \`-passout\`, that flag doesn't exist in \`openssl ocsp\` version 1.1.1 only on version 3.

​

**Question is:**

Is there a way for me to parse the password via STDIN?

This is the command I'm trying to run:

openssl ocsp -index intermediate/index.txt -port 8080 -rsigner ${INTERMEDIATE_CRT_PATH} -rkey ${INTERMEDIATE_KEY_PATH} -CA ${INTERMEDIATE_CRT_PATH} -text

This will prompt:

Enter pass phrase for <cert_path>:

I have tried all the shell or bash magic I know and could find, tried getting the PID and redirecting to /proc/pid/fd/0 with no luck.

&#x200B;

Any help would be appreciated. Thanks !

https://redd.it/xomaxz
@r_bash
How to print to bad sector count when disk is inserted?

(using BASH version 5.1.16 on Ubuntu 22.04)

&#x200B;

Hello,

I would like to see the bad sector count of a drive when it is attached to the computer (not mounted). For example, I can run:

sudo smartctl -a /dev/sdb | grep ReallocatedSectorCt

&#x200B;

and the output I get is something like:

&#x200B;

5 ReallocatedSectorCt 0x0033 154 154 140 Pre-fail Always - 386

&#x200B;

When I hotswap a SATA drive, I would like the command run again and outputted to the display.

Any help would be appreicated

https://redd.it/xoqz7i
@r_bash
Saving a folder path to a variable

Please excuse such simple question newbie to bash :)

Pretty much what the noscript saids.
Looking to store a folder path into a variable to be later used in the noscript.
The noscript asks a few questions and based on your answer, nmap runs a scan against a set of IPs from File A or File B and then save the output to a folder.

There will be multiple different scan in which the output file is name different but all save to the same folder.

I’ve tried a few different method and get stuck with its Outputting everything in the file (CAT) or tells me the individual IPs in the files “no such file or directory xx.xx.xx.xx”

Few things I’ve tired:
Results= $Path://home/kali/Desktop/Results

FileA= $Path://home/kali/Desktop/FileA.txt

This is met with “No such file or directory”

Or
FileB= cat /home/kali/Desktop/FileB.txt

Thank you for any input!

https://redd.it/xowj9l
@r_bash
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