r_bash – Telegram
how to show progress bar of currently running bash noscript

I have a noscript (wrapper) that calls for another noscript (main), when I launch it, it does not really show anything, I would like for the wrapper noscript to show a progress bar of the main noscript and when main reaches to the end of the file, wrapper shows a complete progress bar :

​

wrapper.sh

#!/bin/bash
main.sh | tee | aha > generated.html

main.sh is noscript that has multiple commands to run, about 335 line

How can I achieve this?

https://redd.it/164lamu
@r_bash
Powershell Script Help
https://redd.it/1651r0h
@r_bash
Why does ls output everything on one line but if we write ls to a file, there are newlines?

$ ls
gives us
file1 file2 file3 ... etc

--------------------------------
$ls > newfile
$cat new
file
gives us

file1

file2

file3

Why doesn't the native ls command output the files with newlines?

echo -e $(ls) doesn't output the newlines either

https://redd.it/1656186
@r_bash
Script runs perfectly when run manually but gives an error when run from crontab

- I am trying to do something very simple
- Send an email via AWS SES from within an EC2 instance
- I have an IAM instance role configured on EC2 which has the appropriate permissions to send an email using SES

Here is my test.sh noscript which is stored at /home/ec2-user
#!/bin/bash
from="cron@example.com"
to="example@gmail.com"
subject="Test email sent at $(date)"
text="Test email with test data sent at test date $(date)"
aws ses send-email --from "${from}" --subject "${subject}" --text "${text}" --to "${to}"

When I run this noscript manually using
./test.sh

It sends me the email successfully and I can see this output (checked my inbox too and it works)
{
"MessageId": "0200028a46979f63-3b413e00-4c88-4504-b677-d908007e032d-000000"
}

But when I created a crontab -e as follows
*/15 * * * * bash /home/ec2-user/test.sh >> /tmp/cron-test.log 2>&1

I see an error inside the tmp/cron-test.log file
You must specify a region. You can also configure your region by running "aws configure".

I did not create an AWS credentials because I am assuming the EC2 instance with the IAM role is automatically going to create temporary credentials for me? Why is this not working? I dont want to store access key id and secret on AWS ec2 as it goes against best practices. How do I fix this?

https://redd.it/165ev1q
@r_bash
One command line to open sh app$ and execute command 🤔

Hi i have docker on Vps and i want to check some state of my nodes in container. I think the best way to do that is make bash noscript, which loop command over all containers and read status.

I want to do that remotely with paramiko (Python library).

I think i should do it in one line, and here is my problem idk how to do it in one line.


docker exec - it d1 bin/sh <- bash


cd /root/.folder & & ./app.sh <-shell


COMMAND - - status <- app$



Is it possible to do it in one line? 🤔

https://redd.it/165jow5
@r_bash
Error: Value too great for base

Hey everyone, I wrote a bash noscript that is suppose to automate the creation of an AWS resource (specifically the noscript helps to create EBS snapshots).

Volume_map takes the volume IDs, which is a required credential to create the snapshot.

volume_map["vol-0b235e5983aae1aef"]="Folder1"
volume_map["vol-050ec02d0a7d2288b"]="Folder2"

But when i run the noscript I get an error

./create_snapshot.sh: line 5: vol-050ec02d0a7d2288b: value too great for base (error token is "050ec02d0a7d2288b")

https://redd.it/165l5ut
@r_bash
can only read out last thing added to array

function arraystest {

declare -a item
type
declare -a itemname
declare -a season
number
declare -a episodenumber

echo "num: ${num}"
echo -e

read -p -r "what type of item? " item
type${num}
echo -e
read -p "name of item? " itemname[${num}]
echo -e
read -p "season number? " season
number${num}
echo -e
read -p "episode number? " episodenumber[${num}]
echo -e

(( num++ ))

if (($num > 2))
then clear
printout
else arrays
test
fi
}

function clearprintout {

clear
printout
}

function dump {

echo "string number 0 : ${item
type0} ${itemname[0]} ${seasonnumber0} ${episodenumber[0]}"
echo "string number 1 : ${item
type1} ${itemname[1]} ${seasonnumber1} ${episodenumber[1]}"
echo "string number 2 : ${item
type2} ${itemname[2]} ${seasonnumber2} ${episodenumber[2]}"
echo -e
echo "item
type: ${itemtype[*]}"
}

