r_bash – Telegram
"
cat $noscriptname

exit
;;

#Help
h)
usage >&2
exit
;;
#Remove commands
r)
if [ "$#" -eq 2 ]; then
sed -i "/^$2/d" $COMMAND_FILE
sed "s/^[0-9]* //" $COMMAND_FILE | awk '{print NR-1 " " $0}' | sponge $COMMAND_FILE
cat $COMMAND_FILE
elif [ "$#" -eq 1 ]; then
command_delete_flag="y"
read -p "Are you sure that you want to delete all the commands? [Y/n] " command_delete_flag
if [[ "$command_delete_flag" != "n" ]]; then
printf "">$COMMAND_FILE
echo "All commands have been removed"
fi
else
# read -p "Select the command to erase: $(echo '') "$(sed 's/^\([A-Za-z0-9]\) /\1) /' $COMMAND_FILE )"; echo 'Option: ')" option;
# sed -i "/^$option/d" $COMMAND_FILE
# sed -i '/^$/d' $COMMAND_FILE
# sed "s/^[0-9]* //" $COMMAND_FILE | awk '{print NR-1 " " $0}' | sponge $COMMAND_FILE
#
fi
exit
;;
l)#PERF:
cat $COMMAND_FILE
exit
;;
#PERF:
b)
if [[ -n $2 ]]; then
number_of_lines=$(wc -l $COMMAND_FILE | cut -c 1)
if [[ $number_of_lines > $2 ]]; then
echo $2 > $STACK_FILE
tail -n "$2" "$COMMAND_FILE" | sponge "$COMMAND_FILE"
cat $COMMAND_FILE
sed "s/^[0-9] \(.*\)/ \1/" $COMMAND_FILE | awk '{print NR-1 $s}' | sponge $COMMAND_FILE
cat $COMMAND_FILE
echo The size has been changed to $2 , the first commands have been erased
cat $COMMAND_FILE
else
echo $2 > $STACK_FILE
echo Stack size changed to: $2
fi
else
echo Error, no argumets given
fi
exit
;;
m)#PERF:
if (($# == 3)); then
sed -i --expression "s:^$2.*:$2 $3:" $COMMAND_FILE
cat $COMMAND_FILE
exit
fi
if (( $# == 1 )); then
echo "These are the current slots"
cat $COMMAND_FILE
read -p "Which slot would you like to edit? " SLOT
fi
if (( $# == 2 )); then
SLOT=$2
echo "slot = $SLOT"
fi
read -p "Introduce the new command: " COMMAND
sed -i --expression "s:^$SLOT.*:$SLOT $COMMAND:" $COMMAND_FILE
cat $COMMAND_FILE
exit
;;
s)#PERF:
if [ "$#" == 1 ]; then
cat $COMMAND_FILE
read -r -p "Please select the two commands that you want to switch: " command1 command2
elif (($# == 3 )); then
command1=$2
command2=$3
else
usage >&2
exit 1
fi
first_command=$(grep "^$command1" $COMMAND_FILE | sed "s/^[0-9]/$command2/")
second_command=$(grep "^$command2" $COMMAND_FILE | sed "s/^[0-9]/$command1/")
sed -i "s/^${command1}.*/$second_command/" $COMMAND_FILE
sed -i "s/^${command2}.*/$first_command/" $COMMAND_FILE
echo "Commands $command1 and $command2 have been swaped"
cat $COMMAND_FILE
exit 1
;;
e)
if [ "$#" == 2 ]; then
FILETOLOAD=$SCRIPT_DIR$2
echo "The file to load is: $FILETOLOAD"
number_of_lines_original_file=$(wc -l $COMMAND_FILE | cut -c 1)
number_of_lines_new_file=$(wc -l $FILETOLOAD | cut -c 1)
if [[ $number_of_lines_new_file -ne $number_of_lines_original_file ]]; then
echo $number_of_lines_new_file > $STACK_FILE
echo The size has been changed to $number_of_lines_new_file
fi
awk '{print NR-1 " " $s}' $FILETOLOAD > $COMMAND_FILE
echo "The new command file is as follows: "
cat $COMMAND_FILE
exit
else
usage >&2
exit
fi
;;
x)
ls $SCRIPT_DIR
exit
;;
d)
echo "Quick command directory: $QC_DIR
Command file: $COMMAND_FILE
File with the size of the stack: $STACK_FILE
Script directory: $SCRIPT_DIR" | column --table --separator ":"
exit
;;
u)
echo "The current stacksize is: $(cat $STACK_FILE)"
exit
;;
?)
usage >&2
exit 1
;;
esac
done
shift "$((OPTIND -1))"

if [ "$#" -eq 1 ]; then
SLOT=$1
EXECUTE_COMMAND=$(grep ^"$1" $COMMAND_FILE | cut --complement -c 1,2)
$EXECUTE_COMMAND
fi
exit

https://redd.it/13lgglt
@r_bash
Help with understanding awk code

Hey guys,

I'm quite new to bash still and I found a piece of code online which removes new lines from a FASTA text file but I was hoping someone could help me break it down and understand how it's working. Here is the code:

awk '/^>/ {printf("%s%s\t",(N>0?"\n":""),$0);N++;next;} {printf("%s",$0);} END {printf("\n");}'

Thanks in advance :)

https://redd.it/13lry9m
@r_bash
Need help with inner loop from a while read with multiple values

SRC FILE:

hostname WWN1 WWN2 WWN3 WWN4
hostname WWN1 WWN2 WWN3 WWN4

....

​

Hello,

I am looking to use an inner loop on the WWN values in my bash noscript.

This is what I have thus far, but can't seem to get the WWN values into the inner loop, any ideas what I am doing wrong?

​

#!/usr/bin/env bash

while read host wwn1 wwn2 wwn3 wwn4; do
echo $host
for i in {1..4}; do #THIS DOES NOT WORK
ggrep -A18 -E $wwn$i $host.storage_check.txt > $host.$wwn$i.txt
done
done

​

this doesn't work because its the 1..4 being used and not the WWN1-4

I want to read in the 5 values per line and then use them in the inner loop. Is it possible?

If I do this line by line, yes it works, but I want it cleaner in a loop if possible.

​

ggrep -A18 -E $wwn1 $host.storage_check.txt > $host.$wwn1.txt
ggrep -A18 -E $wwn2 $host.storage_check.txt > $host.$wwn2.txt
ggrep -A18 -E $wwn3 $host.storage_check.txt > $host.$wwn3.txt
ggrep -A18 -E $wwn4 $host.storage_check.txt > $host.$wwn4.txt

​

I just cant seem to find what I am looking for to do this.

https://redd.it/13m876q
@r_bash
no idea why this noscript isn't working.

deckdir=$XDG_CONFIG_HOME/mydir/

read ans

selectedDeck=$(ls -1 $deckdir | awk -v var="$ans" 'NR==var' )

echo "$(selectedDeck)"

​

the error is

./noscript: 4: selectedDeck: not found

https://redd.it/13mgtcu
@r_bash
Setting up gitbash for a coding bootcamp on Monday everything is screwed up

I’m not computer savvy and start class on a Monday. Trying to set up gitbash and VS. I don’t even know where I screwed up but my directory and paths are all messed up. Should I delete everything and start over?

https://redd.it/13mhn0b
@r_bash
why this noscript isn't working:(

Hello! I made a shell noscript to sort a list of students from a file, make the top 3 students from a class and calculate the average grade for those who have the grade >=5. I don't know what i wrote wrong, please tell me what to modify. I am a beginner, i don't know how to write very good.

\#!/bin/bash

filename=$1

echo "The first three promovated students:"

sorted_names=$(awk -F ',' '$4 >= 5' "$filename" | sort -t ',' -k4 -nr | awk -F ',' '{print $1}' | head -n 3)

if [[ -z $sorted_names \]\]; then

echo "Doesn't exist."

else

echo "$sorted_names"

fi

sum=0

count=0

while IFS=',' read -r number name surname grade; do

if (( nota >= 5 )); then

sum=$((sum + nota))

count=$((count + 1))

fi

done < "$filename"

if (( count > 0 )); then

class_avg=$(bc <<< "scale=2; $sum / $count")

echo "Class average: $class_avg"

else

echo "There are no passed students to calculate the class average."

fi

https://redd.it/13n938v
@r_bash
Stop git from running

Everytime I try to run a .sh file written in bash, a Git window appears and disappears right after, stopping the program from running properly. Any solutions to this?

https://redd.it/13napt7
@r_bash
Script to find matching strings in 2 large CSV files and print matching rows to 3rd file.

I have two very large CSV files: File1 and File2. File1 has over 730,000 entries and File2 has several million. Column A of each file is a unique 7 digit ID number.

I want to automatically find all rows from File2 that have the same ID numbers as File1, and write those rows to a new third csv file.

Any help would be appreciated, thanks.

https://redd.it/13nbxep
@r_bash
Why this noscript doesn't work?

This noscript works as expected:

if [ $1 != "" ]; then
play ~/.config/dunst/notification-sound.opus
fi

But if I add an or it start running play always:

if [ $1 != "Spotify" || $1 != "" ]; then
play ~/.config/dunst/notification-sound.opus
fi

What I'm doing wrong?

https://redd.it/13nbay9
@r_bash
best way to deal with unexpected operator when user inputs enter instead of a letter?

context: user is given two valid options to choose from

read var
while [ $var != a \] && [ $var != z \]
do
printf "Not a valid option. Enter again\\n"
read var
done


but if the user presses the enter key, then
[: !=: unexpected operator
same thing happens when the user is supposed to enter a number but enters a letter instead and vice versa

is 2>/dev/null the best solution?

https://redd.it/13neu16
@r_bash
How to write a mounted shared path

I haven't touched bash or cbash in years and today i installed a ubuntu VM client on a windows OS.
The VM shares a folder, and i'm planning to convert a few noscripts into bash (or whatever) which i previously ran in windows batch files.

The question i have is how do i write the path?
The path in windows might be e:/shared
I think the folder in ubuntu is /mnt/hgfs/shared

Is /mnt/hgfs/shared the path i use in a bash noscript file?

Eg., Delete pdfs in a subfolder might be something like "rm /mnt/hgfs/shared/no-more-pdfs/*.pdf"?

Thx
A

https://redd.it/13ngzg4
@r_bash
Can't get archiving backup noscript to work

Following a readout of a noscript in the 'Linux Command Line and Shell Script BIBLE (4th Ed.)', and it doesn't seem to archive the directories specified in the files-to-backup.txt file; rather, I get a 45B 'archive<today's-date>.tar.gz' file (in the correct periodic directory, at least) that's completely empty.

It does use an interesting method of building the $file_list variable, though:

#!/bin/bash

#DailyArchive - Archive designated files & directories
######## Variables ########################################
#
# Gather the Current Date
#
today=$(date +%y%m%d)
#
# Set Archive filename
#
backup
file=archive$today.tar.gz
#
# Set configuration and destination files
#
basedir=/mnt/j
configfile=$basedir/archive/files-to-backup.txt
period=daily
basedest=$basedir/archive/$period
destination=$basedest/$backup
file
#
# Set desired number number of maintained backups
#
backups=5

######### Functions #######################################

prunebackups() {
local directory="$1" # Directory path
local num
archives="$2" # Number of archives to maintain

# Check if the directory exists
if [ ! -d "$directory" ]
then
echo "Directory does not exist: $directory"
return 1
fi

# Check if there are enough archives in the directory to warrant pruning
local numfiles=$(find "$directory" -maxdepth 1 -type f | wc -l)
if (( num
files >= numarchives )) # If there are...
then
# ...delete the oldest archive
local num
filestodelete=$(( numfiles - numarchives + 1 ))
local filestodelete=$(find "$directory" -maxdepth 1 -type f -printf '%T@ %p\n' | sort -n\
| head -n "$numfilestodelete" | awk '{print $2}')

echo
echo "Deleting the following backup:"
echo "$files
todelete"
sudo rm -f "$files
todelete"
echo "Continuing with backup..."
fi
}

######### Main Script #####################################
#
# Check Backup Config file exists
#

if [ -f "$config
file" ] # Make sure the config file still exists.
then # If it exists, do nothing and carry on.
echo
else # If it doesn't exist, issue an error & exit the noscript.
echo
echo "$(basename "$0"): Error: $configfile does not exist."
echo "Backup not completed due to missing configuration file."
echo
exit 1
fi

#
# Check to make sure the desired number of maintained backups isn't exceeded.
#

prune
backups $basedest $backups || { echo "$(basename "$0"): Error: Unable to prune backup\
directory. Exiting." >&2 ; exit 1; }


#
# Build the names of all the files to backup.
#

fileno=1 # Start on line 1 of the Config File.
exec 0< "$config
file" # Redirect Std Input to the name of the Config File.

read filename # Read first record.

while [ "$?" -eq 0 ] # Create list of files to backup.
do
# Make sure the file or directory exists.
if [ -f "$file
name" ] || -d "$file_name"
then
# If the file exists, add its name to the list.
filelist="$filelist $filename"
else
# If the file does not exist, issue a warning.
echo
echo "$(basename "$0"): Warning: $file
name does not exist."
echo "Obviously, I will not include it in this archive."
echo "It is listed on line $fileno of the config file."
echo "Continuing to build archive list..."
echo
fi

file
no=$((fileno + 1)) # Increment the Line/File number by one.
read file
name # Read the next record.
done

########################################
#
# Back up
the files and Compress Archive
#

echo "Starting archive..."
echo

sudo tar -czf "$destination" "$filelist" 2> /dev/null

echo "Archive completed"
echo "Resulting archive file is: $destination."
echo

exit

Now, I have modified the noscript, adding the 'prune\
backups()' function, but something doesn't quite seem right though, and I can't put my finger on what it is. Can anyone see either where I've screwed up, or if it's just something with the noscript itself?

https://redd.it/13np7cp
@r_bash
Is there a way to record bash keyboard shortcuts in a command line noscript?

When I use specific shortcuts on a line such of Control+U, does that save to the noscript file? It doesn't create any output and nothing shows up when I open the file with cat.

On the other hand, when I open the file using Notepad, it shows the line I deleted with Control+U followed by " [K" is that the indicator that a shortcut was used on the line?

https://redd.it/13nqlcm
@r_bash
Only download if file doesn't exist

Wrote this Bash noscript to download a file and place it in a folder however it's re-downloading every time it's run. I'd like for the download to only occur if the file does not already exist.

#!/bin/bash
# Download files from Github repo to /tmp/appfolder

URLDOWNLOAD1="https://raw.githubusercontent.com/Work/Repo/main/appfolder/logo.png";
TEMPDIR="appfolder";

FILENAME="$(basename $URLDOWNLOAD1)";
DOWNLOADFILE="/tmp/$TEMPDIR/$FILENAME";

if ! -d "/tmp/$TEMPDIR" ; then
mkdir -p "/tmp/$TEMPDIR";
fi;

curl -L --silent -o "$DOWNLOADFILE" "$URLDOWNLOAD1";

if -e $DOWNLOADFILE ; then
echo "$DOWNLOADFILE - Success";
else
echo "logo.png Download failed";
fi

Any assistance would be greatly appreciated!

https://redd.it/13of1p1
@r_bash
Pipelight - Automation with Typenoscript{Bash} 🤌

I needed something to glue commands together but I prefer using javanoscript syntax over bash conditionals, loops and functions (yes i am evil 😈)

It has matured over the years, has been roasted, improved, refactored, and I think it has become stable enough to share it once again!

It's merely bash wrapped with typenoscript , with extra automation super powers 🦸

Documentation still improving: https://pipelight.dev/

I leave this here for the ones who need this kind of tool and hope you will find it usefull 😄

https://redd.it/13olxdi
@r_bash
Protecting SSHD's Listening Port with Port Knocking (or better)

Hi all

Whenever I set up a Linux box/VPS,
the first thing I do is change SSHD's Listening Port to something different than the default port.

I would like to add another protection to it, and start using Port Knocking,
so the port would appear Closed/NotListening to anyone,
except me, when I want to connect.

Can you please tell me what you recommend to use?

And also:
Is there something more advanced than Port Knocking that I should check,
for protecting the listening port?


Thank you very much

https://redd.it/13onqb7
@r_bash
print $0 giving me unexpected result

Good afternoon, I am relatively new to using awk commands but I am running into a small issue with a output that involves $0.

I am reading in trade_data.csv.small:

sym,price,size,side,market,time,id

MDT,72.34000000,100,Short,NASDAQ,10:00:00.146882,1180355

MDT,72.35000000,125,Short,NASDAQ,10:00:00.165655,1180537

&#x200B;

This is my awk line:

awk -F "[,\]" 'NR>1 {if ($3 == 100) print $0, " | 100 "; else print $0, " | not 100 | "}' trade_data.csv.small

&#x200B;

The output is:

| 100 4000000,100,Short,NASDAQ,10:00:00.146882,1180355

| not 100 | 0,125,Short,NASDAQ,10:00:00.165655,1180537

&#x200B;

I thought that the output would be something like:

MDT,72.34000000,100,Short,NASDAQ,10:00:00.146882,1180355 | 100

MDT,72.35000000,125,Short,NASDAQ,10:00:00.165655,1180537 | not 100

&#x200B;

What am I doing wrong here?

Thank you in advance for your insight(s).

https://redd.it/13oxjfs
@r_bash
HAVING ERROR IN FIRST CODE

Hello everyone so I started learning bash today and I am making a basic math calculator thorugh my VMware kali terminal. I dont know why but I am getting this error of line 35 error: syntax error near unexpected token ' echo "result: $result" '...Can anyone help me out? (Attaching my code below)
ALSO in my code image if I remove single quotes it still shows error

https://preview.redd.it/akbma2u5kf1b1.png?width=412&format=png&auto=webp&v=enabled&s=76a7ac94a05cc9e0e11c8956e5358b43e516cd21

https://redd.it/13ozru9
@r_bash