r_bash – Telegram
How to clean the terminal when running bash noscript (like when you open vim or less)?

Hi, I would like to know how to flush or clean the terminal when run noscript and at the end of the noscript back to what was before the noscript run.

For example, when I open a tool like vim or less The terminal window isn’t show the last commands, when I quit I back to the terminal, This is exactly what I would like to achieve.

https://redd.it/xh92pl
@r_bash
How to clean the terminal when running bash noscript (like when you open vim or less)?

Hi, I would like to know how to flush or clean the terminal when run noscript and at the end of the noscript back to what was before the noscript run.

For example, when I open a tool like vim or less The terminal window isn’t show the last commands, when I quit I back to the terminal, This is exactly what I would like to achieve.

https://redd.it/xh92pl
@r_bash
How to redirect or control ffmpeg output?

I want the error and processing messages to be stored in a log.txt , but I cant do it

ffmpeg song.mp3 > log.txt # this doesnt work

https://redd.it/xhhxa6
@r_bash
transposing text in a file rows and columns, using awk or possibly xargs.

from a leetcode question, just for fun, I have file.txt

name age
alice 21
ryan 30

I want to transpose this replacing each row into a column so that it becomes:

name alice ryan
age 21 30

I'm pretty new to awk, and played with using xargs redirection such as

xargs -n3 < file.txt

which wouldn't quite do the trick.

Wondering if there's a simple way to do this in awk.

https://redd.it/xhotw6
@r_bash
Correct way to pipe data to specific screen.

So i'm trying to pipe different data to different screens from ssh via crontab.

So the screens need to persist, even after logout of ssh.

And i need to be able to to see the specific screens later and see the piped data.

And i'm wondering about the correct way to do this since i get duplicates of the same data on different screens.

To watch an ongoing session i use

screen -x -S data

To exit current screen i use

ctrl a+d

to pipe data to a specific screen i've tried.

screen -S data bash -c "/noscript.sh" &

And

/files/noscript.sh >> /dev/pts/6 2>&1

The second option made the closest result of what iam looking for.

But the issue persist.

Duplicates of the same data gets on another screen.

Which is supposed to be for another data1/Screen

Even though i specify different tty's on different screens.

Is there another more stable solution than screen or is there a better solution?

https://redd.it/xhqs3x
@r_bash
Is there a way to quickly translate a command into human readable text?

Is there a way to quickly translate a command into human readable text? I sometimes see >> > * and various symbols and syntax and I would like to know if there's a way to quickly check what a command does.

https://redd.it/xhxx6p
@r_bash
Trying to run cat and forward to file without interpreting variables

I have a bash noscript where it attempts to cat a file, and send the output to a file somewhere else. The relevant blob is below:

cat > $PLUGIN_NAME/build.sh <<EOF
#!/bin/sh

# Set the variable MCSERVER to ~/Desktop/server
# unless it's already set
: ${MCSERVER:="/mnt/c/Users/<user>/Desktop/server"}

BUKKIT="$MCSERVER"/bukkit.jar

however, what actually ends up in the file is

#!/bin/sh

# Set the variable MCSERVER to ~/Desktop/server
# unless it's already set
: /home/<user>/Desktop/server

BUKKIT="$MCSERVER"/bukkit.jar

How can I get the `cat` output to forward to the secondary file without the variables being interpreted and sent to the file with their current values?

https://redd.it/xhxw8w
@r_bash
Issues with white spaces in file names

Bash noob here. I have a bunch of numbered directories that contain unremembered files. I want to create a noscript that renames files within each directory starting with the number of their parent directory. So from this :

.
├── 1-folder
│   ├── files1
│   ├── files2
│   ├── files3
│   └── files4
├── 2-folder
│   ├── files1
│   ├── files2
│   ├── files3
│   └── files4


I want to get this :

├── 1-folder
│   ├── 1-files1
│   ├── 1-files2
│   ├── 1-files3
│   └── 1-files4
├── 2-folder
│   ├── 2-files1
│   ├── 2-files2
│   ├── 2-files3
│   └── 2-files4



I created a noscript and it works if the directories and files don't have any space

for d in ; do
num=$(echo $d | cut -d'-' -f1)
for f in "$d"/
; do
mv $f $d/${num}-$(basename $f)
done
done

But I get an error when trying to do the same for directories that contain white spaces

.
├── 1- folder
│   ├── files 1.png
│   ├── files 2.png
│   ├── files3 3.mp4
│   └── files4 4.mp3
└── 2- folder
├── files 1.mp3
├── files 2.png
├── files 3.jpeg
└── files 4.pdf

error :

mv: target 'folder/1-' is not a directory

https://redd.it/xhrqg1
@r_bash
Script that runs every second-last sunday of every month

Hi everyone, i'm trying to create a noscript that run a command every second-last sunday of every month.

until now i'm here:

#!/bin/bash

d=$(date +%Y-%m-%d) echo "$d"

data=$(date +%Y-%m-%d -d '14 days') echo "$data"