function print
out {

echo "printnum: ${printnum}"
echo -e
echo "string number ${printnum} : ${itemtype[${printnum}]} ${itemname${printnum}} ${seasonnumber[${printnum}]} ${episodenumber${printnum}}"

(( printnum++ ))

if (($printnum > 2))
then dump
else printout
fi
}

# main

num=0
printnum=0
arrays
test


This code is meant to ask a set of four questions, with the answer to each question going in an array. It's meant to do this three times, then print them out.

As far as I can tell, it's throwing away previous answers and keeping only the last one.

Where am I going wrong?

https://redd.it/165jifv
@r_bash
Can someone explain these wildcards with my shell expansions?

locate /bin/zip - This command works (and outputs files like usr/bin/zipgrep, etc) but I thought there was an implicit wildcard like this locate */bin/zipand that's why it locates all the directories that house /bin/zip

So then I tried locate "*/bin/zip". No output... if usr/bin/zipgrep matched with command locate /bin/zip, why wouldn't anything appear in the output for locate "*/bin/zip"?

locate "/bin/*zip*" - This command also does not work but I'm thinking it should because the wildcards loosen up the search parameter.

I'm not sure why these wildcards are making my search results more restrictive than not having them in the first place.

https://redd.it/165x09z
@r_bash
Eases that make writing Bash code easier?

It's been a while since I write bash noscripts (around 1 year) and the environment is always the same: the so well known and always used - for me \- in its most basic essence VIM. VIM is so settled in me that it took a really long process to write C code on VSCode instead of VIM. However, I do not intend to leave VIM and I suppose that many of you guys write your bash noscripts through this editor.

Therefore, I'd like to know from you things that you have plugged in (and consider indispensable nowadays) on the editor so that writing bash code turned easier. It can be anything like, for example, autocomplete. If it's not a pain for you, it'd be nice to know how to implement this feature on someone's else machine, so feel free to throw links too!

https://redd.it/166oo3d
@r_bash
Beginner needs help with a short AWK function

Hi all. I'm trying to create a short bash noscript to assist me at work. I want to take the output of a command (which displays the install history of a server). Here is an example of what typical output of the history command looks like:

(install command)<machine name>

(machine name) | (install date) | (install time) | (install log id number) | (install status)

&#x200B;

The (install status) field can be either "Successful" or "Failed".

I want to change the color and also make the text bold for both the first and last fields. But I want to specifically only change the font color in the last field to green if the last field contains the word "Successful" and if it contains the word "Failed" I want the font color for the last field to be red.

Then I want to display the entire output of the install command with the modified first and last fields pasted in.

So the goal output would look like:

