r_bash | Unsorted

Telegram-канал r_bash - r_bash

36

Credits: @r_channels & @reddit2telegram

Subscribe to a channel

r_bash

How do I finish a pipe early?

Hi.

I have this script that is supposed to get me the keyframes between two timestamps (in seconds). I want to use them in order to splice a video without having to reencode it at all. I also want to use ffmpeg for this.

My issue is that I have a big file and I want to finish the processing early under a certain condition. How do I do it from inside of an awk script? I've already used this exit in the early finish condition, but I think it only finishes the awk script early. I also don't know if it runs, because I don't know whether it's possible to print out some debug info when using awk. Edit: I've added print "blah"; at the beginning of the middle clause and I don't see it being printed, so I'm probably not matching anything or something? print inside of BEGIN does get printed. :/

I think it's also important to mention that this script was written with some chatgpt help, because I can't write awk things at all.

Thank you for your time.

https://pastebin.com/cGEK9EHH

#!/bin/bash
set -x #echo on
SOURCEVIDEO="$1"
START
TIME="$2"
ENDTIME="$3"

# Get total number of frames for progress tracking
TOTAL
FRAMES=$(ffprobe -v error -selectstreams v:0 -countpackets -showentries stream=nbreadpackets -of csv=p=0 "$SOURCEVIDEO")
if -z "$TOTAL_FRAMES" ; then
echo "Error: Unable to retrieve the total number of frames."
exit 1
fi

# Initialize variables for tracking progress
framesprocessed=0
start
frame=""
endframe=""
start
diff=999999
enddiff=999999

# Process frames
ffprobe -show
frames -selectstreams v:0 \
-print
format csv "$SOURCEVIDEO" 2>&1 |
grep -n frame,video,0 |
awk 'BEGIN { FS="," } { print $1 " " $5 }' |
sed 's/:frame//g' |
awk -v start="$START
TIME" -v end="$ENDTIME" '
BEGIN {
FS=" ";
print "start";
start
frame="";
endframe="";
start
diff=999999;
enddiff=999999;
between
frames="";
print "startend";
}
{
print "processing";
current = $2;

if (current > end) {
exit;
}

if (start
frame == "" && current >= start) {
startframe = $1;
start
diff = current - start;
} else if (current >= start && (current - start) < startdiff) {
start
frame = $1;
startdiff = current - start;
}

if (current <= end && (end - current) < end
diff) {
endframe = $1;
end
diff = end - current;
}

if (current >= start && current <= end) {
betweenframes = betweenframes $1 ",";
}
}
END {
print "\nProcessing completed."
print "Closest keyframe to start time: " startframe;
print "Closest keyframe to end time: " end
frame;
print "All keyframes between start and end:";
print substr(betweenframes, 1, length(betweenframes)-1);
}'

Edit: I have debugged it a little more and I had a typo but I think I have a problem with sed.

ffprobe -showframes -selectstreams v:0 \
-printformat csv "$SOURCEVIDEO" 2>&1 |
grep -n frame,video,0 |
awk 'BEGIN { FS="," } { print $1 " " $5 }' |
sed 's/:frame//g'

The above doesn't output anything, but before sed the output is:

38:frame 9009
39:frame 10010
40:frame 11011
41:frame 12012
42:frame 13013
43:frame 14014
44:frame 15015
45:frame 16016
46:frame 17017
47:frame 18018
48:frame 19019
49:frame 20020
50:frame 21021
51:frame 22022
52:frame 23023
53:frame 24024
54:frame 25025
55:frame 26026

I'm not sure if sed is supposed to printout anything or not though. Probably it is supposed to do so?

https://redd.it/1fxhx2n
@r_bash

Читать полностью…

r_bash

Getting the “logname” of a PID

Say I log into a box with account “abc”. I su to account “def” and run a script, helloworld.sh, as account “def”. If I run a ps -ef | grep helloworld, I will see the script running with account “def” as the owner. Is there a way I can map that back to the OG account “abc” to store that value into a variable?

Context: I have a script where I allow accounts to impersonate others. The impersonation is logged in the script’s log via the logname command, but I also have a “current users” report where I can see who’s currently running the script. I’d like the current users report to show that, while John is running the script, it’s actually Joe who’s impersonating John via an su.