nextmonth=$(date +"%B %Y" --date="$(date +%Y-%m) next month") echo "$nextmonth"

currentmonth=$(date +"%B" -d '14 days'="$(date +%Y-%m) next month") echo
"$current
month"

if $current_month -eq $next_month
then
echo "SCRIPT START"
else
echo "SCRIPT DON'T START"
fi

Can you help me and telling me what i'm doing wrong?

https://redd.it/xi9laz
@r_bash
Bash Game of Life (problem with neighbor detection)

GitHub - Bash Game of Life (WIP)

I've made the Game of Life in Python, but also wanted to write it in Bash since I'm new to it and want to get a feel for working with it. It was going okay until I started to test and make sure the correct next state is created, but it's having problems detecting neighbors on the bottom 3 cells and keeping alive cells that should remain alive.

I've set 3 cells in an L shape so that the next state will create a new cell inside of the L. The new cell is created, but the first 3 cells I had preset would disappear.

I've added a bunch of debug echo lines in the next_board function to show when/where neighbors are detected. The first cell that it checks (top-left) should have a neighbor on the bottom-right, but it doesn't. By the 8 or 9th cell, it starts to detect neighbors.

TLDR: Problems detecting any bottom 3 neighbors and keeping cells alive that are supposed to remain alive.

https://redd.it/xindh5
@r_bash
Bash Game of Life (problem with neighbor detection)

GitHub - Bash Game of Life (WIP)

I've made the Game of Life in Python, but also wanted to write it in Bash since I'm new to it and want to get a feel for working with it. It was going okay until I started to test and make sure the correct next state is created, but it's having problems detecting neighbors on the bottom 3 cells and keeping alive cells that should remain alive.

I've set 3 cells in an L shape so that the next state will create a new cell inside of the L. The new cell is created, but the first 3 cells I had preset would disappear.

I've added a bunch of debug echo lines in the next_board function to show when/where neighbors are detected. The first cell that it checks (top-left) should have a neighbor on the bottom-right, but it doesn't. By the 8 or 9th cell, it starts to detect neighbors.

TLDR: Problems detecting any bottom 3 neighbors and keeping cells alive that are supposed to remain alive.

https://redd.it/xindh5
@r_bash
how do we write a bash noscript to run python or pip commands.

Want to create a bash noscript to upload a custom package i created to pypi. Includes commands like
python setup.py sdist
Pip install twine
Twine upload dist/*
Twine upload must be able yo take user name and password as well.
Would be helpful if someone could point me towards any resource or example as I'm not familiar with noscripting.
Thankyou.

https://redd.it/xiyk0p
@r_bash
how do we write a bash noscript to run python or pip commands

Need to create a bash noscript that is able to upload a custom python package to pypi. Hence run python and pip command , also upload to pypi with the correct credentials.

Will something like this work if python is already installed in the environment i am running this.


#!/bin/bash
python setup.py sdist
pip install twine
twine upload dist/* -u 'username:password'

Nog familiar with bash, could someone point me towards any resource to do something like this.

https://redd.it/xiz6gc
@r_bash
Awk only processing the first word

I am trying to make a simple histogram for a text file.

declare -A letters

&#x200B;

awk 'BEGIN{FS=""}

{letters[$(NR)]++}

END {for(letter in letters){print letter ":" letters[letter]}}' input.txt

input.txt has Hello world

but the output is only H:1 and no other letters.

Can someone point out where I messed up? I am still very new to awk.

https://redd.it/xisr2a
@r_bash
Help embedding a command into a noscript

I am trying to automate with bash noscripting a few commands that I use to report on some things. The command runs a select statement fed into the executable as arguments.

This seems to work in a command line:

for i in server1 server2 server3 server4; do runexec -se=$i "select SERVERNAME, SCHEDULEDSTART, ACTUALSTART, SCHEDULENAME, cast ((NODENAME) as char (60)) as NODENAME , cast ((STATUS) as char (15)) as STATUS from STATUS, EVENTS where STATUS <> 'Completed' AND STATUS <> 'Future' AND CURRENTTIMESTAMP - 15 HOURS < SCHEDULEDSTART" >> /tmp/report-$date; done

But when I try to modify it for use in a noscript, I get all sorts of errors:

for i in $serverlist;
do
/usr/bin/runexec -se=$i "select SERVERNAME, SCHEDULEDSTART, ACTUALSTART, SCHEDULENAME, cast ((NODENAME) as char (60)) as NODENAME , cast ((STATUS) as char (15)) as STATUS from STATUS, EVENTS where STATUS <> 'Completed' AND STATUS <> 'Future' AND CURRENTTIMESTAMP - 15 HOURS < SCHEDULEDSTART" >> /tmp/report-$date
done

Can you offer some (gentle) guidance as how I can integrate double quotes, single quotes, and round brackets into a command in bash noscript? I've tried using a \ delimiter around them all, curly brackets instead of round brackets, but I'm getting mismatch errors

https://redd.it/xjcgcx
@r_bash
How to output all lines of a file up to a specific character?

>filename.txt
>
>12.345.678.901/23
>
>13.456.789.01/24
>
>13.345.678.901/25

I want to make a new file that is everything before the /

>newfile.txt
>
>12.345.678.901
>
>13.456.789.01
>
>13.345.678.901

https://redd.it/xjb1tt
@r_bash
yq REPL using fzf

Hello "bash noscripts" experts,

I am a beginner: new to fzf, yq (yaml), and bash noscripts.


The purpose of my noscript: I want to search for given keys in all yaml files under the current directory.


All yaml files are in the following sample format.


- Keys:
- k1
- k2
- k3
- AnotherField1: Value 11
- AnotherField2: Value 12

- Keys:
- k1
- k3
- k4
- AnotherField1: Value 21
- AnotherField2: Value 22

- Keys:
- k3
- k4
- k5
- AnotherField1: Value 31
- AnotherField2: Value 32



If a user enters k1 for keys, the following records should be returned.


- Keys:
- k1
- k2
- k3
- AnotherField1: Value 11
- AnotherField2: Value 12

- Keys:
- k1
- k3
- k4
- AnotherField1: Value 21
- AnotherField2: Value 22




If the user further filters using k1,k3 for keys, the following record should be returned.

    
- Keys:
- k1
- k3
- k4
- AnotherField1: Value 21
- AnotherField2: Value 22




I need to fetch AnotherField1, AnotherField2 fields for the selected record.

The corresponding yq query on terminal looks like:



yq eval '.[] | select(.Keys.[] | select(.== "k1")) | select(.Keys.[] | select(.== "k3")) | (.AnotherField1, .AnotherField2) ' **/*.yml



