r_bash – Telegram
Can no longer check if associative array exists in Bash 5.2 with -v, is this a bug?

bash-4.4# declare -A a
bash-4.4# a[foo]=bar
bash-4.4# [[ -v a[@] ]]; echo $?
0

bash-5.2# declare -A a
bash-5.2# a[foo]=bar
bash-5.2# [[ -v a[@] ]]; echo $?
1


https://redd.it/y2zxg1
@r_bash
How can I see what is coming from stdout vs stderr?

I’d like to see when a command returns something what if it is stdout and what of it is stderr.

Is there any way to make that explicit?

https://redd.it/y33flb
@r_bash
Syntax error when finishing shell noscript (macOS)

I'm trying to install SentinelOne using a shell noscript that will load its token into the installer so that we can use this in an MDM environment. My noscript is unable to run, however; no matter what changes I make, the macOS terminal will state:

line 13: syntax error near unexpected token `fi'

line 13: `fi'

​

Here is my noscript in question:

​

https://preview.redd.it/kvev5zi65mt91.jpg?width=2880&format=pjpg&auto=webp&s=8c421af6474816f9ea0aef15827e2e45707cea78

https://redd.it/y35gmo
@r_bash
How can you check whether your file is in a certain directory on Bash?

Just wanted to know if someone could help me with some programming homework.

We are asked to write a shell noscript that displays a message saying whether or not the working directory is your home directory and I am not too sure how to go about it.

If someone could help, it would be much appreciated.

https://redd.it/y39nip
@r_bash
Trim a logfile to the first and last lines containing a substring

I have a big debug log file. I need to trim this down so it contains just lines between the first and last occurrence of SUBSTRING12345.

I cannot simply use grep 'SUBSTRING12345', because the logfile contains 'blocks' spanning multiple lines, and the substring is only present on the first line of that block

I'm now digging into whether this is something sed/awk could do, but would appreciate any help as I only have limited experience of these tools. Thanks





Example input file

2022/10/13 12:41:59 S:UNRELATED -- UNRELATED
2022/10/13 12:39:55 S:UNRELATED -- UNRELATED
2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:55 S:UNRELATED -- UNRELATED
2022/10/13 12:41:59 S:UNRELATED -- UNRELATED
2022/10/13 12:41:59 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:42:03 S:SUBSTRING12345 -- MULTILINE ENTRY HERE
| MULTILINE CONTENT
| MULTILINE CONTENT
| MULTILINE CONTENT SUBSTRING12345
| MULTILINE CONTENT
| MULTILINE CONTENT
2022/10/13 12:41:59 S:UNRELATED -- UNRELATED
2022/10/13 12:42:03 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:42:03 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:43:59 S:UNRELATED -- UNRELATED
2022/10/13 12:44:55 S:UNRELATED -- UNRELATED


What grep 'SUBSTRING12345' would (incorrectly) produce...

2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:41:59 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:42:03 S:SUBSTRING12345 -- MULTILINE ENTRY HERE
| MULTILINE CONTENT SUBSTRING12345
2022/10/13 12:42:03 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:42:03 S:SUBSTRING12345 -- SINGLE LINE LOG

Desired output - Finding lines between first and last occurrence of 'SUBSTRING12345'...

2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:32 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:39:55 S:UNRELATED -- UNRELATED
2022/10/13 12:41:59 S:UNRELATED -- UNRELATED
2022/10/13 12:41:59 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:42:03 S:SUBSTRING12345 -- MULTILINE ENTRY HERE
| MULTILINE CONTENT
| MULTILINE CONTENT
| MULTILINE CONTENT SUBSTRING12345
| MULTILINE CONTENT
| MULTILINE CONTENT
2022/10/13 12:41:59 S:UNRELATED -- UNRELATED
2022/10/13 12:42:03 S:SUBSTRING12345 -- SINGLE LINE LOG
2022/10/13 12:42:03 S:SUBSTRING12345 -- SINGLE LINE LOG

https://redd.it/y3b4lw
@r_bash
is it impossible to have trap catch stuff during sleep

id like to have trap catch stuff with sleep infinity for speed and cpu savings is this not possible? id assume just not ending the bash noscript with no sleep would fix this but cant find out how to do that

https://redd.it/y3e9dd
@r_bash
problem with automating artix linux install noscript

When I run my install noscript: https://github.com/Luharion/Lis/blob/master/lis.sh. It says

Error detected while processing command line

E492: not an editor command: PlugInstall|q|q

The function that is having problems is

vimplugininstall() {
# Installs vim plugins.
whiptail --infobox "Installing neovim plugins..." 7 60
mkdir -p "/home/$name/.config/nvim/autoload"
curl -Ls "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" > "/home/$name/.config/nvim/autoload/plug.vim"
chown -R "$name:wheel" "/home/$name/.config/nvim"
sudo -u "$name" nvim -c "PlugInstall|q|q"
}