I’ve tried ps -U and ps -u, but obviously, that didn’t work.

https://redd.it/1fx6n52
@r_bash

Читать полностью…

r_bash

How do I replace part of a line with the output of a variable?

Hi all,

I am writing a script that will update my IPv4 on my Wireguard server as my dynamic IP changes. Here is what I have so far:

#! /bin/bash

CurrentIP= curl -S -s -o /dev/null http://ipinfo.io/ip

Wireguard
IP= grep -q "pivpnHOST=" /etc/pivpn/wireguard/setupVars.conf |tr -d 'pivpnHOST='

if "$Current_IP" = "$Wireguard_IP" ;then
exit
else
#replace WireguardIP with CurrentIP in setupVars.conf
fi
exit 0


when trying to find my answer I searched through stack overflow and think I need to use awk -v, however; I don't know how to in this case. Any pointers would be appreciated.

https://redd.it/1fwxq9l
@r_bash

Читать полностью…

r_bash

How do i change the colors of that bar?

https://preview.redd.it/w3hvszskswsd1.png?width=1032&amp;format=png&amp;auto=webp&amp;s=5b31a400d02ea56e41adeb17da96327373068443

Hello, so i am using Chris Titus Tech's custom bash config but the colors dont fit with the pallete of my terminal (im making my system Dune themed).