Below is my non-fzf version noscript-name.sh. Pardon my lack of knowledge. Please feel free to suggest improvements in the code.


#!/usr/bin/env bash

while [ "$1" != "" ]; do
case $1 in
--keys )
shift
keys=$1
;;
-h | --help )
exit
;;
* )
exit 1
esac
shift
done


if [ -z $keys ]; then
echo "The keys field is required."
exit
fi

# Parse comma seprated (delimiter) keys, and store in an array.
IFS=',' read -r -a array <<< "$keys"


# I am trying to build filters for yq here.
yq_filter=""

for ((idx=0; idx<${#array[@]}; ++idx)); do
if [ "$idx" != 0 ]
then
yq_filter="${yq_filter} | "
fi

yq_filter="${yq_filter}select( .Keys.[] | select ( .== \"${array[idx]}\" ) )"
done

# All Yaml files
files=$( find . -name *.yml )

# Final command
command='yq eval ".[] | $yq_filter | (.AnotherField1, .AnotherField2)" $files'

eval "$command"



If I enter a command like below, the noscript works fine.



./noscript-name.sh --keys k1,k3




However, I would like to use the noscript as REPL with the help of fzf.

Could someone please help with the following code? I only found single query {q} examples, but I need to parse the query where the comma is used as a delimiter. Is it possible? If not, please suggest an alternative approach. Thanks.


#!/usr/bin/env bash

IFS=: read -ra selected < <(
FZF_DEFAULT_COMMAND="??" \
fzf --ansi \
--disabled \
--bind "change:reload:sleep 0.1; ?? || true" \
--delimiter : \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
)



I also intend to capture the result i.e. AnotherField1, AnotherField2 fields, and pass them into a bash function as arguments.



function someBashFunction(param1, param2) {
# param1 and param2 should be AnotherField1, AnotherField2 of the selected record.
}




I am not sure about what the following code should look like. Once a record is selected, the record's AnotherField1, AnotherField2 fields should be passed to the someBashFunction.


[ -n "${selected[0]}" ] && someBashFunction(??, ??)




Additional question:


Please also suggest how you debug fzf code, like how do I find out the exact command that was passed to fzf.

https://redd.it/xjgcwn
@r_bash
preserve history in the tmux session when you reconnect with ssh?

Is this even possible?

https://redd.it/xjjk1e
@r_bash
Output somehow "escaping" from inside variable

I'm trying to create a noscript for detecting the current song in quodlibet. It all works perfectly except for one strange thing: everything works, except when there's a certain output. Here it is:

&#x200B;

$ quodlibet --print-playing

Quod Libet is not running (add '--run' to start it)

&#x200B;

This output refuses to be assigned to a variable, and gets output into stdout.

&#x200B;

$ songstat=$(quodlibet --print-playing)

Quod Libet is not running (add '--run' to start it)

$ echo $songstat

[something else]

&#x200B;

If a song is playing, it works just fine

$ songstat=$(quodlibet --print-playing)

$ echo $songstat

Dean Martin - Mambo Italiano

&#x200B;

I'm puzzled. Is it the "--run" which is causing problems?

https://redd.it/xjg8xk
@r_bash
i need help with bash (18f)

we can be friends but please i really need help with this bash lab so kind of a tutor

https://redd.it/xjnywx
@r_bash