r_bash – Telegram
Multiple conditions help

Hi everyone. I would like to apologize in advance. I am only a beginner and I have to learn bash for a class that I'm doing. I'm not here to get answers, of course. I'm here to understand what I'm doing and learn from my mistakes.

This is currently what I have written out. I do not know what to do with multiple conditions, so I just wrote out a bunch of elifs. This obviously did not work and now I don't know what to do.

\#!/bin/bash

read -p "Please enter shipping weight: " var

echo "Your number is $var"

if [ "$var" -le 2 \]

then

echo "Price is 1.10"

elif [ "$var" -ge \] && [ 2 "$var" -le 5 \]

then

echo "Price is 2.20"

elif [ "$var" -ge 6 \] && [ "$var" -le 10 \]

then

echo "Price is 3.70"

elif [ "$var" -ge 10 \]

then

echo "Price is 3.80"

fi

​

The actual question for my homework in question, purely for context of what I'm supposed to be doing:

​

Just for context.

https://redd.it/11evq69
@r_bash
Help with regular expressions

I have downloaded some videos but the program used for downloading has appended some random string in brackets at the end of the filename. I want to remove that random string. I tried renaming the files using:

❯ mmv -n '* [*] .mp4' '#1.mp4'

* [*] .mp4 -> #1.mp4 : no match.

Nothing done.

I believe that what I'm writing means "match whatever (and a blank space) up to the first opening bracket, then match whatever again up to first closing bracket and finally match a blankspace and the .mp4 extension. Replace all that with just the first whatever-matching.:

This however returns a "no match" error.

Perhaps this has something to do with the fact that the names of the files are pretty obscure. They are greek characters and contain a lot of white spaces, so perhaps it needs more precise handling. However, I'm not sure. This is the output of the "ls -a" command.

❯ ls -a

.

..

'2021 03 04 15 37 53 [JdSDGDNC2Uo].mp4'

'2η Ενισχυτική Matlab 2021 03 23 18 46 58 [lfzYHsF0QVc].mp4'

'2η ενισχυτική εξάσκηση σε MATLAB [TLuW6SK3XCc].mp4'

'Απεικονιση1 2021 02 25 [mUEzmJWkPKk].mp4'

'Ιατρική Απεικόνιση 11 3 [puElBwRAXxU].mp4'

'Ιατρική Απεικόνιση 18 3 [xJKXG5RcaQ0].mp4'

Any help is well appreciated. Feel free to ask for clarifications.

https://redd.it/11f0g8p
@r_bash
Janitor V1 | Tronnoscript for Linux

Here's what I've got.
It's supposed to behave similar to Tronnoscript for Windows, but it stops after the very first task.

I put it together using ChatGPT because I don't know what I'm doing. I'm starting to understand how this works, but I need some help.

Are you guys able to help me spot where I'm going wrong?

#!/bin/bash



# Create an array of tasks with corresponding flags



tasks=("Update system" "Clean up temporary files" "Check for ClamAV malware" "Check for rkhunter malware" "Remove old log files" "Check and repair file system errors" "Clean up apt cache" "Clear system memory cache" "Optimize disk usage" "Reboot if needed")



flags=(-s -c -v -r -l -f -a -m -o -b)



# Display a checklist of tasks to the user and prompt for input



echo "Please select which tasks to run by entering the corresponding flags:"



echo ""



for (( i=0; i<${#tasks@}; i++ )); do



echo " ${flagsi} - ${tasksi}"



done



echo ""



# Read user input and set corresponding variables



read -p "Enter task flags (default=all): " inputtasks



input
tasks=${inputtasks:-"all"}



# Check for invalid task flags



for flag in $(echo $input
tasks | tr "," " "); do



if [ ! "${flags[@}" =~ "${flag}" && $flag != "all" ]]; then



echo "Error: Invalid task flag: $flag"



exit 1



fi



done



# Set variables based on user input

systemupdate=false

clean
tempfiles=false

clamav
scan=false

rkhunterscan=false

remove
logs=false

checkfilesystem=false

cleanaptcache=false

clearmemorycache=false

optimizediskusage=false

rebootifneeded=false



IFS=',' read -ra taskflags <<< "$inputtasks"

for flag in "${taskflags[@]}"; do

case "$flag" in

-s|all) system
update=true;;

-c|all) cleantempfiles=true;;