Here is the .bashrc file: https://github.com/ChrisTitusTech/mybash/blob/main/.bashrc , i really tried to find where i can change those colors but couldnt find the line.
My ocd is killing me ;(

https://redd.it/1fwmwol
@r_bash

Читать полностью…

r_bash

what does the $ BY ITSELF mean in bash?

i understand it has something to do with variables but i googled it and it couldn't get a straight answer on what the $ alone means, so i wanted to ask here

thank you

https://redd.it/1fv4k1r
@r_bash

Читать полностью…

r_bash

Help creating script to email on boot

I am looking for help in creating a script to email me when a system boots or reboots. I have tried various online sources but nothing seems to work. I would like to have my Raspberry Pi running Raspbian email me when it boots. I have frequent power outages and want to be able to have the always on Pi let me know when it boots so that I know the power had gone out and I can check the logs for the duration.

Can anyone help me with this?

https://redd.it/1fupucl
@r_bash

Читать полностью…

r_bash

¿Cómo puedo realizar un script en Ubuntu que envíe correos electrónicos con el uso del disco desde la terminal?



Es una tarea nose si me pueden ayudar, ya investigue y no me sale nada alguien que me ayude porfa 🫠

https://redd.it/1ftz0pg
@r_bash

Читать полностью…

r_bash

TBD - A simple debugger for Bash

I played with the DEBUG trap and made a prototype of a debugger a long time ago; recently, I finally got the time to make it actually usable / useful (I hope). So here it is~ https://github.com/kjkuan/tbd

I know there's set -x, which is sufficient 99% of the time, and there's also the bash debugger (bashdb), which even has a VSCode extension for it, but if you just need something quick and simple in the terminal, this might be a good alternative.

It could also serve as a learning tool to see how Bash execute the commands in your script.

https://redd.it/1ft5r7h
@r_bash

Читать полностью…

r_bash

Unsure as to how I would pull this off

My Synology runs my OpenVPN server. I have the "keepalive 10 60" directive and 2 concurrent sessions / user account is allowed for, which means if the user accidentally reboots without disconnecting from the VPN first, they'll be reconnected upon the next logon.

My issue is that I want to solve this by leaving in the keepalive directive as is, but running some bash script as a cron job for when users reboot without disconnecting the VPN first.

Synology support would only say I have the following tools available for this:

netstat

procfs (/proc/net/nfconntrack or /proc/net/ipconntrack)

ip (iproute2)

tcpdump : yes

I'm very new to bash and Unix. I've been googling but I'm unsure as to how I could implement this. I'd appreciate some help, thanks

https://redd.it/1fq9p8n
@r_bash

Читать полностью…

r_bash

Aligning Two Columns for Files and Directories in Bash from Bottom-Up

[What I am trying to do](https://imgur.com/a/UjzUqXe)

I am working on a bash navigation script that displays files and directories side by side in two columns. However, I am stuck on aligning the output properly.
> I am hoping when its finished I can post it here and get some feedback. The script has some nice abilities which I have wanted for CLI interaction.

### The Problem:
When you look at the screenshot, the outputs don't align properly at the bottom. I need the files on the left and directories on the right, and both should start from the bottom and grow upwards.

### Why it’s Important:
The main issue I want to solve is that when there are many files, you have to scroll up to see the directories, which defeats the ease of navigation. I want the directories and files to always stay visible, both aligned properly from the bottom, like in my picture.

The main display:

display_items_fileNav() {
# Get terminal height using tput
term_height=$(tput lines)

# Get the outputs of the existing functions
file_output=$(display_files_only) # Get the file output
dir_output=$(display_dir_only) # Get the directory output

# Determine how many lines are used for each output
file_lines=$(echo "$file_output" | wc -l)
dir_lines=$(echo "$dir_output" | wc -l)

# Calculate the maximum of the file and directory lines to ensure both align from the bottom
max_lines=$(( file_lines > dir_lines ? file_lines : dir_lines ))

# Set the start position for both columns so they align at the bottom
start_position=$((term_height - max_lines - 3)) # Reserve 3 lines for the prompt and buffer

# Set the column width for consistent spacing; adjust based on the longest file name
file_column_width=40 # Adjust this as needed
dir_column_width=30 # Adjust this as needed

# Pad the top so the output aligns to the bottom of the terminal
tput cup $start_position 0

# Combine the outputs, aligning the columns side by side
paste <(echo "$file_output" | tail -n "$max_lines") <(echo "$dir_output" | tail -n "$max_lines") | while IFS=$'\t' read -r file_line dir_line; do
printf "%-${file_column_width}s %s\n" "$file_line" "$dir_line"
done

# Print category titles (Files and Directories) at the top of each column
echo -e "${NEON_GREEN}Files:${NC}$(printf '%*s' $((file_column_width - 5)) '')${NEON_RED}Directories:${NC}"

# Move cursor to the prompt position and show the prompt
tput cup $((term_height - 1)) 0
}

**The output functions** (I did this because I could not get the reverse order to properly display).




display_dir_only() {
# Get terminal height using tput
term_height=$(tput lines)

# Get directories and files
dirs=($(ls -d */ 2>/dev/null)) # List directories
files=($(ls -p | grep -v /)) # List files (excluding directories)

# Reverse the order of directories and files
dirs=($(printf "%s\n" "${dirs[@]}" | tac)) # Reverse the directories array
files=($(printf "%s\n" "${files[@]}" | tac)) # Reverse the files array

dir_count=${#dirs[@]} # Count directories
file_count=${#files[@]} # Count files
total_count=$((dir_count + file_count)) # Total number of items (directories + files)

# Calculate how many lines we need to "pad" at the top
padding=$((term_height - total_count - 22)) # 6 includes prompt space and a clean buffer

# Pad with empty lines to push content closer to the bottom
for ((p = 0; p < padding; p++)); do
echo ""
done

# Skip file display but count them for numbering
reverse_index=$total_count # Start reverse_index from the total count (dirs + files)

# First, we skip file output but count files
reverse_index=$((reverse_index - file_count))

# Then, display directories in reverse order

Читать полностью…

r_bash

Styling preference for quoting stuff in comments

In shell scripts, I have lots of comments and quoting is used for emphasis. The thing that is being quoted is e.g. a command, a function name, a word, or example string. I've been using backticks, double, single quote chars all over the place and looking to make it consistent and not completely arbitrary. I typically use double quotes for "English words". backticks for commands (and maybe for functions names), single quotes for strings.

E.g. for the following, should funcA and file2 have the same quotes?

# "funcA" does this, similar to cp file file2. 'file2' is a file

Is this a decent styling preference or there some sort of coding style code? Would it make sense to follow this scheme in other programming languages? What do you do differently?

Maybe some people prefer the simplicity of e.g. using "" everywhere but that is a little more ambiguous when it comes to e.g. keywords or basic names of functions/variables.

Also, I used to use lower case for comments because it's less effort, but when it's more than a sentence, the first char of the second sentence must be capitalized. I switched to capitalizing at the beginning of every comment even if it's just one sentence and I kind of regret it--I think I still prefer # this is comment. Deal with it because I try to stick with short comments anyway. I never end a comment with punctuation--too formal.

Inb4 the comments saying it literally doesn't matter, who cares, etc. 🙂

https://redd.it/1fpglmg
@r_bash

Читать полностью…

r_bash

Is there an "official" Usage syntax syntax?

With getopt or getopts I see options treated as optional. That makes sense to me, but making people remember more than 1 positional parameter seems likely to trip up some users. So, I want to have a flag associated with parameters.

Example a with optional options:

Usage: $0 -x <directory> -o <directory> <input>

Is this the same, with required options:

Usage: $0 -x <directory> -o <directory> <input>

Any other suggestions? Is that how I should indicate a directory as an option value?

https://redd.it/1fo7eom
@r_bash

Читать полностью…

r_bash

Use variable inside braces {}

for NUM in {00..10}; do echo $NUM; done
outputs this, as expected
00
01
02
...
10

However MAX=10; for NUM in {00..$MAX}; do echo $NUM; done
produces this
{00..10}

What am I missing here? It seems to expand the variable correctly but the loop isn't fucntioning?

https://redd.it/1fnro7f
@r_bash

Читать полностью…

r_bash

Anyway to Tail CLI Terminal output ?

Hi,

I have the below script which runs a loop and display on the output.

What I want to do is just see the last 5 lines on the terminal, how can I do this ?

I know about tail but have not found an example where tail is used for Terminal output..

for i in $(seq 1 10);
do
echo $i
sleep 1
done

https://redd.it/1fngur2
@r_bash

Читать полностью…

r_bash

Command into remote system works from CLI but not from Bash script ?

Hi,

I'm running the below command from my system and it works.

root@bai-ran-cluster-master0# ssh -q bai-ran-cluster-worker1 ssh -q 10.42.8.11 '/tmp/SWWW/a.sh' >> prechecks.log
root@bai-ran-cluster-master0# cat prechecks.log
HELLO
HELLO
HELLO

This works fine, but when I add this command into a Bash file and run it from my system, it does not work.

Code in Bash script

worker="bai-ran-cluster-worker1"
ipaddr=10.42.8.11

ssh -q $worker ssh -q $rruip '/tmp/SWWW/a.sh' | tee -a prechecks.log
cat prechecks.log

No error, but nothing happens.

The remote OS is Yocto, and the shell is /bin/sh

I tried the below but its not working, anything else I can check ?

ssh -q $worker ssh -q $rruip 'sh /tmp/SWWW/a.sh' | tee -a prechecks.log

ssh -q $worker ssh -q $rruip '/bin/sh /tmp/SWWW/a.sh' | tee -a prechecks.log

Below is the Shell information from the OS

root@benetelru:~# echo $0
-sh
root@benetelru:~# echo $SHELL
/bin/sh
root@system:~# cat /etc/shells
# /etc/shells: valid login shells
/bin/sh

https://redd.it/1fn1eo5
@r_bash

Читать полностью…

r_bash

Shell scripting projects for 1 yoe helpdesk to get promoted to a new role?

What could that be?(in new company-switch)

https://redd.it/1fx6m6a
@r_bash

Читать полностью…

r_bash

Rate my bash script
https://drive.google.com/file/d/1q7DVciDMbCrO4YyV6U4S4StgZ2ll5MFu/view

https://redd.it/1fx3pbb
@r_bash

Читать полностью…

r_bash

what does "echo $$" do?

so question, what does "echo $$" do?

some people are saying that it prints the PID of the current instance of the shell you are on

other people are saying that it prints the PID of "a" or "the" script that's currently executing?

which is it? or is it both?

thank you

https://redd.it/1fwp9lf
@r_bash

Читать полностью…

r_bash

ffmpeg bash question - How to create bash to cut out intro and outro from multiple videos

I'm trying to cut the intro and outro out of multiple videos with different beginning times and different end times using ffmpeg

The code I know of that will do this from a single video is

"ffmpeg -ss 00:01:00 -to 00:02:00 -i input.mkv -c copy output.mkv"

But I don’t know how to tell ffmpeg to do this for multiple videos and to do this with different beginning times and different ending times so that it will do one right after the other

I am new to all of this so if anyone could help me, that would be amazing, thank you

https://redd.it/1fwjvy8
@r_bash

Читать полностью…

r_bash

weird behavior from a bash line

hi there,

I wonder why :


find /home/jess/* -type f -iname "*" | wofi --show=dmenu | xargs -0 -I vim "{}"


returns


xargs: {}: No such file or directory

why the find arg isn't passed to vim ?
thx for help guys and girls

https://redd.it/1fuq7r2
@r_bash

Читать полностью…

r_bash

How can I make a script in Ubuntu that sends emails with disk usage from the terminal?

It's a task I don't know if you can help me, I've already investigated and nothing comes up, someone help me please 🫠

https://redd.it/1fuis7w
@r_bash

Читать полностью…

r_bash

Output a section of stdout

Imagine the output from wpctl status:

...
- Some info
- Some info

Audio:
- Some info
- ...
- Some info

Video:
- Some info
...

I want to get the block of output under "Audio", so the output between "Audio" and "Video". Is there a efficient way to achieve this, e.g. with sed or awk... or grep... ?

https://redd.it/1ftkvra
@r_bash

Читать полностью…

r_bash

Automation Shell Script for CTFs

Here is the code: https://codefile.io/f/l9LmkIdHZK

I am new to shell scripting; I just know few Linux Commands. I was hoping if someone could check the code I have written and help me improve my shell script skills. I am trying to build an automation script for CTF's. I want to save my time by not executing all same commands again and again for every target.

I know there's a lot of if statement. I want to know how to make this more effective and faster.

https://redd.it/1ft3dsf
@r_bash

Читать полностью…

r_bash

with correct numbering
for ((i = 0; i < dir_count; i++)); do
if [ $reverse_index -eq $current_selection ]; then
printf "${NEON_RED}%2d. %s${NC}/ <---" "$reverse_index" "${dirs[i]}"
else
printf "${NEON_RED}%2d. %s${NC}/" "$reverse_index" "${dirs[i]}"
fi
reverse_index=$((reverse_index - 1))
echo ""
done echo ""}




display_files_only() {
# Get terminal height using tput
term_height=$(tput lines)

# Get directories and files
dirs=($(ls -d */ 2>/dev/null)) # List directories
files=($(ls -p | grep -v /)) # List files (excluding directories)

# Reverse the order of directories and files
dirs=($(printf "%s\n" "${dirs[@]}" | tac)) # Reverse the directories array
files=($(printf "%s\n" "${files[@]}" | tac)) # Reverse the files array

dir_count=${#dirs[@]} # Count directories
file_count=${#files[@]} # Count files
total_count=$((dir_count + file_count)) # Total number of items (directories + files)

# Calculate how many lines we need to "pad" at the top
padding=$((term_height - total_count - 24)) # 6 includes prompt space and a clean buffer

# Pad with empty lines to push content closer to the bottom
for ((p = 0; p < padding; p++)); do
echo ""
done

# Skip directory display but count them for numbering
reverse_index=$total_count # Start reverse_index from the total count (dirs + files)

# First, skip directory display but count directories
# The reverse_index skips by the dir_count, so it correctly places files after.
reverse_index=$((reverse_index))

# Then, display files with correct numbering (including counted but hidden directories)
for ((i = 0; i < file_count; i++)); do
if [ $reverse_index -eq $current_selection ]; then
printf "${NEON_GREEN}%2d. %-*s${NC} <---" "$reverse_index" $COLWIDTH "${files[i]}"
else
printf "${NEON_GREEN}%2d. %-*s${NC}" "$reverse_index" $COLWIDTH "${files[i]}"
fi
reverse_index=$((reverse_index - 1)) # Decrease the reverse_index each time
echo ""
done

# Add an empty line for clean separation between the listing and the prompt
echo ""
}


### Any ideas or suggestions on how to fix this alignment issue?

https://redd.it/1fq69nt
@r_bash

Читать полностью…

r_bash

Any simple way to remove ALL escape sequences (except \r\n) from a screen log?

I am logging the SSH connection within a screen session. I want to parse the log, but all methods in the internet only get me so far. I get garbage letters written to the next line like:


;6R;6R;2R;2R;4R;4R24R24


There is not even any capital R in the log, neither a 6. And they are not just visually glitched to the next line, pressing enter will try to execute this crap.


This garbage comes from loggin in to a MikroTik device via SSH. Unfortunately I need to parse this code in a predictable way. Using cat on the logfile without filtering prints the colors correctly, but this even prints this garbage to the new line. I have absolutely 0 plan, where this comes from. Any idea, how one could get a screen log, that is clean, or a way to parse it in bash in a clean way? I would prefer something lightweight, that is available in typical linux distros, if possible.

https://redd.it/1fpygiu
@r_bash

Читать полностью…

r_bash

RapidForge: Create Web Apps and Automate Tasks with Bash

I've been working on a project called RapidForge that makes it easier to create custom Bash scripts for automating various tasks. With RapidForge, you can quickly spin up endpoints, create pages (with dnd editor) and schedule periodic tasks using Bash scripting. It’s a single binary with no external dependencies so deployment and configuration are a breeze.

RapidForge injects helpful environment variables directly into your Bash scripts, making things like handling HTTP request data super simple. For example, when writing scripts to handle HTTP endpoints, the request context is parsed and passed as environment variables, so you can focus on the logic without worrying about the heavy lifting.

Would love to hear your thoughts or get any suggestions on how to improve it

https://redd.it/1fpc1po
@r_bash

Читать полностью…

r_bash

A script that will delete all subdirectories except those which contain pdf or mp3 files

Let's say I have a directory "$my_dir". Inside this directory there are various subdirectories, each containing files. I'd like to have a script which, when executed, automatically removes all subdirectories which do not contain pdf or mp3 files. On the other hand, the subdirectories which do contain some mp3 or pdf files should be left untouched. Is this possible?

https://redd.it/1fnxi3i
@r_bash

Читать полностью…

r_bash

If condition to compare time, wrong result ?

Hi,

I have the below script which is to check which system uptime is greater (here greater refers to longer or more elapsed).

rruts=$(ssh -q bai-ran-cluster-worker1 ssh -q 10.42.8.11 'uptime -s')
rrepoch=$(date --date "$rruts" +"%s")

sysuts=$(uptime -s)
sysepoch=$(date --date "$sysuts" +"%s")

epochrru=$rrepoch
echo "RRU $(date -d "@${epoch
rru}" "+%Y %m %d %H %M %S")"

epochsys=$sysepoch
echo "SYS DATE $(date -d "@${epoch
sys}" "+%Y %m %d %H %M %S")"

currentdate=$(date +%s)
echo "CURRENT DATE $(date -d "@${current
date}" "+%Y %m %d %H %M %S")"

rrudiff=$((currentdate - epochrru))
sysdiff=$((currentdate - epochsys))

echo "RRU in minutes: $(($rrudiff / 60))"
echo "SYS in minutes: $(($sysdiff / 60))"

if "$rrudiff" > "$sysdiff"
then
echo "RRU is Great"
else
echo "SYS is Great"
fi

The outcome of the script is

RRU 2024 09 20 09 32 16
SYS DATE 2024 02 14 11 45 38
CURRENT DATE 2024 09 23 14 11 10
RRU in minutes: 4598 <--- THIS IS CORRECT
SYS in minutes: 319825 <--- THIS IS CORRECT
RRU is Great <--- THIS IS WRONG

As in the result :

RRU has been up since 20 Sep 2024

SYS has been up since 14 eb 2024

So how is RRU Great, while its minutes are less.

Or what is wrong in the code ?

Thanks



https://redd.it/1fnljy5
@r_bash

Читать полностью…

r_bash

It's always the Mentally Ill Girls(mostly Cosers)

https://redd.it/1fn52q1
@r_bash

Читать полностью…

r_bash

Convert all directories in every command to absolute ones in the bash history

Currently I use fzf with a custom keybinding Ctrl+R to search commands in my bash history. For example:

$ cd folder <Ctrl+R>

should allow me to select cd ./long/path/to/folder. This wonderful way helps me quickly navigate to a directorie or even a file, which is very close to what zoxide does. The only drawback is that ./long/path/to/folder must be an absolute path.

To fix this, I modified the cd command:

cd() {
if [ -d "$1" ]; then
local dir=$(realpath "$1")
builtin cd "$dir" && history -s "cd $dir"
else
builtin cd "$1"
fi
}

This works, but I want it to work for vim and other commands that use directories too. Is there a better way to do this?

https://redd.it/1fmy6n8
@r_bash

Читать полностью…
Subscribe to a channel