r_bash – Telegram
New to bash with a simple question.

Say I’m already in a folder and just want to see all files that are 4 character long in the name. What command can do that?

https://redd.it/xladhp
@r_bash
Working on browser agnostic bookmarks, shellcheck no help.

This seems to work fine.

https://paste.debian.net/1254702/

But shellcheck complains about egrep -o use grep -E instead. However if I do that any selection I make tries to open a local file. eg..

https://i.imgur.com/M3OoBmf.jpg

Ignore shellcheck? I mean it works as I want, I think. Haven't tested spaces in denoscription much, which I'd very much like to have, but it does seem to work as is.

https://redd.it/xlbtgc
@r_bash
how to validate options in noscript.

​

So I have noscript that takes file from user with option -y or -n and -d is required noscript runs ./example.sh -d file -y or -n variable. I made case getops "d:up" option`

​



Correct way running noscript: ./example.sh -d file -y or -n variable.

file="" -- that user inputs file

I need to do validation on if the user did not pass file in for example

./example.sh -y --- it should give error "must provide file"
./example.sh -n --- it should give error "must provide file"

./example.sh -d --- it should give error "must provide file"
./example.sh -y -n --- it should give error "must provide file"

i came up with this but its not working #checking if the file is provided

if $file -eq 1 ; then echo "No file provided"
exit 1
fi
# Does the file exist which works for checking if the file exists but it only check if the file in there
if [ ! -f $file ]; then
echo "Error: The file $file does not exist."
exit 1 fi

i already have validations for everything else but i need figure out how to make sure if the person doesnt provide file like examples above if gives out a error

https://redd.it/xm36mm
@r_bash
How to add all the values up?

So I want to add all the values up for each species, and get something like

adinocephala 1

atla 7

alicastrum 30

etc for my entire list, but I'm just a little lost on how to do it easily without having to grep each individual species and add up the values with a for loop. That would take absolutely forever and I know there's got to be an easier solution, but I'm not finding anything good on google.

https://preview.redd.it/ozyojuv8rqp91.png?width=258&format=png&auto=webp&s=11eeadfd510c2887ec4a12fe9a77ee19af42d8d0

Any help would be appreciated!!

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