-v|all) clamavscan=true;;

-r|all) rkhunter
scan=true;;

-l|all) removelogs=true;;

-f|all) check
filesystem=true;;

-a|all) clean
aptcache=true;;

-m|all) clear
memorycache=true;;

-o|all) optimize
diskusage=true;;

-b|all) reboot
ifneeded=true;;

*) echo "Error: Invalid task flag: $flag"; exit 1;;

esac

done



# Step 1: System update

system
updatefunc() {

echo "Updating system..."

sudo apt update && sudo apt upgrade -y

echo "System updated."

wait

}



# Step 2: Clean temp files

clean
tempfilesfunc() {

echo "Cleaning temp files..."

sudo rm -rf /tmp/

sudo rm -rf /var/tmp/

echo "Temp files cleaned."

}



# Step 3: Check and install/update ClamAV

clamavscanfunc() {

echo "Checking ClamAV..."

if ! command -v clamscan &> /dev/null

then

echo "ClamAV not found. Installing ClamAV..."

sudo apt install clamav -y

else

echo "ClamAV found. Updating ClamAV..."

sudo freshclam

fi



echo "Running ClamAV scan..."

sudo clamscan -r / --exclude-dir=/sys/fs/cgroup --exclude-dir=/run/media --exclude-dir=/proc

if ! command -v clamscan &> /dev/null

then

echo "ClamAV was not initially installed. Uninstalling ClamAV..."

sudo apt remove clamav -y

fi

}



# Step 4: Check and install/update rkhunter

rkhunterscanfunc() {

echo "Checking rkhunter..."
Janitor V1 | Tronnoscript for Linux

Here's what I've got.
It's *supposed* to behave similar to Tronnoscript for Windows, but it stops after the very first task.

I put it together using ChatGPT because I don't know what I'm doing. I'm starting to understand how this works, but I need some help.

Are you guys able to help me spot where I'm going wrong?

#!/bin/bash



# Create an array of tasks with corresponding flags



tasks=("Update system" "Clean up temporary files" "Check for ClamAV malware" "Check for rkhunter malware" "Remove old log files" "Check and repair file system errors" "Clean up apt cache" "Clear system memory cache" "Optimize disk usage" "Reboot if needed")



flags=(-s -c -v -r -l -f -a -m -o -b)



# Display a checklist of tasks to the user and prompt for input



echo "Please select which tasks to run by entering the corresponding flags:"



echo ""



for (( i=0; i<${#tasks[@]}; i++ )); do



echo " ${flags[i]} - ${tasks[i]}"



done



echo ""



# Read user input and set corresponding variables



read -p "Enter task flags (default=all): " input_tasks



input_tasks=${input_tasks:-"all"}



# Check for invalid task flags



for flag in $(echo $input_tasks | tr "," " "); do



if [[ ! "${flags[@]}" =~ "${flag}" && $flag != "all" ]]; then



echo "Error: Invalid task flag: $flag"



exit 1



fi



done



# Set variables based on user input

system_update=false

clean_temp_files=false

clamav_scan=false

rkhunter_scan=false

remove_logs=false

check_file_system=false

clean_apt_cache=false

clear_memory_cache=false

optimize_disk_usage=false

reboot_if_needed=false



IFS=',' read -ra task_flags <<< "$input_tasks"

for flag in "${task_flags[@]}"; do

case "$flag" in

-s|all) system_update=true;;

-c|all) clean_temp_files=true;;

-v|all) clamav_scan=true;;

-r|all) rkhunter_scan=true;;

-l|all) remove_logs=true;;

-f|all) check_file_system=true;;