Can anyone help me?

https://redd.it/y3ffie
@r_bash
A complex question

There are four type of files I have. I have 11 files in total. I am required to write a noscript that create 4 new sub directories named after the four type of files and move each file to the correct directory via making a loop variable which places all the files of the same type in the same directory

Let me use an analogy to help understand:

I have 11 fruits. Red Apple, Green Apple, Blue Banana, Red Banana, Yellow Banana, Blue Grape, Yellow Grape, Red Grape, Blue Watermelon, Green Watermelon & Yellow Watermelon.

All the fruits that I have mentioned are part of one of the following groups: Apple, Banana, Grape and Watermelon.

So to go back in Bash terms, I have to create a command that directs all the files of one particular group to a newly made directory which is only for that one particular group of files.

For example,

I need to make a command that makes Blue Banana, Red Banana and Yellow Banana move into one file called Banana. And it cannot be manually, the terminal needs to select it based on the attributes just described and move it there.

​

I know how to make directories, all of that but it's the making the loop variable that is confusing me so if anyone can help me explain how I could go about doing this. I would appreciate it.

https://redd.it/y3ezwy
@r_bash
I need help to fix my noscript for concatenation of xml files

Needing help with a concatenate of xml files.

Hello, I have been tasked to work with concatenating xml files from a path and merge them into a single xml.

I have the following noscript

​

#!usr/bin/sh
ORIGIN_PATH="/backup/data/export/imatchISO"
HISTORY_PATH="/backup/data/batch/hist"
SEND_PATH="/backup/data/batch/output"
DATE=`date +%y%m%d`
LOG="/backup/data/batch/log/concatIMatch_"$DATE

cd $ORIGIN_PATH

ls -lrt >> $LOG

cat $ORIGIN_PATH/SWIFTCAMT053_* >> $SEND_PATH/SWIFTCAMT053.XML_$DATE 2>> $LOG

mv $ORIGIN_PATH/SWIFTCAMT053_* $HISTORY_PATH >> $LOG 2>> $LOG


if [[ $(ls -A $SEND_PATH/SWIFTCAMT053.XML_$DATE) ]]; then
echo $(date "+%Y-%m-%d %H:%M:%S")" - Ficheros 053 concatenados" >> $LOG
mv $SEND_PATH/SWIFTCAMT053.XML_$DATE $SEND_PATH/SWIFTCAMT053.XML 2>> $LOG
exit 0
else
echo $(date "+%Y-%m-%d %H:%M:%S")" - ¡ERROR CON LOS FICHEROS 053 AL CONCATENAR!" >> $LOG
exit 1
fi


and what I have is a path containing several xml files with the same format:

​

<?xml version="1.0" ?>
<DataPDU xmlns:ns2="urn:swift:saa:xsd:saa.2.0">
<ns2:Revision>2.0.13</ns2:Revision>
<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

</DataPDU>

the thing is that when I concatenate with this is appending the end of the file to the next one , which is not the expected result as it is duplicating the xml declaration tag and the opening <DataPDU> and closing <DataPDU> for all files.

What I'm needing is to have a single xml file with the following sctructure

​

<?xml version="1.0" ?>
<DataPDU xmlns:ns2="urn:swift:saa:xsd:saa.2.0">
<ns2:Revision>2.0.13</ns2:Revision>
<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

<ns2:Header>
...
</ns2:Header>

<ns2:Body>
...
</ns2:Body>

</DataPDU>

So technically what I want is to have the first 3 lines and the last line only occurring once.

​

I have received a tip that I could do something with:

$ awk 'NR<3 {print} FNR>3 {print last} {last=$0} END{print}' *.xml

But I don't understand how to modify my noscript for this

Edit: I'm having also problems putting format from mobile to this thread

https://redd.it/y3rg99
@r_bash
Fuzzy file completion in bash

I used oh-my-zsh zsh on the computer of another person.

When I typed `md<tab>` it nicely completed to the first markdown file `first.md`. Can I have the same in bash?

https://redd.it/y3rmqs
@r_bash
Having trouble saving full sub directory names

I'm trying to save the folder name of sub directories, for example I want to store "long folder name" in x/folder/long folder name, but from folders with multiple words I'm only getting the first word and I can't figure out how to get the whole thing.

The whole thing does print when I just run the command in terminal though.

$ find . -type d -maxdepth 2 -mindepth 2
./folder/Apple Banana

Script:

#!/bin/bash
set -x

for directory in find . -type d -maxdepth 2 -mindepth 2
do
echo "$directory" exists here - do something with it
done

Output:

