bash-boost 1.14: now with directory bookmarks
https://asciinema.org/a/584985
https://github.com/tomocafe/bash-boost
I added a new bookmark package to the bash-boost interactive module to quickly move around to directories. I use
bash-boost has lots of other useful functions, if you haven't seen it, please check it out!
https://redd.it/13i7h2e
@r_bash
https://asciinema.org/a/584985
https://github.com/tomocafe/bash-boost
I added a new bookmark package to the bash-boost interactive module to quickly move around to directories. I use
bind to map these functions to keyboard shortcuts: Ctrl+B to create and/or go to a bookmark and Ctrl+X Ctrl+B to delete bookmarks. There's also functions to load bookmarks from a file (probably most useful in .bashrc) and show/get bookmarks.bash-boost has lots of other useful functions, if you haven't seen it, please check it out!
https://redd.it/13i7h2e
@r_bash
asciinema.org
bash-boost bookmarks
Recorded by tomocafe
Alternatives to eval that don't create a subshell
Basically I want to run a noscript that respects the aliases of the environment in which it's executed. I could make a noscript to convert all the aliases into functions in the same noscript and most should work but I bet there's a more elegant solution. For those curious this is the part of the code that I'm trying to use:
eval "$(echo -e "...commands" | dmenu_instant -l 5 -p "These are the commands: " -fn "monospace:size=9" )"
https://redd.it/13ia9qj
@r_bash
Basically I want to run a noscript that respects the aliases of the environment in which it's executed. I could make a noscript to convert all the aliases into functions in the same noscript and most should work but I bet there's a more elegant solution. For those curious this is the part of the code that I'm trying to use:
eval "$(echo -e "...commands" | dmenu_instant -l 5 -p "These are the commands: " -fn "monospace:size=9" )"
https://redd.it/13ia9qj
@r_bash
Reddit
r/bash on Reddit: Alternatives to eval that don't create a subshell
Posted by u/Velascu - No votes and 1 comment
Loco.sh: program your env
Loco.sh helps you program UNIX configurations, known as profiles.
Maintaining noscripts and dotfiles over many systems, manually bootstrapping VMs, new laptops, complex DSLs impossible to forget at night, this is over.
https://github.com/t0pd4wn/loco.sh
https://i.redd.it/r13b8cgt980b1.gif
https://redd.it/13jbp91
@r_bash
Loco.sh helps you program UNIX configurations, known as profiles.
Maintaining noscripts and dotfiles over many systems, manually bootstrapping VMs, new laptops, complex DSLs impossible to forget at night, this is over.
https://github.com/t0pd4wn/loco.sh
https://i.redd.it/r13b8cgt980b1.gif
https://redd.it/13jbp91
@r_bash
How to display the stdout data from background subfunctions, when calling bash noscripts using "sudo -u"?
I have a bash noscript that has an internal function, which in turn is started as a background process. The main noscript basically sets up the subfunction, starts it, and then exists (I need to do this since this noscript is supposed to be able to be used as a systemd oneshot service).
When I run the noscript, I see the stdout output from both the main noscript and the subfunction, no problem.
When I start the noscript using "sudo -u anotheruser mynoscript.sh", I only see the stdout output from the main noscript, but nothing from the subfunction (which is perhaps not surprising since the
​
One possible workaround I can think of is to redirect stdout to a file under /tmp, and read it as the original user with "tail -f" or similar, but is there a better way to see all outputs, even after the
https://redd.it/13k21n3
@r_bash
I have a bash noscript that has an internal function, which in turn is started as a background process. The main noscript basically sets up the subfunction, starts it, and then exists (I need to do this since this noscript is supposed to be able to be used as a systemd oneshot service).
When I run the noscript, I see the stdout output from both the main noscript and the subfunction, no problem.
When I start the noscript using "sudo -u anotheruser mynoscript.sh", I only see the stdout output from the main noscript, but nothing from the subfunction (which is perhaps not surprising since the
sudo command terminates just about then).​
One possible workaround I can think of is to redirect stdout to a file under /tmp, and read it as the original user with "tail -f" or similar, but is there a better way to see all outputs, even after the
sudo command has terminated? I.e., some way to direct stdout from "anotheruser" to the normal user session?https://redd.it/13k21n3
@r_bash
Insert command output into current cursor location?
Please don't mind my English since it's not my mother tongue.
Basically, I want to insert some command's output to current line.
Take
Say my cursor is at the end of
I heard that When shell-command is executed, the shell sets the READLINE_LINE variable to the contents of the readline line buffer, but
Is this achievable using only bash builtins?
Thanks in advance!
https://redd.it/13k54ua
@r_bash
Please don't mind my English since it's not my mother tongue.
Basically, I want to insert some command's output to current line.
Take
fzf as an example.Say my cursor is at the end of
~$ vim , now I press some key and fzf selection menu pops up. After file selection and exiting of fzf, bash current line looks like ~$ vim OUTPUT_STR_HERE.I heard that When shell-command is executed, the shell sets the READLINE_LINE variable to the contents of the readline line buffer, but
bind -x '...' print output to the previous line.Is this achievable using only bash builtins?
Thanks in advance!
https://redd.it/13k54ua
@r_bash
Stack Overflow
Can "bind -x" access the current command line?
When I type some command, I sometimes want to read the help of the command. For example, when I'm typing
sort --overwrite some_texI # I is a cursor
, I'd like to check if sort command has --overw...
sort --overwrite some_texI # I is a cursor
, I'd like to check if sort command has --overw...
Bash noscript collection
I created a bash noscript collection github repo https://github.com/jothi-prasath/bash-noscripts
currently there is less noscripts.
checkout the repo and contribute your noscripts.
https://redd.it/13k8b5j
@r_bash
I created a bash noscript collection github repo https://github.com/jothi-prasath/bash-noscripts
currently there is less noscripts.
checkout the repo and contribute your noscripts.
https://redd.it/13k8b5j
@r_bash
GitHub
GitHub - jothi-prasath/bash-noscripts: Collection of bash noscripts
Collection of bash noscripts. Contribute to jothi-prasath/bash-noscripts development by creating an account on GitHub.
Seeking critique: shell function for reading a password into a variable
This is something I wrote quite a while ago, but I am now wondering whether there's a better way. In particular, I'm not happy with the use of
# password <varname>: prompt for and read a password into varname.
password () {
[ -t 0 ] || { echo "$FUNCNAME: stdin is not a tty"; return 2; }
# We use the var=$(foo) && eval construct so that we can run foo
# in a subshell but still get data back from it.
# Since this variable would clash with the user-supplied variable
# name in the eval, we pick something unlikely to be used.
local passwdstr=$(
local ch='' str=''
# Restore echo even if we die
trap 'stty echo' EXIT
# Just read -s is not enough, because characters might
# arrive between calls to read; we must keep echo disabled
# the whole time.
stty -echo
printf "Password: " >&2
while
IFS='' read -r -s -n 1 ch || {
exit 1;
}
# Is the character not a newline?
[[ "$ch" ]]
do
str="$str$ch"
printf "*" >&2
done
printf "%s" "$str"
echo >&2
) && eval "${1-REPLY}="'$passwdstr';
}
https://redd.it/13kcizq
@r_bash
This is something I wrote quite a while ago, but I am now wondering whether there's a better way. In particular, I'm not happy with the use of
eval. Also, I'm not convinced the use of trap is entirely bullet-proof.# password <varname>: prompt for and read a password into varname.
password () {
[ -t 0 ] || { echo "$FUNCNAME: stdin is not a tty"; return 2; }
# We use the var=$(foo) && eval construct so that we can run foo
# in a subshell but still get data back from it.
# Since this variable would clash with the user-supplied variable
# name in the eval, we pick something unlikely to be used.
local passwdstr=$(
local ch='' str=''
# Restore echo even if we die
trap 'stty echo' EXIT
# Just read -s is not enough, because characters might
# arrive between calls to read; we must keep echo disabled
# the whole time.
stty -echo
printf "Password: " >&2
while
IFS='' read -r -s -n 1 ch || {
exit 1;
}
# Is the character not a newline?
[[ "$ch" ]]
do
str="$str$ch"
printf "*" >&2
done
printf "%s" "$str"
echo >&2
) && eval "${1-REPLY}="'$passwdstr';
}
https://redd.it/13kcizq
@r_bash
Reddit
r/bash on Reddit: Seeking critique: shell function for reading a password into a variable
Posted by u/neilmoore - No votes and no comments
need help with xdg-open noscript
Hello I'm a Linux noob trying to get my all my application to work on Linux. I finally got Unreal Engine 5 to work on Ubuntu, but I'm having trouble getting a bash noscript to open Unreal Editor and would like some help.
\#! /bin/bash
xdg-open "../UnrealEngine/Engine/Binaries/Linux/UnrealEditor"
It will open the file location if I don't put "UnrealEditor" but when I do it gives me a
:Failed to find default application for content type ‘application/x-executable’
any advice would help
https://redd.it/13kf6hd
@r_bash
Hello I'm a Linux noob trying to get my all my application to work on Linux. I finally got Unreal Engine 5 to work on Ubuntu, but I'm having trouble getting a bash noscript to open Unreal Editor and would like some help.
\#! /bin/bash
xdg-open "../UnrealEngine/Engine/Binaries/Linux/UnrealEditor"
It will open the file location if I don't put "UnrealEditor" but when I do it gives me a
:Failed to find default application for content type ‘application/x-executable’
any advice would help
https://redd.it/13kf6hd
@r_bash
Reddit
r/bash on Reddit: need help with xdg-open noscript
Posted by u/DomFreecss - No votes and 1 comment
showing system stats during startup (novice showcase)
Hi. I have been tinkering around with bash and have the following setup to email me some stats during startup -
## cat /etc/systemd/system/customstartup.service
[Unit]
Denoscription=Notify that system has booted.
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
ExecStart=/bin/bash /home/user/bin/startupbootstrap.sh
Install
WantedBy=multi-user.target
and here's the noscript
## cat /home/user/bin/startupbootstrap.sh
#!/usr/bin/env bash
hostname=$(hostname -s)
lastreboot=$(command last -xF reboot | head -1 | awk {'print $5, $6, $7, $8'})
lastshutdown=$(command last -xF shutdown | head -1 | awk {'print $5, $6, $7, $8'})
uptime=$(uptime -p)
lastboot=$(who -b) # display time of last system boot
avgload=$(grep 'cpu ' /proc/stat | awk '{cpuusage=($2+$4)*100/($2+$4+$5)} END {print cpuusage "%"}')
cpuusage=$(top -n 1 | head -n 3 | tail -n 1 | awk '{print $2 + $4}')
day=`date +"%F %T"`
printf "[ %s ] %s\n\n" "$day" "$hostname"
printf "System powered ON at %s. " "$lastreboot"
printf " %s \n" "$uptime"
printf "Last successful shutdown at %s \n\n" "$lastshutdown"
printf " %s Avg. CPU Load\n" "$avgload"
printf "[ %s ] CPU Usage\n" "$cpuusage"
Any way I can improvise the noscript ? Thanks in advance.
cpu load source1 source2
https://redd.it/13kr1t1
@r_bash
Hi. I have been tinkering around with bash and have the following setup to email me some stats during startup -
## cat /etc/systemd/system/customstartup.service
[Unit]
Denoscription=Notify that system has booted.
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
ExecStart=/bin/bash /home/user/bin/startupbootstrap.sh
Install
WantedBy=multi-user.target
and here's the noscript
## cat /home/user/bin/startupbootstrap.sh
#!/usr/bin/env bash
hostname=$(hostname -s)
lastreboot=$(command last -xF reboot | head -1 | awk {'print $5, $6, $7, $8'})
lastshutdown=$(command last -xF shutdown | head -1 | awk {'print $5, $6, $7, $8'})
uptime=$(uptime -p)
lastboot=$(who -b) # display time of last system boot
avgload=$(grep 'cpu ' /proc/stat | awk '{cpuusage=($2+$4)*100/($2+$4+$5)} END {print cpuusage "%"}')
cpuusage=$(top -n 1 | head -n 3 | tail -n 1 | awk '{print $2 + $4}')
day=`date +"%F %T"`
printf "[ %s ] %s\n\n" "$day" "$hostname"
printf "System powered ON at %s. " "$lastreboot"
printf " %s \n" "$uptime"
printf "Last successful shutdown at %s \n\n" "$lastshutdown"
printf " %s Avg. CPU Load\n" "$avgload"
printf "[ %s ] CPU Usage\n" "$cpuusage"
Any way I can improvise the noscript ? Thanks in advance.
cpu load source1 source2
https://redd.it/13kr1t1
@r_bash
Unix & Linux Stack Exchange
how to check CPU usage at linux system startup?
I am stuck on finding a way to test CPU usage for a linux system at startup, usuing top or htop is too late for me to see the real usage at startup; is there an efficient way to it ?
Bash newbie here, wanted to know your opinion
Hi, I've already checked the style with shellcheck and everything seems fine, however I would like to know the opinion of someone who knows a little bit more about bash than me, it's my first noscript afterall and it's probably a mess. Ty in advance. Here's the github page with a readme if something isn't clear https://github.com/Vdevelasco/quickCommand ty in advance
#!/bin/bash
usage() {
echo "Usage: $0 -ihrlcmsexd -a command -f file -b size"
echo "Run qc -i first to set up files needed"
echo " -a: adds a new command to the bottom of the stack"
echo " -i: (init) has to be run one time before using any other commands"
echo " -h: (help) outputs this message"
echo " -r: removes a specific command from the list"
echo " -l: lists all stored commands"
echo " -c: creates a simple noscript with all the stored commands"
echo " -m: modifies the command in a specific slot"
echo " -s: switches two commands"
echo " -b: changes the size of the stack"
echo " -e: loads the commands in a file that can be used as a noscript"
echo " -x: lists avaliable command lists"
echo " -d: lists the directories used by qc"
}
#TODO:Make a flag for changing the arguments of the commands
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace #To debug
QCDIR=~/.local/share/qc/
COMMANDFILE="${QCDIR}qccommands"
VARASSIGNEDFLAG=false
STACKFILE="${QCDIR}qcmaxstacksize"
SCRIPTDIR="${QCDIR}noscripts/"
if -f $STACK_FILE ; then
if -z ${max_stacksize+x} ; then
maxstacksize=$(cat $STACKFILE)
else
echo "var is set to '$maxstacksize'"
fi
fi
isAssigned() {
USEDSLOTS=$(sed 's/|/ /' $COMMANDFILE | awk '{print $1, $8}')
if [[ "$USEDSLOTS" == "$1" ]]; then
echo "That slot is already used by another command"
VARASSIGNEDFLAG=true
else
VARASSIGNEDFLAG=false
fi
}
addCommand() {
numberoflines=$(wc -l $COMMANDFILE | cut -c 1)
isAssigned "$1";
if [ ! $VARASSIGNEDFLAG == true ] ; then
if [ "$maxstacksize" -lt $((numberoflines + 1)) ]; then
echo "Deleting first command from stack"
tail -n $((maxstacksize - 1)) $COMMANDFILE | sponge $COMMANDFILE
awk '{ print substr($0, index($0,$2)) }' $COMMANDFILE | sponge $COMMANDFILE
awk '{print NR-1 " " $s}' $COMMANDFILE | sponge $COMMANDFILE
echo "$((numberoflines - 1)) $(echo "$1")" >> $COMMANDFILE
cat $COMMANDFILE
else
echo "$numberoflines $1" >> $COMMANDFILE
cat $COMMANDFILE
fi
fi
}
touch $COMMANDFILE
while getopts "ia:f:hrlcmsb:exdu" options; do
case "${options}" in
i)#PERF:
if ! -d $QC_DIR ; then
mkdir $QCDIR
fi
echo "qc directory has been created succesfully"
touch $COMMANDFILE
sudo chmod 755 $COMMANDFILE
echo "qccommands has been created succesfully"
touch $STACKFILE
sudo chmod 755 $STACKFILE
echo 5 > $STACKFILE
echo "qcmaxstacksize has been created succesfully"
if [ ! -d $SCRIPTDIR ]; then
mkdir $SCRIPTDIR
fi
echo "noscript directory has been created succesfully"
exit
;;
a)#PERF:
command="${@:2}"
addCommand "$command"
exit
;;
#Make an noscript based on commands
c)#PERF:
noscriptname="${QCDIR}noscripts/${2?Error: no filename given, please see usage or provide a default file from configuration}"
rm $noscriptname &
touch $noscriptname
echo "#!/bin/bash" >> $noscriptname
awk '{$1=""}1' $COMMANDFILE >> $noscriptname
echo "The noscript $noscriptname has been saved in $SCRIPTDIR
Hi, I've already checked the style with shellcheck and everything seems fine, however I would like to know the opinion of someone who knows a little bit more about bash than me, it's my first noscript afterall and it's probably a mess. Ty in advance. Here's the github page with a readme if something isn't clear https://github.com/Vdevelasco/quickCommand ty in advance
#!/bin/bash
usage() {
echo "Usage: $0 -ihrlcmsexd -a command -f file -b size"
echo "Run qc -i first to set up files needed"
echo " -a: adds a new command to the bottom of the stack"
echo " -i: (init) has to be run one time before using any other commands"
echo " -h: (help) outputs this message"
echo " -r: removes a specific command from the list"
echo " -l: lists all stored commands"
echo " -c: creates a simple noscript with all the stored commands"
echo " -m: modifies the command in a specific slot"
echo " -s: switches two commands"
echo " -b: changes the size of the stack"
echo " -e: loads the commands in a file that can be used as a noscript"
echo " -x: lists avaliable command lists"
echo " -d: lists the directories used by qc"
}
#TODO:Make a flag for changing the arguments of the commands
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace #To debug
QCDIR=~/.local/share/qc/
COMMANDFILE="${QCDIR}qccommands"
VARASSIGNEDFLAG=false
STACKFILE="${QCDIR}qcmaxstacksize"
SCRIPTDIR="${QCDIR}noscripts/"
if -f $STACK_FILE ; then
if -z ${max_stacksize+x} ; then
maxstacksize=$(cat $STACKFILE)
else
echo "var is set to '$maxstacksize'"
fi
fi
isAssigned() {
USEDSLOTS=$(sed 's/|/ /' $COMMANDFILE | awk '{print $1, $8}')
if [[ "$USEDSLOTS" == "$1" ]]; then
echo "That slot is already used by another command"
VARASSIGNEDFLAG=true
else
VARASSIGNEDFLAG=false
fi
}
addCommand() {
numberoflines=$(wc -l $COMMANDFILE | cut -c 1)
isAssigned "$1";
if [ ! $VARASSIGNEDFLAG == true ] ; then
if [ "$maxstacksize" -lt $((numberoflines + 1)) ]; then
echo "Deleting first command from stack"
tail -n $((maxstacksize - 1)) $COMMANDFILE | sponge $COMMANDFILE
awk '{ print substr($0, index($0,$2)) }' $COMMANDFILE | sponge $COMMANDFILE
awk '{print NR-1 " " $s}' $COMMANDFILE | sponge $COMMANDFILE
echo "$((numberoflines - 1)) $(echo "$1")" >> $COMMANDFILE
cat $COMMANDFILE
else
echo "$numberoflines $1" >> $COMMANDFILE
cat $COMMANDFILE
fi
fi
}
touch $COMMANDFILE
while getopts "ia:f:hrlcmsb:exdu" options; do
case "${options}" in
i)#PERF:
if ! -d $QC_DIR ; then
mkdir $QCDIR
fi
echo "qc directory has been created succesfully"
touch $COMMANDFILE
sudo chmod 755 $COMMANDFILE
echo "qccommands has been created succesfully"
touch $STACKFILE
sudo chmod 755 $STACKFILE
echo 5 > $STACKFILE
echo "qcmaxstacksize has been created succesfully"
if [ ! -d $SCRIPTDIR ]; then
mkdir $SCRIPTDIR
fi
echo "noscript directory has been created succesfully"
exit
;;
a)#PERF:
command="${@:2}"
addCommand "$command"
exit
;;
#Make an noscript based on commands
c)#PERF:
noscriptname="${QCDIR}noscripts/${2?Error: no filename given, please see usage or provide a default file from configuration}"
rm $noscriptname &
touch $noscriptname
echo "#!/bin/bash" >> $noscriptname
awk '{$1=""}1' $COMMANDFILE >> $noscriptname
echo "The noscript $noscriptname has been saved in $SCRIPTDIR
GitHub
GitHub - Vdevelasco/quickCommand: A noscript for easily storing commands in a stack and using them faster. Designed for improving…
A noscript for easily storing commands in a stack and using them faster. Designed for improving speed. - GitHub - Vdevelasco/quickCommand: A noscript for easily storing commands in a stack and using th...
Bash newbie here, wanted to know your opinion
Hi, I've already checked the style with shellcheck and everything seems fine, however I would like to know the opinion of someone who knows a little bit more about bash than me, it's my first noscript afterall and it's probably a mess. Ty in advance. Here's the github page with a readme if something isn't clear [https://github.com/Vdevelasco/quickCommand](https://github.com/Vdevelasco/quickCommand) ty in advance
#!/bin/bash
usage() {
echo "Usage: $0 [-ihrlcmsexd] [-a command] [-f file] [-b size]"
echo "Run qc -i first to set up files needed"
echo " -a: adds a new command to the bottom of the stack"
echo " -i: (init) has to be run one time before using any other commands"
echo " -h: (help) outputs this message"
echo " -r: removes a specific command from the list"
echo " -l: lists all stored commands"
echo " -c: creates a simple noscript with all the stored commands"
echo " -m: modifies the command in a specific slot"
echo " -s: switches two commands"
echo " -b: changes the size of the stack"
echo " -e: loads the commands in a file that can be used as a noscript"
echo " -x: lists avaliable command lists"
echo " -d: lists the directories used by qc"
}
#TODO:Make a flag for changing the arguments of the commands
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace #To debug
QC_DIR=~/.local/share/qc/
COMMAND_FILE="${QC_DIR}qc_commands"
VAR_ASSIGNED_FLAG=false
STACK_FILE="${QC_DIR}qc_max_stacksize"
SCRIPT_DIR="${QC_DIR}noscripts/"
if [ -f $STACK_FILE ]; then
if [ -z ${max_stacksize+x} ]; then
max_stacksize=$(cat $STACK_FILE)
else
echo "var is set to '$max_stacksize'"
fi
fi
isAssigned() {
USED_SLOTS=$(sed 's/|/ /' $COMMAND_FILE | awk '{print $1, $8}')
if [[ "$USED_SLOTS" == *"$1"* ]]; then
echo "That slot is already used by another command"
VAR_ASSIGNED_FLAG=true
else
VAR_ASSIGNED_FLAG=false
fi
}
addCommand() {
number_of_lines=$(wc -l $COMMAND_FILE | cut -c 1)
isAssigned "$1";
if [ ! $VAR_ASSIGNED_FLAG == true ] ; then
if [ "$max_stacksize" -lt $((number_of_lines + 1)) ]; then
echo "Deleting first command from stack"
tail -n $((max_stacksize - 1)) $COMMAND_FILE | sponge $COMMAND_FILE
awk '{ print substr($0, index($0,$2)) }' $COMMAND_FILE | sponge $COMMAND_FILE
awk '{print NR-1 " " $s}' $COMMAND_FILE | sponge $COMMAND_FILE
echo "$((number_of_lines - 1)) $(echo "$1")" >> $COMMAND_FILE
cat $COMMAND_FILE
else
echo "$number_of_lines $1" >> $COMMAND_FILE
cat $COMMAND_FILE
fi
fi
}
touch $COMMAND_FILE
while getopts "ia:f:hrlcmsb:exdu" options; do
case "${options}" in
i)#PERF:
if [ ! -d $QC_DIR ]; then
mkdir $QC_DIR
fi
echo "qc directory has been created succesfully"
touch $COMMAND_FILE
sudo chmod 755 $COMMAND_FILE
echo "qc_commands has been created succesfully"
touch $STACK_FILE
sudo chmod 755 $STACK_FILE
echo 5 > $STACK_FILE
echo "qc_max_stacksize has been created succesfully"
if [ ! -d $SCRIPT_DIR ]; then
mkdir $SCRIPT_DIR
fi
echo "noscript directory has been created succesfully"
exit
;;
a)#PERF:
command="${@:2}"
addCommand "$command"
exit
;;
#Make an noscript based on commands
c)#PERF:
noscriptname="${QC_DIR}noscripts/${2?Error: no filename given, please see usage or provide a default file from configuration}"
rm $noscriptname &
touch $noscriptname
echo "#!/bin/bash" >> $noscriptname
awk '{$1=""}1' $COMMAND_FILE >> $noscriptname
echo "The noscript $noscriptname has been saved in $SCRIPT_DIR
Hi, I've already checked the style with shellcheck and everything seems fine, however I would like to know the opinion of someone who knows a little bit more about bash than me, it's my first noscript afterall and it's probably a mess. Ty in advance. Here's the github page with a readme if something isn't clear [https://github.com/Vdevelasco/quickCommand](https://github.com/Vdevelasco/quickCommand) ty in advance
#!/bin/bash
usage() {
echo "Usage: $0 [-ihrlcmsexd] [-a command] [-f file] [-b size]"
echo "Run qc -i first to set up files needed"
echo " -a: adds a new command to the bottom of the stack"
echo " -i: (init) has to be run one time before using any other commands"
echo " -h: (help) outputs this message"
echo " -r: removes a specific command from the list"
echo " -l: lists all stored commands"
echo " -c: creates a simple noscript with all the stored commands"
echo " -m: modifies the command in a specific slot"
echo " -s: switches two commands"
echo " -b: changes the size of the stack"
echo " -e: loads the commands in a file that can be used as a noscript"
echo " -x: lists avaliable command lists"
echo " -d: lists the directories used by qc"
}
#TODO:Make a flag for changing the arguments of the commands
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace #To debug
QC_DIR=~/.local/share/qc/
COMMAND_FILE="${QC_DIR}qc_commands"
VAR_ASSIGNED_FLAG=false
STACK_FILE="${QC_DIR}qc_max_stacksize"
SCRIPT_DIR="${QC_DIR}noscripts/"
if [ -f $STACK_FILE ]; then
if [ -z ${max_stacksize+x} ]; then
max_stacksize=$(cat $STACK_FILE)
else
echo "var is set to '$max_stacksize'"
fi
fi
isAssigned() {
USED_SLOTS=$(sed 's/|/ /' $COMMAND_FILE | awk '{print $1, $8}')
if [[ "$USED_SLOTS" == *"$1"* ]]; then
echo "That slot is already used by another command"
VAR_ASSIGNED_FLAG=true
else
VAR_ASSIGNED_FLAG=false
fi
}
addCommand() {
number_of_lines=$(wc -l $COMMAND_FILE | cut -c 1)
isAssigned "$1";
if [ ! $VAR_ASSIGNED_FLAG == true ] ; then
if [ "$max_stacksize" -lt $((number_of_lines + 1)) ]; then
echo "Deleting first command from stack"
tail -n $((max_stacksize - 1)) $COMMAND_FILE | sponge $COMMAND_FILE
awk '{ print substr($0, index($0,$2)) }' $COMMAND_FILE | sponge $COMMAND_FILE
awk '{print NR-1 " " $s}' $COMMAND_FILE | sponge $COMMAND_FILE
echo "$((number_of_lines - 1)) $(echo "$1")" >> $COMMAND_FILE
cat $COMMAND_FILE
else
echo "$number_of_lines $1" >> $COMMAND_FILE
cat $COMMAND_FILE
fi
fi
}
touch $COMMAND_FILE
while getopts "ia:f:hrlcmsb:exdu" options; do
case "${options}" in
i)#PERF:
if [ ! -d $QC_DIR ]; then
mkdir $QC_DIR
fi
echo "qc directory has been created succesfully"
touch $COMMAND_FILE
sudo chmod 755 $COMMAND_FILE
echo "qc_commands has been created succesfully"
touch $STACK_FILE
sudo chmod 755 $STACK_FILE
echo 5 > $STACK_FILE
echo "qc_max_stacksize has been created succesfully"
if [ ! -d $SCRIPT_DIR ]; then
mkdir $SCRIPT_DIR
fi
echo "noscript directory has been created succesfully"
exit
;;
a)#PERF:
command="${@:2}"
addCommand "$command"
exit
;;
#Make an noscript based on commands
c)#PERF:
noscriptname="${QC_DIR}noscripts/${2?Error: no filename given, please see usage or provide a default file from configuration}"
rm $noscriptname &
touch $noscriptname
echo "#!/bin/bash" >> $noscriptname
awk '{$1=""}1' $COMMAND_FILE >> $noscriptname
echo "The noscript $noscriptname has been saved in $SCRIPT_DIR
GitHub
GitHub - Vdevelasco/quickCommand: A noscript for easily storing commands in a stack and using them faster. Designed for improving…
A noscript for easily storing commands in a stack and using them faster. Designed for improving speed. - GitHub - Vdevelasco/quickCommand: A noscript for easily storing commands in a stack and using th...
"
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
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
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
Reddit
r/bash on Reddit: Bash newbie here, wanted to know your opinion
Posted by u/Velascu - No votes and no comments
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:
Thanks in advance :)
https://redd.it/13lry9m
@r_bash
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
Reddit
r/bash on Reddit: Help with understanding awk code
Posted by u/PhreshwithaPH - No votes and 1 comment
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
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
Reddit
r/bash on Reddit: Need help with inner loop from a while read with multiple values
Posted by u/kali949 - No votes and 2 comments
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
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
Reddit
r/bash on Reddit: no idea why this noscript isn't working.
Posted by u/Aeul289 - No votes and 1 comment
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
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
Reddit
r/bash on Reddit: Setting up gitbash for a coding bootcamp on Monday everything is screwed up
Posted by u/425Marine - No votes and 3 comments
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
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
Reddit
r/bash on Reddit: why this noscript isn't working:(
Posted by u/Long-Consideration49 - No votes and 1 comment
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
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
Reddit
r/bash on Reddit: Stop git from running
Posted by u/LeMiePalle33 - No votes and no comments
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
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
Reddit
r/bash on Reddit: Script to find matching strings in 2 large CSV files and print matching rows to 3rd file.
Posted by u/DharmaTantra - No votes and no comments