(machine name in blue font color + bold) | (install date) | (install time) | (install log id number) | (install status ("Successful" <-- Green font or "Failed" <-- Red font) |

I have gotten this to work but it is a horrible way of doing it, it's about 100 lines of code. I think the command I really need to be using is awk(match) but I cannot find good guides on how to use it. I was able to get it to work to replace a certain word with another word, but I don't know how to use awk + match to change the font formatting like color and bold. Thank you to anyone who can help!!

https://redd.it/166sdag
@r_bash
Difference between \( and "(

There's this example in the book I'm reading

find /etc \( -type d -not -perm 0777 \). So I understand the backslashes are escaping the parenthesis creating the group but then I wanted to try find /etc "( -type d -not -perm 0777 )" but it gives different results. Don't the quotes also escape the parenthesis?

https://redd.it/166urz2
@r_bash
Efficiently replace a substring within a string

I want to replace a substring within a string. Is there a way, maybe with awk, to just replace the, let's say, second substring with another string? Something like replace $2 of variable1 with $substring (or "substring"). This would be very convenient, this way I could skip the step to grep the actual substring in the variable and then replace this string for the new string, e.g. with sed.

https://redd.it/167cck1
@r_bash
Bash Scripting

Any resources to keep noscripts syntax skills sharp or advance them?

I've done a few courses on pluralsight but I don't often find myself noscripting much at work (I am in INFO SEC but most of our tools are already written) and with almost every coding I've tried to learn I will undoubtedly forget everything I've learned if I don't reinforce the skills for awhile.

Thank you.

https://redd.it/167h9z9
@r_bash
is it possible to count the number of rows printed to in the terminal?

I'm working on a program that takes the size of the terminal into account. So far, I've got it adapting all the text output to the width, no problem at all.

The design of the program prints a bunch of text to the top of the screen, has a space, then prints some more at the bottom. (That space will be replaced later on with a table of data)

The blank space should be the bit that scales to fit vertically. I could cheat, and set something up with a fixed variable for the top and bottom half. But I was hoping for something robust that could handle any changes I might add to the top.

So, what I want to know is, is there a way to cook up a function that will count the number of terminal rows (top to bottom) that have been written to at the point it's envoked?

Say my program runs:

clear
echo "first line"
echo "second line"
echo "third line"
magicnumber linecount
echo "fourth line"

magic
number runs and makes $linecount=3. It doesn't make it 4 because the fourth hadn't happened when it was envoked.

possible?

https://redd.it/167jfs4
@r_bash
Potential incompatible change to printf's '%b' format specifier

The people coming up with the next C language standard are proposing new printf() format specifiers %b and %B, to output integers as binary literals, i.e. 0b00010110, the binary representation of 22.

The section of the Bash manual describing the builtin printf command references the manual for the external printf program, printf(1); which itself references the manual for the C function printf(), printf(3). The Bash builtin printf command supports a bunch of C-language printf() format specifiers, so they just direct you to that documentation to see how those work.

However, the Bash builtin printf supports additional format specifiers, beyond those supported by the C-language printf(). One of those is %b, which in the case of Bash's printf, currently "causes printf to expand backslash escape sequences in the corresponding argument in the same way as echo -e."

A number of people from the Austin Group, the people behind the POSIX specification, emailed in to the bug-bash email list, where Bash development discussion takes place, saying that they would like a new POSIX standard to change the mandated behavior of the %b format specifier used by the command-line printf command, whether that's an external program or the Bash builtin, to mirror the behavior of this format specifier as used by the C printf() function in the upcoming C standard. They are recommending %#s, signifying an alternative string format, to take over the duties of the current %b. They do seem to be saying that they'd only change the standard in this way if they get buy-in from shell authors, though.

Chet Ramey, the Bash maintainer, really wasn't having it:

> I don't have a problem adding %#s. I have a problem with POSIX not seeing that the printf builtin is not a direct parallel to the library function and forcing an incompatible change.

So, do noscripts you write or maintain use the %b printf format specifier? What's your take on all this?

https://redd.it/167pnkt
@r_bash
Is there a keyboard shortcut to navigate through to the next or previous whitespace?

If I have a string, sdf df ddg'dfgd fg'dgffgms...dfdsg re rg./ert re/tr.et/ret/rtr, in the commandline and I press alt + b or alt + f, it only transverses to the next nonletter symbol (.,/ etc).

Is there a way to jump from whitespace to whitespace ( and allow me to delete the word between the two whitespaces)?

https://redd.it/167r3q7
@r_bash
How to send password to redis-cli , perform the login and keep redis-cli open with a bash noscript?

#!/usr/bin/env bash

# Unofficial Bash Strict Mode
set -e
set -E
set -o pipefail
set -u
set -x

IFS=$'\n\t'
# End of Unofficial Bash Strict Mode


host="localhost"
password="somepassword"
port="6379"
db="0"

redis-cli -h "${host}" -p "${port}" -n "${db}"
# HOW TO send AUTH command with password and keep the redis-cli OPEN?

https://redd.it/168rqbs
@r_bash
Please tell me why running cat returns contents but running cat within echo or printf statements it says the file doesn't exits

This command works:

docker exec --user root myapp cat admin/password

output:

KMZWaE2r81b(cT5x&(S7Wg7CJ

When I run it like this:

docker exec --user root myapp echo "ADMIN PASSWORD: " && "$(cat admin/password)"

output:

ADMIN PASSWORD:
cat: admin/password: No such file or directory

I attempted with printf as well:

docker exec --user root myapp printf "ADMIN PASSWORD: \"%s\"", "$(cat admin/password)"

output:

cat: admin/password: No such file or directory

At this point I am extremely confused why this is happening.

https://redd.it/1690klv
@r_bash
Why is this noscript not working to print the logs between two time duration?

LC_ALL=C awk -v beg=10:00:00 -v end=13:00:00 '
match($0, /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/) {
t = substr($0, RSTART, 8)
if (t >= end) selected = 0
else if (t >= beg) selected = 1
}
selected'


Why do you think is this not working in some servers, whereas working in some servers?

I invoke it as
bash noscript.sh application.log

https://redd.it/16920u2
@r_bash
How do I find the field names for the output of a command?

Something easy like ls -l, it outputs data but the column name isn't displayed. I looked in man ls but there isn't any information either.

https://redd.it/16939ml
@r_bash