+ for directory in find . -type d -maxdepth 2 -mindepth 2
+ echo ./folder/Apple exists here - do something with it
./folder/Apple exists here - do something with it
+ for directory in find . -type d -maxdepth 2 -mindepth 2
+ echo Banana exists here - do something with it
Banana exists here - do something with it

Single word folders print as expected:

+ for directory in find . -type d -maxdepth 2 -mindepth 2
+ echo ./aaa/bbb exists here - do something with it
./aaa/bbb exists here - do something with it

https://redd.it/y3w2t2
@r_bash
GUI Sudo Password Prompto for launching Applications as root?

How does one prompt the GUI sudo password window to launch applications such as xed without launching a terminal window.

I want to create a launcher which will launched xed as root and be able to edit some files from XAMPP to allow me to do modify my XAMPP virtual hosts.

pkexec xed /etc/hosts /opt/lampp/etc/extra/httpd-vhosts.conf /opt/lampp/etc/httpd.conf

https://redd.it/y4ays3
@r_bash
What is the asterisk between the patterns, in Grep AND using -E?

grep -E 'pattern1.*pattern2' filename

I know this command should bring `pattern1` and `pattern2` in the file `filename`.

But what is the asterisk in the second pattern and not the first one, and what is its usage?

https://redd.it/y4pwh5
@r_bash
Is it possible to handle a command before it's passed to the bash command parser?

I'm not sure how to word the question correctly, so ill try to explain.

When you want to increment an integer, you have to do

NUMBER=$((NUMBER + 1))

I'm wanting to simplify this by doing what other languages do, which would be

NUMBER++

Is there something that I can make that looks through all the input before it's executed by bash? Because I know that I could just make a command and pass it in as an argument like

math NUMBER++

But id like this to be simpler than that.

&#x200B;

Sorry if this sounds weird. I'm not sure how bash handles commands, so I'm just going off of what I assume happens.

https://redd.it/y51b3q
@r_bash
Confusion with Bash and Z shell

Hi!

So I'm trying to install flutter on my macOS Monterey. When I first ran the command in terminal to see what shell I have it told me bash. I had some trouble finding the .bashrc file but eventually I was able to find it and make the necessary changes. But when I tried to run flutter in terminal it would not let me. Now when I run the command to see what shell I have it's telling me zsh. So I tried opening the .zshrc file in textedit to input path of my flutter git repo but it keeps telling me that I do not have permission to make changes to it. Does anyone know how I can fix this?

https://redd.it/y5owuv
@r_bash
What's the best way to localize (for different languages and locales) bash terminal apps?

Just wondering what a good solution to localize an existing app might be, with string text embedded in the noscripts. Refactoring isn't an obstacle (and eventual translation of text blocks), but what options are there in terms of libraries to achieve this. Thanks.

https://redd.it/y5rp0a
@r_bash
Using a bash noscript to streamline GitHub Actions

Using a bash noscript (based on bashew framework) to centralize the GitHub Action setup (e.g. pip install), Action payload and Action wrap-up (commit/push the results) in one place, so that the Action config YML becomes this simple:

- name: gha:before
run: |
./bashewgithubaction.sh gha:before
- name: gha:execute
run: |
./bashewgithubaction.sh gha:execute
- name: gha:after
run: |-
./bashewgithubaction.sh gha:after

Advantages:

Single Responsibility. You edit your setup code in the same file as the payload or the wrap-up code. You don’t touch the `action.yml` file anymore after initial setup.
Cleaner Code. The YML syntax forces you to a do this && do that && do the other
one-liner format. A bash noscript allows you to do if/then, for/do, while/do, recursion … in an easy and clean way.
Easy Debugging. `bashew` has built-in support for log files and optional verbose mode. It also cleans up log files after 30 days, so they don’t clog up your repo.
Self-Contained. You don’t need external libraries or runtimes. bash runs on most Action Runners (Linux/MacOS) and each bashew noscript contains the entire framework.

More details on blog.forret.com

https://redd.it/y5t8me
@r_bash
inline noscript runs as expected, however not when read from file

I am at a loss here. I write my code inline and it just works. Then I transfer the code into a .sh file and it no longer does.

I am in a directory /foo/bar and there is a subdirectory present named cfg

while true; do { if -d ./cfg ; then cd ./cfg/; break; fi; } done

Running this, it changes into the director cfg as expected. However after transferring it to a file:

1 #!/bin/bash
2
3 while true; do
4 {
5 if -d ./cfg ; then
6 cd ./cfg/; else
7 break;
8 fi;
9 }
10 done

it does nothing, outputs no errors and merely rests in the directory /foo/bar.

Any tips would be appreciated


Edit: Obviously it works fine while being run in the same shell with source noscript.sh . However I am looking for a way that it executes without the source command.

https://redd.it/y64byl
@r_bash