10 Essential Terminal Commands Every Developer Should Know
https://trevorlasn.com/blog/10-essential-terminal-commands-every-developer-should-know/
https://redd.it/1exvpjr
@r_bash
For loop, grep, and curly braces
Me again :)
Today, I had to pull this one out of the hat. I found it tough, as I wanted to use the curly braces as a range. Like I've done in the past for simpler queries (E.g - {0..9}
and at the beginning of this command!). But a colleague of mine pointed out, when using grep, you cannot do this, and instead they are interpreted as repetition operators. I confirmed this, myself, when reading the man page. However, only after stepping away did things click.
I must point out as well, we were after logs from a specific time/ date (15:40-15:59
)
I hope this is somewhat useful to those who are, like me, trying to get a firm grasp of bash.
Here is what was ran:-
for i in servers-{1..3}.domain.io; do ssh user@"$i" "zgrep -E '16/Aug/2024:15:4-50-9:0-9{2}' /var/log/nginx/access.log-20240817.gz >> /tmp/outfile"; done
https://redd.it/1exkfum
@r_bash
Linux Bible and error
I have been going through the Linux Bible by Christopher Negus. In it he discusses using aliases. He gives an example to use
alias p='pwd ; ls -CF'
whenever i run that I get
ls -CF:not found
I then enter ls --help and can see both C and F for arguments. I can type ls -CF from terminal and it will show the files formatted and in columns. However, when using it with the alias command it is not working.
Is there an error in the book? I have also ensured that /bin is in $PATH
I also tried to run it as root and I still received the same error.
UPDATE: well i figured out what was going on. I was using putty and was ssh into my machine. I went directly to the machine and entered the command and it was fine. so weird thanks all.
https://redd.it/1ex3j5w
@r_bash
bash completion for pet
I use pet command-line snippet manager (GitHub link)
there are few commands:
Usage:
pet command
Available Commands:
clip Copy the selected commands
configure Edit config file
edit Edit snippet file
exec Run the selected commands
help Help about any command
list Show all snippets
new Create a new snippet
search Search snippets
sync Sync snippets
version Print the version number
Flags:
--config string config file (default is $HOME/.config/pet/config.toml)
--debug debug mode
-h, --help help for pet
Use "pet command --help" for more information about a command.
I write this very simple bash completion:
petcompletions() {
local cur commands
cur="${COMPWORDS[COMPCWORD]}"
commands="clip configure edit exec help list new search sync version"
if [ ${COMP_CWORD} -eq 1 ]; then
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
fi
}
complete -F petcompletions pet
it works,
but I would like to know if it is well written or if it can be improved
very thanks
https://redd.it/1ewqnsb
@r_bash
Had a suit made they asked if I wanted anything embroidered...
https://i.imgur.com/pIx5eXT.jpg
https://redd.it/1ewit33
@r_bash
Expanding filenames containing spaces with readlink in a bash script
Several programs don't remember the last document(s) they worked with given by command line, e.g. eog ("Eye of GNOME Image Viewer"). So i wrote a general script:
- when given command line args: expand them with readlink, call eog, and store expanded names in <last-args-file>.
- when given no command line args at current invocation: load the files specified on command line at last time of invocation, stored in <last-args-file>
This mechanism works quite fine, so far i don't need that it does not allow specifying other parameters to the "wrapped" programs.
The question: see commented code ("DOES NOT WORK") in lastargs.sh. My intent is to clean up files that do not exist anymore since the last invocation. But $(expandargs "$ARGS") returns empty paths when paths contains spaces.
Any idea/hint? Thank you.
btw. eval was used to allow invocations like PRG="QTSCALEFACTOR=1.8 /opt/libreoffice/program/oosplash"
eog:
#!/bin/bash
FILENAME="eog-lastargs.txt"
PRG=/usr/bin/eog
source ~/bin/lastargs.sh
lastargs.sh:
# Specify the folder to check
FOLDER="$HOME/.config/last-args"
if [[ "$1" == "c" || "$1" == "clear" ]]; then
rm -f "$FOLDER/$FILENAME"
exit 0
fi
expandargs() {
expandedargs=""
for arg in "$@"; do
# Resolve the full path using readlink and add it to the
# expandedargs string
fullpath=$(readlink -e "$arg")
if [[ $? == 0 ]]; then
expandedargs+="\"$fullpath\" "
fi
done
# Trim the trailing space and return the full string
echo "${expandedargs% }"
}
# Check if there are no command line arguments
if $# -eq 0 ; then
# Specify the file to store the last command line arguments
FILE="$FOLDER/$FILENAME"
# Check if the specified folder exists
if ! -d "$FOLDER" ; then
# If not, create the folder
mkdir -p "$FOLDER"
fi
# Check if the file with the last command line arguments exists
if -f "$FILE" ; then
# Read the last command line arguments from the file
ARGS=$(cat "$FILE")
# DOES NOT WORK
# - returns empty paths when path contains spaces
#ARGS=$(expandargs "$ARGS")
#echo "$ARGS" > "$FOLDER/$FILENAME"
# Start with the content of the file as command line arguments
eval "$PRG $ARGS" &
else
# Start without command line arguments
eval "$PRG" &
fi
else
ARGS=$(expandargs "$@")
# Write the current command line arguments to the file in the
# specified folder
echo $ARGS > "$FOLDER/$FILENAME"
# Start with the provided command line arguments
eval "$PRG $ARGS" &
fi
https://redd.it/1evv3q2
@r_bash
mirror one GNU Screen session to another?
I'd like to create two screen sessions, then mirror the activity of one to another. So, if I create session_1
in one Terminal window, and create session_2
in another Terminal window, they'd look the exact same if I ran a certain program in session_1
. It'd also be nice if detaching session_1
detached session_2
as well.
Is this possible using functionality built into screen, or would if be more complicated? I can't find anything about this online, so I'm guessing it's the latter.
https://redd.it/1evp3t5
@r_bash
I have written some helper scripts to simplify on-demand GNU/Linux proxy configuration
https://gitlab.com/brlin/linux-proxy-configuration-helper
https://redd.it/1ev7j98
@r_bash
GitHub - AnonAmosAdmn/OTTO-STACK-DEPLOYER
https://github.com/AnonAmosAdmn/OTTO-STACK-DEPLOYER
https://redd.it/1euwj08
@r_bash
Tab-completion for a command name
I have two custom commands: play-music
and play-video
. I want to write a bash script that allows me to autocomplete these commands when I press TAB.
For example:
$ play<TAB>
play-music play-video
$ play-vi<TAB>
play-video
I’ve found a tutorial on creating a bash-completion script, but it only works for command arguments. How can I achieve this for the command names themselves?
https://redd.it/1eugy51
@r_bash
Any tricks to not have to escape a quote?
I have a pretty lengthy cURL that looks something like this:
--data '{
"description": "Foo",
"expression": "this is csdude\'s, it has \"this\", \"that\", and \"the other thing\""
}'
The real one is much longer; the longest is about 3800 characters, and there are 5 of them called in the bash script.
For the sake of easier coding and minimizing typos, is there a way to NOT have to escape all of the inner quotes?
I tried surrounding it with `, but that just threw an error :-/
https://redd.it/1eu9je9
@r_bash
how do i alias cowsay?
hello, i would like to take the command "cowsay" and alias so every time i type in "endvideo" into the terminal the cowsay command pops up and spits out
"like comment share and subscribe!"
how would i do this?
thank you
https://redd.it/1etti1j
@r_bash
Configuration in bash?
I have this piece of code in Python:
URLVARIANTS = {
'barstool': {
'baseurl': 'mcepl@shell.eng.rdu.redhat.com:publichtml/',
'targeturl': 'http://file.rdu.redhat.com/~mcepl/',
'shortenapi': 'https://url.corp.redhat.com/new?'
},
'wotan': {
'baseurl': 'wotan:Export/',
'targeturl': 'https://w3.suse.de/~mcepl/'
},
'tmp': {
'baseurl': 'fedorapeople.org:publichtml/tmp/',
'targeturl': 'https://mcepl.fedorapeople.org/tmp/',
# 'shortenapi': 'http://is.gd/create.php?format=simple&url='
'shortenapi': 'https://da.gd/s?url='
}
}
curconf = URLVARIANTSsite
# now I am using curconf['baseurl'] etc.
How to rewrite it in bash? Yes, I know about https://linuxsimply.com/bash-scripting-tutorial/array/array-of-arrays/ but the whole is so incredibly finicky and crazy, that I haven’t managed to pull it off (could somebody rewrite this example in the pure sh/bash?). The only alternative I could come up with is to make URL_VARIANTS
a string contain JSON object, and then to use jq
to separate requested values, but that sounds as a potential PITA as well.
Any thoughts?
https://redd.it/1esg0b3
@r_bash
What does - and -- mean in bash?
I've come across scripts that use - or -- for arguments and it's never really explained what they do. What's it called and what's the usage?
# example using -
curl -fsSL 'some_url/install.sh' | env ENV="${HOME}/.bashrc" bash -
# example using --
command -- arg1 arg2
How to learn shell scripting?
I have basic / intermediate knowledge of command line and would like to learn how to use shell scripting. Are there websites or resources that provide problems that I can try and solve? I am also interested in shell scripts for HPC so I was wondering what area I should focus on here.
https://redd.it/1erft7a
@r_bash
what is a "string"
hello, i keep hearing people talking about "strings"?
what is a string? what are people talking about?
thank you
https://redd.it/1exmlum
@r_bash
pong.bash
Was bored so I wrote this bad pong game in bash... https://0x0.st/XJg2.bash
https://redd.it/1ex4bvf
@r_bash
How to uninstall a package installed via curl?
I originally posted this in r/AskProgramming and a fellow redditor suggested I also post my questions here, just in case someone has some additional input on this. Thank you in advance for your help, and pardon the newbie questions.
Hi, everyone 😃 Any input and help is greatly appreciated.
# The Background
I recently installed the package zplug
from its repo. I don't have a use for it anymore, however. So, I would like to uninstall it.
I installed the package (regrefully so) via curl
, rather than using my handy-dandy brew
package manager.
I did this because the project's recommendation was to install via curl:
>The best way (source)
I've uninstalled curl-installed programs before thanks to the devs providing an easy way to do so, via a simple command (like Starship).
# The Problem
I don't know how to reverse engineer the installer script for zplug
to correctly uninstall the package, and any other files it may have created in my system.
# Questions
1. Is there a tool I can install to programmatically fetch any binaries and related files installed via curl
, and then uninstall them?
2. If not, could you please explain how to go about manually uninstalling curled binaries and their files?
# My Setup
Operating System: macOS Sonoma 14.6.1
ZSH version: zsh 5.9 (x86_64-apple-darwin23.0)
`which zplug`: `zplug not found`
Files are indeed within my home directory, though.
https://redd.it/1ex1zlf
@r_bash
what is a "flag" in bash? and how is it different then other options? what would be a good example to highlight the difference?
so i keep hearing that a flag is a TYPE of option, and that the only difference between a flag and normal options is that a flag is a type of "boolean" option, which when explained to me seems no different then binary
so what is a flag? how is it different then other options?
what would be a good example to show someone in the terminal the difference between flags and other types of options?
thank you
https://redd.it/1ewi7pc
@r_bash
Could you help me debug this ChatGPT-created script that deleted my pdfs?
I asked ChatGPT to create a script that changes names of magazines to be consistent with each other (All in the same format), and it deleted all of my files. (Thankfully, I had a backup). Is ChatGPT's fix good? (I asked it to make it not change their directory anymore, not realizing it just outright deleted them). Here is the script:
Sorry, I can't seem to format it
# #!/bin/bash
# Function to rename PDF files
rename_pdfs() {
local dir="$1"
local parent_dir="$(basename "$(dirname "$dir")")"
local grandparent_dir="$(basename "$(dirname "$(dirname "$dir")")")"
# List PDF files in alphabetical order
pdf_files=("$dir"/*.pdf)
IFS=$'\\n' sorted_files=($(sort <<<"${pdf_files[*\]}"))
unset IFS
# Rename files with the desired format, ensuring they stay in the same directory
count=1
for file in "${sorted_files[@\]}"; do
if [[ -f "$file" \]\]; then
base_number=$(printf "%02d" "$count")
new_name="${grandparent_dir}_${parent_dir}_${base_number}.pdf"
mv "$file" "$dir/$new_name" # This keeps the file in the same directory
((count++))
fi
done
}
# Export function so it's available in subshells
export -f rename_pdfs
# Find all directories 2 levels deep and rename PDFs
find . -type d -mindepth 2 -maxdepth 2 -exec bash -c 'rename_pdfs "$0"' {} \\;
https://redd.it/1ewcs17
@r_bash
Trap not taking effect in POSIX script
In this script I launch vim opening a temp file in the terminal window. If the terminal window is closed with vim running, the temp file should be deleted. Closing the terminal window should also kill vim process.
However, closing the terminal window doesn't remove the file and the vim process lingers when the terminal window is closed. If I remove the trap
command, then the vim process will terminate as expected but the temp file will of course remain.
Any ideas? I have exec sh -c
because for some reason without it, vim process lingers when its terminal window closes.
https://redd.it/1evoe9l
@r_bash
Interpolation and sed!
I hope this helps somebody, like it did for myself, last week.
I love this shit. And I am always happy to share/ read contructive critiscism.
I got tasked with a set of instructions, under immense pressure, the other day.
I needed to edit a 20k line file, and was told it needed batching into many, 100 line files, to avoid deadlocking our DB whilst running the finished article. In total, we were deleting millions of duplicate rows, with specific IDs. I also needed to add a comma, at the end of each newline (so far, no sweat). So I did for i in x*; do cat "$i" | tr \\n , >> $i.new;
I then batched that file, into many smaller ones, running split -l 100 FILE.txt
. The newly created batched files then had naming conventions like, `xaa.new`, `xab.new` etc.
But, after I had done this, I discovered that I also needed to remove the very last comma in each file. This is so that the syntax is accepted by MySQL. So I did - for i in x*; do sed -e '$s/,$//' "$i" > "$i".new
.
Then, here's the interpolation... courtesy of my wise and all-knowing colleague, who just happened to shoot me a message, making sure I was ok. I was stuck on how to run the MySQL statement, using the content in all my files. I was also very nervous, at this point. We ended up writing, for i in x*; do mysql databaseName -vvv -e "DELETE from table where table_id in ($(cat $i))" >> /home/userName/incidentNumber/output.sql
after we had tested SELECT statements, echo'ing output (without any MySQL) and had it approved by all stakeholders.
I felt very accomplished, as I always do when I learn something new, and needed a little nudge to get over the line, in the end. But my goodness, it was such a rush! I hope someone finds this useful and/ or interesting. I know I did.
https://redd.it/1evju1s
@r_bash
Bashtutor - interactive bash tutorial
I wrote a minimal framework for creating CLI obstacle courses.
Currently there is one "module" which is for Bash itself.
While its a proof of concept, I attempted to make it entertaining and smoothen the edges as much as I could.
The main inspiration was vimtutor and how I would have liked something like this back when I was starting out.
https://github.com/agvxov/bashtutor
I'm hoping it will be useful to someone somewhere.
https://redd.it/1ev6ex5
@r_bash
what is an "option" in bash? and how is it different the other arguments?
so i understand what an argument is, i understand that an option is a type of argument,
but what i don't understand is how an option is different then other types of arguments
can someone explain it to me?
thank you
https://redd.it/1eujcim
@r_bash
More fun with jq, getting results into a usable array
I'm using this in a cURL to get the data from `result[]`:
foo=$(curl --request GET \
--silent \
--url https://example.com \
--header 'Content-Type: application/json' | jq -r '.result[]')
When I print $foo, this is what I have:
[key]
default
firewall_custom
zone
34
[
{
"id": "example",
"version": "6",
"action": "block",
"expression": "lorem",
"description": "ipsum",
"last_updated": "2024-08-15T19:10:24.913784Z",
"ref": "example",
"enabled": true
},
{
"id": "example2",
"version": "7",
"action": "block",
"expression": "this",
"description": "that",
"last_updated": "2024-08-15T19:10:24.913784Z",
"ref": "example2",
"enabled": true
}
]
What I need from this is to create a loop where, in a series of addtional cURLs, I can insert action, expression, and description.
I'm imagining that I would push these to 3 separate arrays (action, expression, and description), so that ${action\[0\]} would coincide with ${expression\[0\]} and ${description\[0\]}, and so on.
Something along the lines of:
# assuming that I have somehow created the following arrays:
# action=("block" "block")
# expression=("lorem" "this")
# description=("ipsum" "that")
for x in ${action[@]}; do
bar=$(curl --request GET \
--silent \
--url https://example.com \
--data '{
"action": ${action[$x]},
"expression": ${expression[$x]},
"description": ${description[$x]}
}' | jq '.success')
if [[ $bar == true ]]
then
printf "$x succeeded\n"
else
printf "$x failed\n"
fi
# reset bar
bar=''
done
The question is, how to create action, expression, and description arrays from the results of $foo (that original cURL)?
https://redd.it/1euba4a
@r_bash
Limit developers from running a command in command line in a project
We have a fresh Cloudflare worker Typescript project in which we currently use wrangler deploy --production command to deploy to production worker.
We want to disable using that command locally and enable it only on the CI/CD pipeline (Github Actions). The problem is that Cloudflare doesn't offer any kind of permissions to do that, except fully limitting developers from accessing Cloudflare by deleting their accounts, and obviously we don't want to do that.
Is there a way of using a bash script to accomplish this? And have that script fully executable for any developer who would have it locally in the project (git commited to the repository)?
I am fairly new to bash, so I'm not even sure I asked the right question, but I'd say you get the jist.
Also we are open to any other ideas to accomplish this.
Thanks
https://redd.it/1etywgb
@r_bash
[error] guys i think i broke bash in a way never before seen
https://redd.it/1etq4eu
@r_bash
Using read -p to prompt bold variable with ANSI escape codes?
Hi,\
As the title, I was wondering if it is possible to do it.\
I've tried 1
var=candy
bold=$(tput bold)
normal=$(tput sgr0)
read -p "IS ${bold}$var${normal} correct? " ans
# assuming answer yes
printf "Your answer is \033[1m%s\033[0m." "$ans"
var=candy
read -rep $'Is \033[1m$var\033[0m correct?' ans
printf "Your answer is \033[1m%s\033[0m." "$ans"
printf
and read
separately. Something likeprintf "Is \033[1m%s\033[0m correct?" "$var"
read ans
printf "Your answer is \033[1m%s\033[0m." "$ans"
read -p
is not supported in every shell, maybe it's a good habit to not use -p
.~~argc - Top-tier utility/framework for creating shell scripts
https://github.com/sigoden/argc
I’m not the author. Whoever it is, they are a bloody legend!
Figured I would share it as it deserves way more love.
https://redd.it/1eqvgj4
@r_bash