-a|all) clean_apt_cache=true;;

-m|all) clear_memory_cache=true;;

-o|all) optimize_disk_usage=true;;

-b|all) reboot_if_needed=true;;

*) echo "Error: Invalid task flag: $flag"; exit 1;;

esac

done



# Step 1: System update

system_update_func() {

echo "Updating system..."

sudo apt update && sudo apt upgrade -y

echo "System updated."

wait

}



# Step 2: Clean temp files

clean_temp_files_func() {

echo "Cleaning temp files..."

sudo rm -rf /tmp/*

sudo rm -rf /var/tmp/*

echo "Temp files cleaned."

}



# Step 3: Check and install/update ClamAV

clamav_scan_func() {

echo "Checking ClamAV..."

if ! command -v clamscan &> /dev/null

then

echo "ClamAV not found. Installing ClamAV..."

sudo apt install clamav -y

else

echo "ClamAV found. Updating ClamAV..."

sudo freshclam

fi



echo "Running ClamAV scan..."

sudo clamscan -r / --exclude-dir=/sys/fs/cgroup --exclude-dir=/run/media --exclude-dir=/proc

if ! command -v clamscan &> /dev/null

then

echo "ClamAV was not initially installed. Uninstalling ClamAV..."

sudo apt remove clamav -y

fi

}



# Step 4: Check and install/update rkhunter

rkhunter_scan_func() {

echo "Checking rkhunter..."
if ! command -v rkhunter &> /dev/null

then

echo "rkhunter not found. Installing rkhunter..."

sudo apt install rkhunter -y

sudo rkhunter --update

else

echo "rkhunter found. Updating rkhunter..."

sudo rkhunter --update

fi



echo "Running rkhunter scan..."

sudo rkhunter --check

if ! command -v rkhunter &> /dev/null

then

echo "rkhunter was not initially installed. Uninstalling rkhunter..."

sudo apt remove rkhunter -y

fi

}



# Step 5: Remove logs

remove_logs_func() {

echo "Removing logs..."

sudo find /var/log -type f -delete

sudo find /var/log -type f -iname *.gz -delete

echo "Logs removed."

}



# Step 6: Check file system

check_file_system_func() {

echo "Checking file system..."

sudo fsck -Af -M

echo "File system check completed."

}



# Step 7: Clear apt cache

clean_apt_cache_func() {

echo "Clearing apt cache..."

sudo apt clean

echo "Apt cache cleared."

}



# Step 8: Clear memory cache

clear_memory_cache_func() {

echo "Clearing memory cache..."

sudo sync && sudo echo 3 | sudo tee /proc/sys/vm/drop_caches

echo "Memory cache cleared."

}



# Step 9: Optimize disk usage

optimize_disk_usage_func() {

echo "Optimizing disk usage..."

sudo fstrim -v /

echo "Disk usage optimized."

}



# Step 10: Reboot if needed

reboot_if_needed_func() {

if [ -f /var/run/reboot-required ]; then

echo "Reboot required. Rebooting now..."

sudo reboot

else

echo "No reboot required."

fi

}



# Execute selected tasks

echo "Executing selected tasks..."



if [ "$system_update" = true ]; then

echo "Updating system packages..."

system_update_func

fi



if [ "$clean_temp_files" = true ]; then

echo "Cleaning up temporary files..."

clean_temp_files_func

fi



if [ "$clamav_scan" = true ]; then

echo "Checking for ClamAV malware..."

clamav_scan_func

fi



if [ "$rkhunter_scan" = true ]; then

echo "Checking for rkhunter malware..."

rkhunter_scan_func

fi



if [ "$remove_logs" = true ]; then

echo "Removing old log files..."

remove_logs_func

fi



if [ "$check_file_system" = true ]; then

echo "Checking and repairing file system errors..."

check_file_system_func

fi



if [ "$clean_apt_cache" = true ]; then

echo "Cleaning up apt cache..."

clean_apt_cache_func

fi



if [ "$clear_memory_cache" = true ]; then

echo "Clearing system memory cache..."

clear_memory_cache_func

fi



if [ "$optimize_disk_usage" = true ]; then

echo "Optimizing disk usage..."

optimize_disk_usage_func

fi



echo "Done."

https://redd.it/11fkp5r
@r_bash
Basic scope question

Hello. I am relatively new to bash and ran into a stupid newb issue dealing with variable scope.

The noscript I was writing sets up several environment-related things depending on the machine’s role.

The idea is that I’d source a ‘common’ environment, and then depending on the machine I’d run source other noscripts specific to their role.

Where I went wrong was that I reused variable several variable names from my main noscript in each of the environments. So when I went from the ‘common’ environment setup and on to the contextual environment setup it carried along with it the modified variables leading to side effects.

I really did not want to have to come up with new variable names for variables that were essentially doing the same thing, just in different contexts, so instead I just wrapped each noscript into a function and made each variable local.

This worked, but is this the ‘correct’ way? Is there a more concise way to ensure variable stay put?

https://redd.it/11fq49x
@r_bash
Here you are useifytech

Hello, too much to post in a comment I guess.

Use diff, between my version, and yours.

You'll want to remove the test code I added to see too that the menu system now runs in a while loop, or change the text, and maybe the flags at least.

You'd really want to make it able to handle command line flags as well, and just run through the noscript without showing any menus.
Left as an exercise to you!

#!/bin/bash

# Create an array of tasks with corresponding flags

tasks=("Update system" "Clean up temporary files" "Check for malware (ClamAv)" "Check for rootkits, backdoors and possible local exploits (rkhunter)" "Remove old log files" "Check and repair file system errors" "Clean up apt cache" "Clear system memory cache" "Optimize disk usage" "Reboot (if needed)" "Loop test" "Quit Janitor V1")

flags=(-s -c -v -r -l -f -a -m -o -b t q)



# Step 1: System update
system_update_func() {
echo "Updating system..."
sudo apt update && sudo apt upgrade -y
echo "System updated."
wait
}

# Step 2: Clean temp files
clean_temp_files_func() {
echo "Cleaning temp files..."
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*
echo "Temp files cleaned."
}

# Step 3: Check and install/update ClamAV
clamav_scan_func() {
echo "Checking ClamAV..."
if ! command -v clamscan &> /dev/null
then
echo "ClamAV not found. Installing ClamAV..."
sudo apt install clamav -y
else
echo "ClamAV found. Updating ClamAV..."
sudo freshclam
fi

echo "Running ClamAV scan..."
sudo clamscan -r / --exclude-dir=/sys/fs/cgroup --exclude-dir=/run/media --exclude-dir=/proc
if ! command -v clamscan &> /dev/null
then
echo "ClamAV was not initially installed. Uninstalling ClamAV..."
sudo apt remove clamav -y
fi
}

# Step 4: Check and install/update rkhunter
rkhunter_scan_func() {
echo "Checking rkhunter..."
if ! command -v rkhunter &> /dev/null
then
echo "rkhunter not found. Installing rkhunter..."
sudo apt install rkhunter -y
sudo rkhunter --update
else
echo "rkhunter found. Updating rkhunter..."
sudo rkhunter --update
fi

echo "Running rkhunter scan..."
sudo rkhunter --check
if ! command -v rkhunter &> /dev/null
then
echo "rkhunter was not initially installed. Uninstalling rkhunter..."
sudo apt remove rkhunter -y
fi
}

# Step 5: Remove logs
remove_logs_func() {
echo "Removing logs..."
sudo find /var/log -type f -delete
sudo find /var/log -type f -iname *.gz -delete
echo "Logs removed."
}

# Step 6: Check file system
check_file_system_func() {
echo "Checking file system..."
sudo fsck -Af -M
echo "File system check completed."
}

# Step 7: Clear apt cache
clean_apt_cache_func() {
echo "Clearing apt cache..."
sudo apt clean
echo "Apt cache cleared."
}

# Step 8: Clear memory cache
clear_memory_cache_func() {
echo "Clearing memory cache..."
sudo sync && sudo echo 3 | sudo tee /proc/sys/vm/drop_caches
echo "Memory cache cleared."
}

# Step 9: Optimize disk usage
optimize_disk_usage_func() {
echo "Optimizing disk usage..."
sudo fstrim -v /
echo "Disk usage optimized."
}

# Step 10: Reboot if needed
reboot_if_needed_func() {
if [ -f /var/run/reboot-required ]; then
echo "Reboot required. Rebooting now..."
sudo reboot
else
echo "No reboot required."
fi
}
while true; do
# Display a checklist of tasks to the user and prompt for input

echo "Please select which tasks to run by entering the corresponding flags:"

echo ""

for (( i=0; i<${#tasks[@]}; i++ )); do

echo " ${flags[i]} -
${tasks[i]}"

done

echo ""

# Read user input and set corresponding variables

read -p "Enter task flags (default=all): " input_tasks

input_tasks=${input_tasks:-"all"}

# Check for invalid task flags

for flag in $(echo $input_tasks | tr "," " "); do

if [[ ! "${flags[@]}" =~ "${flag}" && $flag != "all" ]]; then

echo "Error: Invalid task flag: $flag"

exit 1

fi

done

# Set variables based on user input
errthang=false
system_update=false
clean_temp_files=false
clamav_scan=false
rkhunter_scan=false
remove_logs=false
check_file_system=false
clean_apt_cache=false
clear_memory_cache=false
optimize_disk_usage=false
reboot_if_needed=false

IFS=',' read -ra task_flags <<< "$input_tasks"
for flag in "${task_flags[@]}"; do
case "$flag" in
all) errthang=true;;
-s|all) system_update=true;;
-c|all) clean_temp_files=true;;
-v|all) clamav_scan=true;;
-r|all) rkhunter_scan=true;;
-l|all) remove_logs=true;;
-f|all) check_file_system=true;;
-a|all) clean_apt_cache=true;;
-m|all) clear_memory_cache=true;;
-o|all) optimize_disk_usage=true;;
-b|all) reboot_if_needed=true;;
t ) continue ;;
q ) break 2 ;;
*) echo "Error: Invalid task flag: $flag"; exit 1;;
esac
done

#Verbose
# set -x
# Execute selected tasks
echo "Executing selected tasks..."

if [ "$errthang" = true ]; then
echo "Updating system packages..."
system_update_func
wait
clean_temp_files_func
wait
clamav_scan_func
wait
rkhunter_scan_func
wait
remove_logs_func
wait
check_file_system_func
wait
clean_apt_cache_func
wait
clear_memory_cache_func
wait
optimize_disk_usage_func
fi

if [ "$system_update" = true ]; then
echo "Updating system packages..."
system_update_func
fi

if [ "$clean_temp_files" = true ]; then
echo "Cleaning up temporary files..."
clean_temp_files_func
fi

if [ "$clamav_scan" = true ]; then
echo "Checking for ClamAV malware..."
clamav_scan_func
fi

if [ "$rkhunter_scan" = true ]; then
echo "Checking for rkhunter malware..."
rkhunter_scan_func
fi
if [ "$remove_logs" = true ]; then
echo "Removing old log files..."
remove_logs_func
fi

if [ "$check_file_system" = true ]; then
echo "Checking and repairing file system errors..."
check_file_system_func
fi

if [ "$clean_apt_cache" = true ]; then
echo "Cleaning up apt cache..."
clean_apt_cache_func
fi

if [ "$clear_memory_cache" = true ]; then
echo "Clearing system memory cache..."
clear_memory_cache_func
fi

if [ "$optimize_disk_usage" = true ]; then
echo "Optimizing disk usage..."
optimize_disk_usage_func
fi
# set +x
clear
done
echo "Done."

https://redd.it/11g142q
@r_bash
I've implemented working parser in Bash + Sed from the ground and wrote unit tests for it

It allows to extract command name, denoscription, tags, tokens for code examples and their denoscriptions.

Here is my implementation. It works straightforward: parser validates page layout and checks more details user requests for. For instance if I wanna get some tag from page (to get link to the official tool documentation) parser will check not just layout, but also whether written tags are right.

And that's how it looks like right now:


https://preview.redd.it/pgngxz09jbla1.png?width=1587&format=png&auto=webp&v=enabled&s=e3e8160086d637375dd55fd8c85408c60fddf93f

In other words I've proved myself that it's possible to right some cool stuff in Bash and Sed. ;)

https://redd.it/11g1b5q
@r_bash
zentest, small simple solution for testing code.

ZENTEST
-------

### Overview

A minimal test setup, that is taken out of a larger context.
There are so many ways to perform tests, I needed one I
could use standalone, in a loop like so:

for f in fileA FileB fileC ; do
.git/test-runner.sh $f > logs/$f-$(date +%s).log
done

I can also invoke the tests from my build Makefile in a
rule like so:

fileA:
shellcheck -xe $@
.git-test-runner.sh $@

Or, I can call it up from my .git precommit hook, if it fits
the bill, file name wise, that a file that is under testing,
is about to be committed.

### Organization

Everything is initiated/executed with the project root, as
the current working directory $PWD.

Every source file, that is to be tested, has its own little
makefile with the different tests. The make in use is
GNUmake, so that the .ONESHELL directive works, so that you
can yourself decide if the test passes or fails, in fact,
every rule in the makefile will work as a little shell
noscript. (Otherwise every commandline in a makefile is executed in its own shell.)

In project root, we have a workdir directory, which holds
temporary project files, and a tests directory, that
contains all the small makefiles, and what else of files,
that are needed to perform the tests in the makefiles.


The test-runner.sh is a very simple noscript, that only
figures out the correct makefile to run from the test/
directory. It bails if it doesn't find a suitable makefile,
I plan to make a copy of my template-makefile then, and open
it in $EDITOR.

### What is left out here:

The error messages, I like to get the errors into a format
that I can load into a quick fix list in Vim, so when my
tests fails, I deliver errors in the unix error format.
That is outside of the scope here, but easy to implement!

### "Source code".


#!/bin/bash
# Preliminaries: output is redirected up front to $TESTERRORS
# Context: We are executed from the root of our project, i.e. right
# inside the directory that contains the project.
# We have a "tests" folder within the project directory, that contains
# makefiles, one for every file that is to be tested.
# each makefile is named as the same as the file in question,
# which means that every file of your project, that is to be tested
# needs to have a unique stem name.

PNAME="${0##/}"
if [[ $# -ne 1 ]] ; then
echo -e "
$PNAME : I need a file name to test\nTerminating..."
exit 2
fi

if [[ "${1}" != "${1/.//}" ]] ; then
makefname="$( basename "${1}" "${1#
.}")"mk
else
makefname="${1}".mk
fi


if [ ! -r tests/"$makefname" ] ; then
echo -e "$PNAME : tests/$makefname doesn't exist.\nCan't test ${1}!\
\nTerminating..."
exit 2
else
make -f tests/"$makefname"
exit $?
fi

The makefiles is equally simple, below is a simple template.
makefile. Here is a small superb make
tutorial


.ONESHELL:
all: ./workdir/step3 clean

./workdir/step1:
@noscript1 ./workdir/step1
touch ./workdir/step1

./workdir/step2: ./workdir/step1
@noscript1 ./workdir/step2
touch ./workdir/step2
exit 1

./workdir/step3: ./workdir/step2
@noscript1 ./workdir/step3
touch ./workdir/step3


clean:
rm ./workdir/step1 ./workdir/step2 ./workdir/step3

Last updated:23-03-02 15:06

https://redd.it/11g3mnv
@r_bash
My cat entered the following into git bash and it put my editor into some sort've other commandline

p[-0.4kjmn.cf yy;bknmg#f'g 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9Function Key 9OAFunction Key 9i

Typing in p[ seems to trigger it but I don't know what it is, any ideas?

https://redd.it/11g7uma
@r_bash
Diff 2 Files Without Knowing Exact Names

I want to diff two files, but I do not know the exact names.

The use case is, we are using DevOps pipelines that download a pre and post configuration file.

I would know what the files start with but the ending will be a date time string that I would not know.

Looking for help with a noscript that finds the files and does a diff of the later version vs the earlier version.

Names would be something like this:

name20230302T142605
name
20230302T142803

Any thoughts on how to do this?

https://redd.it/11gauho
@r_bash
Worked a lot on this noscript lately

I would like to share a noscript that I think can be quite useful for people who play games in DOSBox, or want to extract the OST from games (works with console games too that use CD audio). All that's required is to pass the CUE file as an argument to the noscript.

&#x200B;

https://github.com/linux4ever07/noscripts/blob/main/cuebin\_extract.sh

&#x200B;

I linked it before as part of a collective post about a bunch of my noscripts. Since I made that post, I've spent a lot of time refining everything so my noscripts can reach their 'final form'. The goal is to have them in such a state that I could die tomorrow and they will keep working indefinitely, and be easy to understand and maintain should people need to.

&#x200B;

This specific noscript seems to be the last of the bunch to get its 'final makeover'. I might still update it, but I don't think I will change it a whole lot more. The one thing that comes to mind is to add support for additional audio formats, like Opus, MP3 etc. The reason I didn't add it myself yet is because I'm not interested. I always just use FLAC, and Ogg Vorbis is there as a lossy option. DOSBox supports Opus though, so maybe someone will be interested in forking the noscript and adding that, I don't know.

&#x200B;

Listing the top comments of the noscript below, to elaborate on how it works:

&#x200B;

\# This noscript is meant to take an input BIN/CUE file, extract the raw

\# track(s) (data / audio) in whatever format the user has specified

\# through noscript arguments. The noscript simply separates all the tracks

\# of BIN/CUE files.

&#x200B;

\# Available audio formats are:

\# * cdr (native CD audio)

\# * ogg (Ogg Vorbis)

\# * flac (Free Lossless Audio Codec)

&#x200B;

\# If no format is specified as an argument, the noscript will extract all

\# 3 formats, and create CUE sheets for all 3 formats as well.

&#x200B;

\# The original purpose of the noscript is to take DOS games that have CD

\# audio, and getting rid of the need to store the uncompressed audio.

\# Ogg Vorbis is a lossy codec, so the files are much smaller and near

\# the same quality. In the case of FLAC, it's a lossless format so the

\# quality is identical to native CD audio. The only difference is FLAC

\# is losslessly compressed so the files are slightly smaller. The

\# generated CUE sheets can be used with DOSBox, using the 'IMGMOUNT'

\# command.

&#x200B;

\# https://www.dosbox.com/wiki/IMGMOUNT

&#x200B;

\# Another use case for this noscript is to simply extract the OST from

\# games, to listen to.

&#x200B;

\# The noscript will work with all kinds of games, including PS1 and Sega

\# Saturn games. All that's required is that the disc image is in the

\# BIN/CUE format. Though, I'm not sure if there's emulators that can

\# handle FLAC or Ogg Vorbis tracks. The point would mainly be to listen

\# to the music.

&#x200B;

\# Yet another use case is to just split a BIN/CUE into its separate

\# tracks, with the '-cdr' argument, without encoding the audio.

&#x200B;

\# It's possible to do a byteswap on the audio tracks (to switch the

\# endianness / byte order), through the optional '-byteswap' argument.

\# This is needed in some cases, or audio tracks will be white noise if

\# the endianness is wrong. So, it's easy to tell whether or not the byte

\# order is correct.

&#x200B;

\# ISO files produced by 'bchunk' are discarded, and data tracks are

\# instead copied directly from the source BIN file, calculating the

\# length of tracks based on information gathered from the CUE sheet.

&#x200B;

\# Since the 'copy_track' function is now able to correctly copy any

\# track from the source BIN file, it's possible to make this noscript not

\# depend on 'bchunk' anymore. The default mode is to use 'bchunk', but

\# if the user passes the '-sox' argument to the noscript, then 'sox' is

\# used instead. The end result is identical either way. It's just nice

\# to have a way out, in
case a certain program is not available.

&#x200B;

\# The advantage of using the '-sox' argument, is that the noscript is then

\# able to process CUE sheets that contain multiple FILE commands (list

\# multiple BIN files). As an example, Redump will use 1 BIN file /

\# track, so that can be processed by the noscript directly in this case,

\# without having to merge the BIN/CUE first.

&#x200B;

\# It may also be possible to use 'ffmpeg' in a similar way to how 'sox'

\# is used in the 'cdr2wav' function. But I did not manage to get it

\# working. The command would probably be something like this:

&#x200B;

\# ffmpeg -i in.cdr -ar 44.1k -ac 2 -f pcm_s16le out.wav

&#x200B;

&#x200B;

https://preview.redd.it/325rj5fpzdla1.png?width=2560&format=png&auto=webp&v=enabled&s=c46a6f02733b6b18d1077925dbd99477cd134985

https://redd.it/11gd8rf
@r_bash
Cannot pipe the output to a file

Hey guys. I am writing a noscript to provision a VM on Oracle Cloud. This Bash noscript is running on Ubuntu. The command themselves are working fine. I want to log the output to a file. I tried but nothing is being logged.

The output for this Oracle command takes about 5 minutes to come through. My guess is that the "tee" command is not waiting around for the output and it times out. How can I make it wait so it will log the output?

#!/bin/bash

echo -e "#######################################################################" >> /home/ubuntu/OCIprovision.log

date >> /home/ubuntu/OCIprovision.log

oci compute instance launch \

--availability-domain $A \

--compartment-id $C \

--shape VM.Standard.A1.Flex \

--subnet-id $S \

--assign-private-dns-record true \

--assign-public-ip true \

--availability-config file:///home/ubuntu/availabilityConfig.json \

--display-name NEW-autoprovisioned \

--image-id $I \

--instance-options file:///home/ubuntu/instanceOptions.json \

--shape-config file:///home/ubuntu/shapeConfig.json \

--ssh-authorized-keys-file /home/ubuntu/.ssh/id_rsa.pub \

--| tee -a /home/ubuntu/OCIprovision.log

https://redd.it/11guy2f
@r_bash
Command to grab piece of text from somewhere

I just need some random small text. Maybe there are sites that store text pieces for such purposes? Why do I need it? Well, I wanna write a program to demonstrate how Linux commands work in practice by showing some grabbed input text on the left and transformed output on the right side with visual highlighted differences via smth like ydiff.

https://redd.it/11hdeyp
@r_bash
Need some help with bash to combine two lists

I have two lists (List-1 and List-2) as shown below.

How would I combine them two by corresponding volume id (see my desired output)

&#x200B;

echo $LIST1
/dev/nvme0n1:vol020dae210a89ec02f
/dev/nvme1n1:vol04a2ddeb86823787a
/dev/nvme2n1:vol0e87fd7996e425e4c
/dev/nvme3n1:vol00835963bde10321b

echo $LIST2
/dev/sda1:vol020dae210a89ec02f
/dev/sdb:vol0e87fd7996e425e4c
/dev/sdf:vol04a2ddeb86823787a
/dev/sdg:vol00835963bde10321b

&#x200B;

Desired output:

echo $OUTPUT
/dev/nvme0n1:/dev/sda1
/dev/nvme1n1:/dev/sdf
/dev/nvme2n1:/dev/sdb
/dev/nvme3n1:/dev/sdg

Appreciate your help ! Cheers !

https://redd.it/11hq0my
@r_bash