shell-script

How to make mv work with exotic directory names?

How to make mv work with exotic directory names? I created several scripts to move messages tagged via notmuch notmuch between Maildir directories. Having multiple accounts, I have some issues with the names of some directories mbsync created when syncing with gmail. Here is my typical Maildir structure for gmail accounts: gmail-user2 ├── [Gmail] │   …

Total answers: 1

What is the equivalent of `localectl set-keymap` command on Debian?

What is the equivalent of `localectl set-keymap` command on Debian? I am porting a shell script written for Arch to Debian. The relevant part: keymaps=$(localectl list-keymaps) if test -n "${1}" && localectl list-keymaps | grep -q "${1}" then keymap="${1}" else exec 3>&1 keymap=$(/sbin/dialog –title "Keyboard layout" –menu "Choose a keyboard layout" 25 50 20 $(for …

Total answers: 1

Sorting Bash History for Redundancy Removal

Sorting Bash History for Redundancy Removal I’m working with my .bash_history file and want to identify repetitive commands for cleanup. Here’s a sample snippet: … #1713251479 sh lowbat.sh #1713251495 nvim lowbat.sh #1713252186 sh lowbat.sh #1713253121 xclip -sel clip lowbat.sh #1713253722 cd code/dotfiles/ #1713253766 git add mpv/ … My goal is to: Sort the commands alphabetically …

Total answers: 1

Open file manager to prompt user for file

Open file manager to prompt user for file I am writing a shell script that is meant to prompt the user for a file, and since I do not expect the end user of my script to be tech-savvy, rather than starting command line for interaction, I would like the shell to be able to …

Total answers: 1

Tar new volume script fails with "Syntax error: Bad fd number"

Tar new volume script fails with "Syntax error: Bad fd number" My shell script (see below) is failing partway through execution with "/bin/sh: 1: Syntax error: Bad fd number." This thread suggests that the problem is using the >& construction in sh, which in Ubuntu is linked to dash, which does not support >&. I …

Total answers: 1

How do I programmatically check if a Flatpak package is installed in a shell script?

How do I programmatically check if a Flatpak package is installed in a shell script? I’m writing a script where I want to detect whether a given Flatpak package is installed, and take a different code path depending on the answer. How can I accomplish this? Traditional measures like command -v obviously won’t work unless …

Total answers: 1

Replacing variable correctly in alias function

Replacing variable correctly in alias function I’m trying to create an alias function to remove inputted line from file: function remove_line(){ line_to_remove="’s/^"$1"$//g’" sed -i $(line_to_remove) my_file } For example: remove_line domain.com should remove this domain from a given file. However it seems that the $ is not interpreted correctly. What am I doing wrong? Asked …

Total answers: 1

Output of loop variable shows different value than expected

Output of loop variable shows different value than expected Imagine I have two folders in my current working directory: back/ and front/. I am trying to enter inside each of them and do some stuff. From the simple script below: for dir in */ ; do echo "$(dir)" done I was expecting an output like: …

Total answers: 1

How do I pass hex characters to printf in a script command?

How do I pass hex characters to printf in a script command? How do I get printf to output hex characters when run from a script? Suppose I am at the prompt and type printf "x41x42" the output I get AB% However if I have a script containing that line i.e. cat test.sh produces printf …

Total answers: 1

How can I use $0 in Makefile?

How can I use $0 in Makefile? I would like to include the following command (taken from here) in a Makefile. A simple version of my Makefile script containing the command is: copy_files: sed ‘s/^/./’ ./input_file | rev | LC_ALL=C sort -u | awk ‘p == "" || substr($0,1,length(p)) != p { print $0; p …

Total answers: 1

How to find files with find tool in system path ($PATH)? Or alternatively, How to specify starting-point directory for find as an expression?

How to find files with find tool in system path ($PATH)? Or alternatively, How to specify starting-point directory for find as an expression? For example, I want to find all symlinks that reference to particular binary in all directories that belong to system $PATH. This can be successfully achieved with manual specification of all directories: …

Total answers: 2

Skip .app folders when using "fdupes" with the option "–recurse"

Skip .app folders when using "fdupes" with the option "–recurse" I am using fdupes to print the list of duplicate files in a certain folder, with the option –recurse. However, since I am using macOS, the recursing process regards Mac apps (which appear to be folders ended with .app) as folders and runs into them, …

Total answers: 1

How to list all file in a path with absolute path and spaces in a script

How to list all file in a path with absolute path and spaces in a script I’m writing a script that accept a path as a parameter and process all the .mkv file in the path. The path could be an absolute path, a relative path, or even a single file. However, I still need …

Total answers: 3

Need help with a script to link files in a directory

Need help with a script to link files in a directory I’m new to bash scripting and wanted to make a script that will get user input and hard link all the files of a directory to another one. So far I have this: read -p "What directory do you want to link? " dir …

Total answers: 1

Why do I need to care about `$IFS` when `read`-ing into a single variable?

Why do I need to care about `$IFS` when `read`-ing into a single variable? A construction similar to IFS= read -r var is often seen as a "canonical" way in Bash to read a line of input into a variable (e.g. on unix.SE). It is clear enough why setting an empty $IFS might be beneficial …

Total answers: 1

Is there a way I can use .bash_aliases in a shell-scripts?

Is there a way I can use .bash_aliases in a shell-scripts? Is there a way I can use bash aliases from my .bash_aliases file within a shell-scripts? I thought of something like including source "$HOME/.bash_aliases into my script but it does not work. Asked By: nath || Source Non-interactive shells don’t expand aliases by default. …

Total answers: 1

Double backslash disappears when printed in a loop

Double backslash disappears when printed in a loop I have a script that joins together various lists of data fields which then needs to have a few more columns added. The file generated looks like this: $ cat compiled.csv "name":"Network Rule 1", "description":"Network Rule 1", "from":["internal"], "source":["any"], "user":["domain\network_user1"], "to":["external"], "destination":["host.example.com","10.1.2.1"], "port":["8443","22"], "name":"Network Rule 2", "description":"Network …

Total answers: 1

Delete directory and create new one with same name and permissions

Delete directory and create new one with same name and permissions I want to delete a directory with its contents recursively, and then re-create it with the same name and permissions. That directory may or may not be created or owned by me earlier before delete, but I am sure I wouldn’t touch any folder …

Total answers: 2