Script for 'cd-ing' into zip archives
Hey everyone!
I wrote this noscript to transparently allow for something like
I would appreciate constructive criticism on the function, as I have very little experience with bash noscripting and how it could be improved/what can go wrong. I recognize the background process is a little kludgy, but I wasn't sure how to do this without it.
I also have the following in my
https://redd.it/1pl6gsb
@r_bash
Hey everyone!
I wrote this noscript to transparently allow for something like
cd archive.zip.I would appreciate constructive criticism on the function, as I have very little experience with bash noscripting and how it could be improved/what can go wrong. I recognize the background process is a little kludgy, but I wasn't sure how to do this without it.
#!/usr/bin/env zsh
# custom cd command allowing for transparent reading and writing of of zip
# archives.
function cd {
# if fuse-zip is not found, then use builtin cd
if ! command -v fuse-zip &>/dev/null; then
builtin cd "$@";
return "$?";
fi
local TARGET_FILE="$1";
if [[ "$#" == 0 || ! -f "$TARGET_FILE" ]]; then
builtin cd "$@";
return $?;
fi
local TARGET_FILE_EXTENSION="${TARGET_FILE:e}";
if [[ ! "$TARGET_FILE" =~ "apk|docx|epub|jar|pptx|pubx|xlsx|zip" ]]; then
builtin cd "$@";
return $?;
fi
if [[ ! -w "$TARGET_FILE" ]]; then
echo "Insufficient permissions to enter possible archive \"$TARGET_FILE\"" 1>&2;
return 1;
fi
echo "Attempting to enter possible archive \"$TARGET_FILE\"..." 1>&2;
local TARGET_FILE_PATH="$(realpath $TARGET_FILE)";
local FILE_NAME="${TARGET_FILE_PATH:t}";
# create a unique name for the stashed file
local TARGET_FILE_PATH_HASH="$(echo -n $TARGET_FILE_PATH | sha1sum | cut -d' ' -f1)";
# ensure stash exists
local STASH_DIR_PATH="$HOME/.zip_fuse_cache";
local STASH_FILE_PATH="$STASH_DIR_PATH/$TARGET_FILE_PATH_HASH-$FILE_NAME";
# prepare for mount
local MOUNT_POINT="$TARGET_FILE_PATH";
mv "$TARGET_FILE_PATH" "$STASH_FILE_PATH";
mkdir -p "$MOUNT_POINT";
# TODO: support other extensions:
# - tar
# - tar.gz
# - 7z
# - rar
case "$TARGET_FILE_EXTENSION" in
"apk" | "docx" | "epub" | "jar" | "pptx" | "pubx" | "xlsx" | "zip") # a large number of filetypes are actually zip archives in disguise
if ! fuse-zip "$STASH_FILE_PATH" "$MOUNT_POINT"; then
echo "\"$FILE_NAME\" not a valid $TARGET_FILE_EXTENSION file" 1>&2;
rmdir "$MOUNT_POINT";
mv "$STASH_FILE_PATH" "$TARGET_FILE_PATH";
return 1;
fi
builtin cd "$TARGET_FILE_PATH";
;;
*)
echo "Unsupported file type \"$TARGET_FILE_EXTENSION\"" 1>&2;
rmdir "$MOUNT_POINT";
mv "$STASH_FILE_PATH" "$TARGET_FILE_PATH";
return 1;
esac
echo "Stashed $TARGET_FILE at $STASH_FILE_PATH" 1>&2;
# background process to monitor for file closing
echo "Monitoring for unmount at process: " 1>&2;
(
cd "$HOME";
while fuser -m "$MOUNT_POINT" &>/dev/null ; do
sleep 3;
done
fusermount -u "$MOUNT_POINT";
# ensure fuse-zip finishes writing
sleep 1;
while ps aux | grep -q "[f]use-zip.*$STASH_FILE_PATH"; do
sleep 0.5;
done
rmdir "$MOUNT_POINT";
mv "$STASH_FILE_PATH" "$TARGET_FILE_PATH";
exit 0;
) &
disown;
return 0;
}
I also have the following in my
zshrc in case the background process is somehow interrupted:#!/usr/bin/env zsh
# checks the .zip_fuse_cache used by .dotfiles/zsh/.config/zsh/functions/cd for
# orphaned files, and alerts the user of any if found.
if [[ -n "$(ls $HOME/.zip_fuse_cache)" ]]; then
echo "Files detected in $HOME/.zip_fuse_cache." 1>&2;
echo "This indicates possible corruption of archives." 1>&2;
echo "Please check the following files: " 1>&2;
echo "$(ls -A $HOME/.zip_fuse_cache/)";
fi
https://redd.it/1pl6gsb
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Recursive file renaming based on parent directory
I have some ripped audiobooks that are currently structured as
/book
/disc 1
/track 1.mp3, track 2.mp3
/disc 2
/track 1.mp3, track 2.mp3
and I need to rename and move the tracks to follow this structure
/book
/disc 01 - track 1.mp3,disc 01 - track 2.mp3, disc 02 - track 1.mp3, disc 02 - track 2.mp3
I know I can use mv to do part of this i.e.
Thank yall
https://redd.it/1pluwfp
@r_bash
I have some ripped audiobooks that are currently structured as
/book
/disc 1
/track 1.mp3, track 2.mp3
/disc 2
/track 1.mp3, track 2.mp3
and I need to rename and move the tracks to follow this structure
/book
/disc 01 - track 1.mp3,disc 01 - track 2.mp3, disc 02 - track 1.mp3, disc 02 - track 2.mp3
I know I can use mv to do part of this i.e.
for f in *.mp3; do mv "$f" "CD 1 - $f"; done but how do I make it name based on the folder it is in and make it recursive?Thank yall
https://redd.it/1pluwfp
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Run multiple bash commands using text-only based on GPT-5.2
https://github.com/matank001/OsDevil
https://redd.it/1plxjlz
@r_bash
https://github.com/matank001/OsDevil
https://redd.it/1plxjlz
@r_bash
GitHub
GitHub - matank001/OsDevil: OsDevil is a lightweight AI agent that converts natural language into operating system commands, enabling…
OsDevil is a lightweight AI agent that converts natural language into operating system commands, enabling automation across development, operations, and DevSecOps workflows - matank001/OsDevil
Automate the initial creation process of your bash noscript
Tired of the initial hassle of creating a Bash noscript—like making the file readable and executable? If so, this tool is for you.
https://github.com/Keys02/noscriptify
PS: noscriptify also adds the shebang line.
All contributions are welcome
https://redd.it/1pm5vaw
@r_bash
Tired of the initial hassle of creating a Bash noscript—like making the file readable and executable? If so, this tool is for you.
https://github.com/Keys02/noscriptify
PS: noscriptify also adds the shebang line.
All contributions are welcome
https://redd.it/1pm5vaw
@r_bash
GitHub
GitHub - Keys02/noscriptify: Scriptify is a utility which gits rid of the friction between creating your bash noscript and actually…
Scriptify is a utility which gits rid of the friction between creating your bash noscript and actually writing it. - Keys02/noscriptify
Concurrent, parallel, or simultaneous?
I frequently write noscripts that should only have 1 instance running at a time. For instance, a noscript that copies a MySQL table from 1 host to another.
I implement this with a snippet like:
Would you consider the second instance of this noscript to be concurrent, parallel, or simultaneous?
https://redd.it/1pmte90
@r_bash
I frequently write noscripts that should only have 1 instance running at a time. For instance, a noscript that copies a MySQL table from 1 host to another.
I implement this with a snippet like:
# prevent simultaneous execution
pids=$(pidof -o '%PPID' -x "$(basename "$0")")
if [[ -n "${pids}" ]]
then
echo "$(basename $0) (${pids}) is already running."
exit
fi
Would you consider the second instance of this noscript to be concurrent, parallel, or simultaneous?
https://redd.it/1pmte90
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Bash noscript for docker monitoring
I wanted to monitor and manage docker containers on a few servers. All the solutions I found were either heave or were missing things which I wanted so I started developing my own bash noscript - it started as a simple noscript but after many imitations and improvements based on usage it has become a real helpful tool.
Just wanted to share here in appreciation of Bash - there is so much which I did not even know can be done with simply bash noscripting.
https://github.com/buildplan/container-monitor
https://redd.it/1pnlqlx
@r_bash
I wanted to monitor and manage docker containers on a few servers. All the solutions I found were either heave or were missing things which I wanted so I started developing my own bash noscript - it started as a simple noscript but after many imitations and improvements based on usage it has become a real helpful tool.
Just wanted to share here in appreciation of Bash - there is so much which I did not even know can be done with simply bash noscripting.
https://github.com/buildplan/container-monitor
https://redd.it/1pnlqlx
@r_bash
GitHub
GitHub - buildplan/container-monitor: Monitoring noscript for Docker containers - checks and updates containers, Shows logs and filter…
Monitoring noscript for Docker containers - checks and updates containers, Shows logs and filter errors etc. - buildplan/container-monitor
change color in css file (working)
i have this simple bash file, to run you do for example "./test.sh accent \\#ffffff", but i want to merge the functions into one. how to do this?
https://preview.redd.it/87w1mp1q6g7g1.png?width=1281&format=png&auto=webp&s=c510c57cc1727e1e94613841bb6dd5281da41d74
https://redd.it/1pnloa1
@r_bash
i have this simple bash file, to run you do for example "./test.sh accent \\#ffffff", but i want to merge the functions into one. how to do this?
https://preview.redd.it/87w1mp1q6g7g1.png?width=1281&format=png&auto=webp&s=c510c57cc1727e1e94613841bb6dd5281da41d74
https://redd.it/1pnloa1
@r_bash
Isn't this the greatest BASH course ever?
https://www.youtube.com/watch?v=Sx9zG7wa4FA : YSAP
The way this guy explains concepts with depth and clarity in it is insane. The fact that he self-learnt everything through man pages is something which keeps me driven in tech.
https://redd.it/1po23s0
@r_bash
https://www.youtube.com/watch?v=Sx9zG7wa4FA : YSAP
The way this guy explains concepts with depth and clarity in it is insane. The fact that he self-learnt everything through man pages is something which keeps me driven in tech.
https://redd.it/1po23s0
@r_bash
YouTube
The Complete Bash Scripting Course - Full Length Guide to learning the Bash Shell
Bash noscripting course and guide created by Dave Eddy of ysap.sh. Learn the Bash Shell and master beginner all the way up to advanced Bash noscripting techniques. Check out the course website below for all materials, source code, referenced material, etc. for…
Asking for help with a command launcher noscript
I'd like to ask a question about an automation strategy which has eluded me.
# What I'm trying to do
I'd like to have a noscript which:
1. can launch a new terminal emulator
2. then run a login shell in the terminal emulator (with all my personal shell initialization)
3. can then run an arbitrary program or command of my choosing
4. then on completion or termination of the program the shell stays alive and interactive
5. also the arbitrary command is added to shell history
Hopefully I explained that well.
Unfortunately something like
With the above the terminal is closed after program completion and is not run with shell initialization (login shell).
I'll share my solution, but I'm curious if there is an easier way to accomplish the same:
# Current Solution
I also put the code in this repo
I add the following to the end my
if [ -n ${INIT_CMD} ]; then
print -s "${INITCMD}"
eval "${INITCMD}"
unset INITCMD
fi
then to launch programs I use something like:
#!/usr/bin/env bash
# terminal='alacritty'
# terminal='ghostty'
# terminal='st'
terminal='kitty'
${terminal} -e $SHELL \
-c 'INITCMD="echo hello" $SHELL'
where
Which does require starting up two shells, however, the first shell with
Thanks in advance if you know a simpler way to accomplish this!
https://redd.it/1po6z4y
@r_bash
I'd like to ask a question about an automation strategy which has eluded me.
# What I'm trying to do
I'd like to have a noscript which:
1. can launch a new terminal emulator
2. then run a login shell in the terminal emulator (with all my personal shell initialization)
3. can then run an arbitrary program or command of my choosing
4. then on completion or termination of the program the shell stays alive and interactive
5. also the arbitrary command is added to shell history
Hopefully I explained that well.
Unfortunately something like
alacritty -e bash -c 'echo hello' does not fulfill these requirements. With the above the terminal is closed after program completion and is not run with shell initialization (login shell).
I'll share my solution, but I'm curious if there is an easier way to accomplish the same:
# Current Solution
I also put the code in this repo
I add the following to the end my
~/.bashrcif [ -n ${INIT_CMD} ]; then
print -s "${INITCMD}"
eval "${INITCMD}"
unset INITCMD
fi
then to launch programs I use something like:
#!/usr/bin/env bash
# terminal='alacritty'
# terminal='ghostty'
# terminal='st'
terminal='kitty'
${terminal} -e $SHELL \
-c 'INITCMD="echo hello" $SHELL'
where
echo hello is the "program"Which does require starting up two shells, however, the first shell with
-c flag is cheap. The second shell is the login shell.Thanks in advance if you know a simpler way to accomplish this!
https://redd.it/1po6z4y
@r_bash
GitHub
GitHub - nanvenomous/command_launcher
Contribute to nanvenomous/command_launcher development by creating an account on GitHub.
How to check if $var is in a list?
Imagine you have:
How can you check if
I don't want to write a loop for that :-)
https://redd.it/1ppmxqf
@r_bash
Imagine you have:
mylist=("foo" "bar" "baz")
How can you check if
$var is in mylist?I don't want to write a loop for that :-)
https://redd.it/1ppmxqf
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Bash Trek: TNG, an Update to a Retro Terminal Game
One or two attentive readers may recall that a couple of months ago, I posted about a Bash version of the old Star Trek terminal game that I'd written, called Bash Trek.
Well - I've adapted it into a new implementation, Bash Trek: TNG which replaces the old typed command interface with mouse control.
Mouse reporting is an underused thing, I think. It's not hard to build simple menus or buttons for simple terminal applications.
https://preview.redd.it/uj9z4g3za18g1.png?width=708&format=png&auto=webp&s=4baa2d60360563fe9c27a2afc6c9f1fb0dd78d5d
https://preview.redd.it/gxmif3e0b18g1.png?width=708&format=png&auto=webp&s=1c685b49377dfcc68684033c7da5397618f2a346
Anyway: if interested, https://github.com/StarShovel/bash-trek-tng
https://redd.it/1pq3ihb
@r_bash
One or two attentive readers may recall that a couple of months ago, I posted about a Bash version of the old Star Trek terminal game that I'd written, called Bash Trek.
Well - I've adapted it into a new implementation, Bash Trek: TNG which replaces the old typed command interface with mouse control.
Mouse reporting is an underused thing, I think. It's not hard to build simple menus or buttons for simple terminal applications.
https://preview.redd.it/uj9z4g3za18g1.png?width=708&format=png&auto=webp&s=4baa2d60360563fe9c27a2afc6c9f1fb0dd78d5d
https://preview.redd.it/gxmif3e0b18g1.png?width=708&format=png&auto=webp&s=1c685b49377dfcc68684033c7da5397618f2a346
Anyway: if interested, https://github.com/StarShovel/bash-trek-tng
https://redd.it/1pq3ihb
@r_bash
Little help needed! sometimes this noscript exits after the first line
the argument in the first line is a youtube video url or channel url. It downloads the id of the video/videos. Sometimes the code exits here, other times it actually goes to the other lines.
the second line is to filter out duplicate lines. Video ids are uniq, but if you run the code again, it just appends the ids to 'ids.txt'
the third line sorts ids.txt in reverse order. I then use the ids to download video urls in the fourth line. Please help me out. I would also appreciate if you help improve the noscript in other areas. I would like to add a padding of 5 to the output filenames, so that 1.jpg becomes 00001.jpg and 200.jpg becomes 00200.jpg
Thank you very much in advance
https://redd.it/1pqloym
@r_bash
#!/bin/bashyt-dlp --skip-download --flat-playlist --print-to-file id 'ids.txt' $1 awk '!seen[$0]++' ids.txt | tee ids.txtawk '{print NR, $0 }' ids.txt | sort -rn | awk '{print $2}' > ids.logawk '{print "wget `http://img.youtube.com/vi/"$1"/mqdefault.jpg` -O "NR".jpg"}' ids.logthe argument in the first line is a youtube video url or channel url. It downloads the id of the video/videos. Sometimes the code exits here, other times it actually goes to the other lines.
the second line is to filter out duplicate lines. Video ids are uniq, but if you run the code again, it just appends the ids to 'ids.txt'
the third line sorts ids.txt in reverse order. I then use the ids to download video urls in the fourth line. Please help me out. I would also appreciate if you help improve the noscript in other areas. I would like to add a padding of 5 to the output filenames, so that 1.jpg becomes 00001.jpg and 200.jpg becomes 00200.jpg
Thank you very much in advance
https://redd.it/1pqloym
@r_bash
Why doesn't closing this program's stdout not cause a pipeline to finish?
I'm seeing something strange.
This command:
dd if=/dev/zero bs=1024k count=256 | gprog --size-estimate $((1024*1024*256)) | sha256sum
...produces a sha256, when gprog closes its stdout.
This command:
gprog-du-tar --directories /usr | sha256sum
...does not produce a sha256 when gprog closes its stdout. Instead, it waits until gprog's GUI is shut down, well after gprog closes its stdout.
gprog itself is a python noscript, and can be found at:
https://stromberg.dnsalias.org/svn/gprog/trunk/gprog
gprog-du-tar is a bash noscript that wraps gprog, and can be found at:
https://stromberg.dnsalias.org/svn/gprog/trunk/gprog-du-tar
I suspect the problem isn't particularly related to gprog, since that same command exhibits such different behavior when run by itself at a bash prompt, vs. run inside a shell noscript: gprog-du-tar.
So you don't need to visit those links above (in case you'd rather not), here's gprog-du-tar's content:
Any suggestions? I'm a bit baffled by why the same program would give such a different result based on how its run.
https://redd.it/1pqz5nj
@r_bash
I'm seeing something strange.
This command:
dd if=/dev/zero bs=1024k count=256 | gprog --size-estimate $((1024*1024*256)) | sha256sum
...produces a sha256, when gprog closes its stdout.
This command:
gprog-du-tar --directories /usr | sha256sum
...does not produce a sha256 when gprog closes its stdout. Instead, it waits until gprog's GUI is shut down, well after gprog closes its stdout.
gprog itself is a python noscript, and can be found at:
https://stromberg.dnsalias.org/svn/gprog/trunk/gprog
gprog-du-tar is a bash noscript that wraps gprog, and can be found at:
https://stromberg.dnsalias.org/svn/gprog/trunk/gprog-du-tar
I suspect the problem isn't particularly related to gprog, since that same command exhibits such different behavior when run by itself at a bash prompt, vs. run inside a shell noscript: gprog-du-tar.
So you don't need to visit those links above (in case you'd rather not), here's gprog-du-tar's content:
#!/bin/bash# this is far from a perfect estimate, but it's usually pretty decentfunction usage { retval="$1" case "$retval" in 0) ;; *) exec 1>&2 ;; esac echo "Usage: $0"echo "--directories A list of directories to du and tar - must be the last option" echo "--help This stuff" echo echo "Tar up a local directory hierarchy and pipe it through gprog, using du to get an estimate of how much data will need" echo "to be copied." exit "$retval" }while [ "$#" -ge 1 ] do if [ "$1" = --directories ] then shift break elif [ "$1" = --help ] then usage 0 else echo "$0: Illegal option: $1" 1>&2 usage 1 fishift doneif type -path gtar > /dev/null 2>&1 then tar=gtar else tar=tar fiestimate=$(for i in "$@" do du -skx "$i" done | \ count -c | \ python3 -c ' import sys total = 0 for line in sys.stdin: total += int(line.split()[0]) * 1024 print(total)')echo "Estimate: $estimate bytes" 1>&2"$tar" --create --sparse --one-file-system "$@" | gprog --size-estimate "$estimate" --noscript "gprog-du-tar $*"Any suggestions? I'm a bit baffled by why the same program would give such a different result based on how its run.
https://redd.it/1pqz5nj
@r_bash