1
1
submitted 1 year ago* (last edited 1 year ago) by Anonymous_TorPerson@lemmy.ml to c/bash@lemmy.ml

[SOLVED] Solution: https://lemmy.ml/comment/4317564


I am doing all of this using ChatGPT, I know enough bash to understand the script partially, but not enough to write the script myself.

I recently posted bash script(click) to download songs on spotify using Spotdl Updated Post

This is all good, but I am not trying to download whole playlists and I want to make sure to

  1. Not download any songs multiple times by comparing the files you are trying to download with the songs you have downloaded already.
  2. Add the songs' URL to the Archieve_file so it doesn't download it again. i.e., compare and if present, exit loop, if not present in file, download and add link to the file.

This was easier when I was dealing with only song links and not playlist links. But now, playlists complicates the equation, but if I can achieve this, I can basically add this script to crontab and make sure I always have a local copy of the songs in my playlist and these playlists would be checked regularly for new downloads and new songs would be downloaded. This is really cool!

Now, the complications I am facing, I don't know how to get the URLs of the spotify songs out of spotify playlists using spotdl or any cli package. If I can do this then, I can make the script go through each link clean the link of unncessary attributes and then download it or if it's downloaded already, move on to the next song.

Now, I don't know how to do it. It would be very helpful if you guys could share any scripts that you have which will achieve this effect or help me get the song URL's from a playlist using a cli package.

PS: I might need some time to reply, I might sleep rn Thank you for your help! ___

2
1
submitted 1 year ago* (last edited 1 year ago) by Anonymous_TorPerson@lemmy.ml to c/bash@lemmy.ml

I would rather prefer that you would buy spotify premium if you can. But, till you can, there's always spotdl which can allow you to download your songs

Installation of spotdl (github page click here): https://i.imgur.com/5g6uUgD.png

https://paste.debian.net/plain/1293528 a very simple script, you don't actually need the script, but it makes it easier to download songs.

btw, I am using file manager nemo here, if you want something else, change it to your default file manager, if you don't want to open folder, well remove last but one line.

execute this command first and when nano text edit appears, paste the debian pastebin in there.

spoiler


3
1
submitted 1 year ago* (last edited 1 year ago) by Anonymous_TorPerson@lemmy.ml to c/bash@lemmy.ml

This will remove all sponsors, download subtitles and view it when you are watching a video. Modify and Share this all over if you like! Edit: Give credits to this community or Lemmy in general if you are posting this or a modified form (please do share) of this elsewhere. Some popularity would do Lemmy good.

Few requirements:

  1. You should be using Linux to run it.
  2. Create a folder named yt-dlp in Videos folder or else change the location in the script below.
  3. You should have yt-dlp not youtube-dl on your system. You can do this by sudo apt install yt-dlp or use your distro's package installer.
  4. Save this file with any name in your home folder (or whichever folder you are you most comfortable with and give it execution permissions by chmod +x name

Debian Pastebin Thanks to folds at debian for making a tor network friendly pastebin. I have noticed that sometimes scripts get corrupted here, so best copy this from pastebin: https://paste.debian.net/1293211/

edit: deleted the codeblock as it was not rendering properly https://i.imgur.com/1lrTcdT.png

You can get the updated code here https://paste.debian.net/1293211/ Just paste it on to your notepad and give it execution permissions.

#edit 1: The program can be improved, if you guys feel like an improvement is needed, copy the entire program modify the parts you think can be made better and paste it in the comments or paste the pastebin link in the comments. I realize there are applications which are gui for yt-dlp but I felt they lacked many options. You can go on the man page, and learn and add attributes to make this script better so that it better suits your needs.

#man page aka github page: yt-dlp

4
1
submitted 1 year ago by Moshpirit@lemmy.world to c/bash@lemmy.ml

The issue is that if my script is correctly done, a SVG is supposed to be exported to PNG with a fixed width, the script seems to work fine, but when I check it out again on Inkscape*, it shows me the original SVG size, not the resized PNG exported picture.

Here's a folder with the script and a random SVG I'm using for testing.


*To check the size of the picture in Inkscape, we need to change the units in the top bar, and then see the numbers that shows. The screenshot shows a 14,79 cm x 9,85 cm, instead of 10,5 cm x 6,9 cm.

5
1
submitted 1 year ago by Moshpirit@lemmy.world to c/bash@lemmy.ml

I want to have a selector in a "case" menu, so I show the options:

   1) option A
   2) option B
   3) option C

Then read the choice (let's say it's B), remove the previous menu and show this instead:

   1) option A
 » 2) option B
   3) option C

How can I do this? I know we can remove the current line with echo -ne "\r", but I have no idea of how to do it with several

6
1
A terminal metronome (mastodon.social)
submitted 1 year ago by wolandark@lemmy.world to c/bash@lemmy.ml

So today I sat down to practice guitar and I realized that I left both my metronomes at my sister's room. Obviously I wasn't going to get up to go and fetch one, so I picked up a bash spell tome (man SoX) and with a little bash magic, made a basic metronome.

Here is the actual code:

tempo () 
{ 
play -n -c1 synth 0.001 sine 1000 pad $(awk "BEGIN { print 60/$1 -.001 }") repeat 999999
}
7
1
submitted 1 year ago* (last edited 1 year ago) by HnuWETqkp4YG@lemmy.world to c/bash@lemmy.ml

This script will get MPV watch history from files watch_later dir and display them in reverse order of watch. The list is numbered and a prompt for a number will play the desired file in MPV

Need line "write-filename-in-watch-later-config=yes" in mpv.conf Deps rg (ripgrep)

#!/usr/bin/env bash
# Return mpv watch history oldest to newest. 
# Need line "write-filename-in-watch-later-config=yes" in mpv.conf
# Deps rg

watch_later_dir="$HOME/.config/mpv/watch_later/"

SAVEIFS=$IFS
IFS=$'\n'

if [ ! -d "$watch_later_dir" ]; then
    echo "Specified dir doesn't exist: $watch_later_dir"
    echo "Set var watch_later_dir to your watch later dir"
    echo "also, mpv.conf should have line \"write-filename-in-watch-later-config=yes\""
    exit 1
fi

watch_later_files="$(find "$watch_later_dir" -type f -printf "%T@ %p\n" | sort | sed 's/^\([0-9]\+\.[0-9]\+\) //')"

file_count=$(find "$watch_later_dir" -type f | wc -l)

if [ "$file_count" -eq 0 ]; then
    echo "no files found in \"$watch_later_dir\""
    exit 1
fi

watch_later_files=($watch_later_files) 

filepaths_not_echoed="$(for (( i=0; i<${#watch_later_files[@]}; i++ ))
do
  cat "${watch_later_files[$i]}" | rg -o --color=never '(/|http).*'
done)"

filepaths_not_echoed=($filepaths_not_echoed) 

# Reverse the order of array
length=${#filepaths_not_echoed[@]}
for ((i=0; i<length/2; i++)); do
    temp="${filepaths_not_echoed[i]}"
    filepaths_not_echoed[i]="${filepaths_not_echoed[length-i-1]}"
    filepaths_not_echoed[length-i-1]="$temp"
done

filepaths="$(for (( i=0; i<${#watch_later_files[@]}; i++ ))
do
  echo -n "$(( $i - $file_count +1 )) " | sed 's/^-//' 
  cat "${watch_later_files[$i]}" | rg -o --color=never '/.*'
done)"

#echo "$filepaths" | perl -pe 's/^(\d+ ).*\//$1/g' | rg \
echo "$filepaths" | sed -E 's/^([0-9]+ ).*\//\1/g' | rg \
  --colors 'match:none' \
  --colors 'match:fg:0,200,0' \
  --colors 'match:bg:0,0,0' \
  --colors 'match:style:bold' \
  "[^0-9 ].*" 

IFS=$SAVEIFS

read -p "Enter number to play " selection

echo "${filepaths_not_echoed[$selection]}"

setsid >/dev/null 2>&1 </dev/null \
mpv "${filepaths_not_echoed[$selection]}" 2>&1 >/dev/null & 
8
1
submitted 1 year ago by HnuWETqkp4YG@lemmy.world to c/bash@lemmy.ml

This will convert all webp in a directory to jpg or mp4 if animated. It will utilize all cores with gnu parallel. It will also handle an error with ffmpeg that happens if animated webp pixel width or height is an odd number by padding 1 pixel if necessary

cat convertwebp

#!/usr/bin/env bash
# Convert webp to jpg if static or gif/mp4 if animated
# Pads animated webp to avoid "libx264 not divisible by 2 error"
# Deps imagemagick libwebp parallel

# Usage assuming saved as convertwebp
# convertwebp             # Will convert all webp in current dir to jpeg or mp4 and gif if animated
# convertwebp /some/dir/  # Same but on specified dir
# convertwebp /some/file  # Same but on specified file

######### Set jpg quality. There will be 2 outputs one of each quality setting
#          0-100 percent
export     QUALITY_ONE=35
export     QUALITY_TWO=75

find_args=(-maxdepth 1 -type f -regextype posix-extended -iregex '.*\.(webp)$' -print0)

dir="$PWD"
if [[ -d "$@" ]]; then
    dir="$@"
    echo "Dir $@"
else
    if [[ -f "$@" ]]; then
        dir="$@"
        echo "File $@"
    fi
fi

mkdir -p "$MY_DWEBP_OUTDIR"

find "$dir" "${find_args[@]}" | parallel -0 -j+0 --eta --bar '
jpg_out_quality_one=$(echo {/.}_"$QUALITY_ONE"_percent.jpg)
jpg_out_quality_two=$(echo {/.}_"$QUALITY_TWO"_percent.jpg)
png_out=$(echo {/.}.ffmpeg.png)
gif_out=$(echo {/.}.gif)
mp4_out=$(echo {/.}.mp4)
isanimated="$(webpmux -info {} | grep animation)"
if [[ "$isanimated" == "Features present: animation transparency" ]]; then
    convert '{}' "$gif_out"
    # Begin mp4 conversion handler to pad geometry 1 pixel to x and y if either are odd to avoid "libx264 not divisible by 2 error"
    geometry_x=$(webpmux -info '{}' | head -n 1 | tr "[:space:]" "\n" | tail -3 | head -n 1)
    geometry_y=$(webpmux -info '{}' | head -n 1 | tr "[:space:]" "\n" | tail -3 | tail -1)
    if [ $(( $geometry_x  % 2)) -ne 0 ] | [ $(( $geometry_y  % 2)) -ne 0 ]; then
        if [ $(( $geometry_x  % 2)) -ne 0 ] && [ $(( $geometry_y  % 2)) -ne 0 ]; then
            splice_geometry="1x1"
            gravity_direction="northeast"
            convert -splice $splice_geometry -gravity $gravity_direction '{}' "$mp4_out"
        else 
            if [ $(( $geometry_x  % 2)) -ne 0 ]; then
                splice_geometry="1x0"
                gravity_direction="east"
                convert -splice $splice_geometry -gravity $gravity_direction '{}' "$mp4_out"
            else
                if [ $(( $geometry_y  % 2)) -ne 0 ]; then
                    splice_geometry="0x1"
                    gravity_direction="north"
                    convert -splice $splice_geometry -gravity $gravity_direction '{}' "$mp4_out"
                fi
            fi
        fi
    else
        convert '{}' "$mp4_out"
    fi
    # End mp4 conversion handler to pad geometry 1 pixel to x and y if either are odd to avoid "libx264 not divisible by 2 error"
    else
        dwebp '{}' -o - | convert - -quality $QUALITY_ONE% "$jpg_out_quality_one"    # pipe to convert for filesize reduction
        dwebp '{}' -o - | convert - -quality $QUALITY_TWO% "$jpg_out_quality_two"    # pipe to convert for filesize reduction
fi
'
unset QUALITY_ONE
unset QUALITY_TWO
9
1
submitted 1 year ago by HnuWETqkp4YG@lemmy.world to c/bash@lemmy.ml

The default character set excludes easy to confuse characters ILOl0. It is fast too

Generating 1 million 40 character passwords

time pw -n 1000000 >/dev/null
0.47s user 0.24s system 229% cpu 0.310 total
cat pw
#!/usr/bin/env bash
#set -x

num_passwords=20                                  # Default number of passwords to return.
pw_len=40                                         # Default password length.
random_data='/dev/urandom'                        # Random data
urandom_bytes_default=300000                      # Default random bytes to read.

letters='A-HJ-KM-NP-Za-km-z'                      # Default letters set.
numbers='1-9'                                     # Default numbers set.
symbols='!?*^_@#%^&*()=+<>}{][;:",./|~\\'\''`-'   # Default symbols set. If dash "-" is needed, put it at the end
characters="$letters$numbers$symbols"             # All default sets combined

min_calculated_urandom_bytes=20000                # Minimum bytes when calculated. Fix issue when not enough data for simple character sets 
urandom_bytes_user=0                              # Leave at 0, for use with logic of -b , --bytes=
urandom_bytes_calculated=0                        # Leave at 0, for use with end logic
regex_match_flags="^-(b|-bytes=|c|-characters=|l|-length=)$" # Pattern to check against a flag being blank and reading next flag as arguemnt

while test $# -gt 0; do
  case "$1" in

    -h|--help)
      echo " "
      echo " "
      echo " "
      echo "pw - generate passwords"
      echo " "
      echo "pw [options]"
      echo " "
      echo "options:"
      echo "-b NUM   ,  --bytes=NUM          Specify bytes to read from "$random_data". Not compatible with flag -n, --ncount. Defaults to $urandom_bytes_default bytes"
      echo "-c 'CHAR',  --characters='CHAR'  Specify allowed password characters. Defaults to '$characters'"
      echo "-h       ,  --help               Show brief help"
      echo "-l NUM   ,  --length=NUM         Specify password length. Defaults to length of $pw_len"
      echo "-n NUM   ,  --ncount=NUM         Specify number of passwords to return. Not compatible with flag -b, --bytes"
      echo " "
      echo " "
      echo " "
      echo " "
      echo "examples:"
      echo " "
      echo " "
      echo "# 20 character alphanumeric with symbols "'!?"*#-'" using 20000 bytes of data from "$random_data""
      echo "pw --bytes=20000 --characters='a-zA-Z0-9"'!?"*#-'"' --length=20"
      echo "   IjLVomO*LZIvBWhmITtS"
      echo "pw -b 20000 -c 'a-zA-Z0-9"'!?"*#-'"' -l 20"
      echo "   IjLVomO*LZIvBWhmITtS"
      echo " "
      echo " "
      echo " "
      echo "# 200 passwords using default values"
      echo "pw --ncount=200"
      echo '   !=[8x|d`dHdVA-:xn8t>G=~tkgbg}T#~2(/r?9N&'
      echo "   ...{200 lines}"
      echo " "
      echo "pw -c '18bu' -l 10 -n 2"
      echo "   bb8b8bb1ub"
      echo "   88b1ub8b8u"
      echo " "
      echo " "
      echo "pw -c '0-4' --length=80 --ncount=10"
      echo "   10132440443120133034412013333104142320411133221101130324111200442311420044122312"
      echo " "
      echo " "
      echo "pw -c 'zplaeiou' --length=80 --ncount=1"
      echo "   uuzzzalilepauzuepaazoizoeiiaazupupalolzliluuoazluzuepzlozepapaioipupapleuzaolpuu"
      echo " "
      echo " "
      echo "pw -c '1-4*-' -l 10 -n 2"
      echo "   2414443*24"
      echo "   *123-*4-31"
      echo " "
      echo " "
      echo "pw -b 400 -c 'a-zA-Z0-9 [#!?*(){}~[]/\\-]'\''' -l 40"
      echo "   EVuMxtVR**6}?M2HTZlED{ARjKL?D]r8h[7Pidvo"
      echo " "
      echo " "
      echo " "
      exit 0
      ;;


    -b)
      shift
      # Test that -b value (previously shifted $1) is gt 0 before setting var urandom_bytes_user
      # And test that $pw_line_count_target has not been set
      if [[ $1 -gt 0 ]] && [[ -z $pw_line_count_target ]] 2> /dev/null; then
        urandom_bytes_user=$1
        urandom_bytes_default=0
        pw_line_count_target=0
      else
          printf "error: \"-b NUM\" needs numeral greater that 0. Value > 1000 recommended\n"
          exit 1
      fi
      shift
      ;;
    --bytes*)
      # Test that --bytes value "${1/*"="/}" is gt 0 before setting var urandom_bytes_user
      # And test that pw_line_count_target is not set
      if [[ "${1/*"="/}" -gt 0 ]] && [[ $pw_line_count_target -le 0 ]] 2> /dev/null; then
        urandom_bytes_user="${1/*"="/}"
        urandom_bytes_default=0
        pw_line_count_target=0
      else
        if [[ ! $pw_line_count_target -le 0 ]] 2> /dev/null; then
          printf "\nflag -n, --ncount not compatible with flag -b, --bytes\n"
          exit 1
        else
          printf "error: usage \"--bytes=NUM\" needs numeral greater that 0. Value > 1000 recommended\n"
          exit 1
        fi
      fi
      shift
      ;;


    -c)
      shift
      # Before set var characters, test for -c value (previously shifted $1) being blank,
      # or another flag shifted in as unintended -c value.
      if [[ ! -z $1 ]] && [[ ! "$1" =~ $regex_match_flags ]]; then
        characters="$1"
      else
        printf "error: usage \"-c 'CHARACTERS'\" (allowed password characters) needs value\n"
        exit 1
      fi
      shift
      ;;
    --characters*)
    # Before set var characters, test for --characters string "${1/*"="/}" being blank,
    # or another flag shifted in as unintended --characters string by checking 
    # $characters_to_check for regex match on $regex_match_flags.
    characters_to_check="${1/*"="/}"
      if [[ ! -z "${1/*"="/}" ]] && [[ ! "$characters_to_check" =~ $regex_match_flags ]]; then
        characters="${1/*"="/}"
      else
        printf "error: usage \"--characters 'CHARACTERS'\" (allowed password characters) needs value\n"
        exit 1
      fi
      shift
      ;;


    -l)
      shift
      # Test that -l value (previously shifted $1) is gt 0 before setting var pw_len
      if [ $1 -gt 0 ] 2> /dev/null; then
        pw_len=$1
      else
        printf "error: usage \"-l NUM\" (password length) needs numeral greater that 0\n"
        exit 1
      fi
      shift
      ;;
    --length*)
      # Test that --length value "${1/*"="/}" is gt 0 before setting var pw_len
      if [[ "${1/*"="/}" -gt 0 ]] 2> /dev/null; then
        pw_len="${1/*"="/}"
      else
        printf "error: usage \"--length=NUM\" (password length) needs numeral greater that 0\n"
        exit 1
      fi
      shift
      ;;


    -n)
      shift
      # Test that -b value (previously shifted $1) is gt 0 before setting var pw_line_count_target
      if [ $1 -gt 0 ] ; then
        pw_line_count_target=$1
        urandom_bytes_default=0
      else
          printf "error: \"-n NUM\" needs numeral greater that 0\n"
          exit 1
      fi
      shift
      ;;
    --ncount*)
      # Test that --bytes value "${1/*"="/}" is gt 0 before setting var pw_line_count_target
      if [[ "${1/*"="/}" -gt 0 ]] ; then
        pw_line_count_target="${1/*"="/}"
        urandom_bytes_default=0
      else
        printf "error: usage \"--ncount=NUM\" needs numeral greater that 0\n"
        exit 1
      fi
      shift
      ;;


    *)
      break
      ;;
  esac
done




# Test that urandom_bytes_user has not been changed from 0
# And test that pw_line_count_target gt 0
if [[ $pw_line_count_target -gt 0 ]] && [[ $urandom_bytes_user -eq 0 ]] ; then
  count_out_of_10000="$(head -c 10000 < "$random_data" | tr -dc "$characters" | wc -c)"
  urandom_bytes_calculated=$(( (13000/$count_out_of_10000) * ($pw_len * $pw_line_count_target) ))
  if [[ $urandom_bytes_calculated -lt $min_calculated_urandom_bytes ]] ; then
    urandom_bytes_calculated=$min_calculated_urandom_bytes
  fi
else
  if [[ $pw_line_count_target -gt 0 ]] && [[ $urandom_bytes_user -ne 0 ]] ; then
    printf "\nflag \" -n|--ncount \" not compatible with flag \" -b|--bytes \"\n"
    exit 1
  fi
fi


if [[ $pw_line_count_target -eq 0 ]]; then
  pw_line_count_target=$num_passwords
fi


# PW generation bits
urandom_bytes=$(( ($urandom_bytes_default) + ($urandom_bytes_user) + ($urandom_bytes_calculated) )) 
head -c     "$urandom_bytes"      < "$random_data" |
tr   -dc    "$characters"                          |
fold -s     -w$pw_len                              |
head -n     "$pw_line_count_target"

10
1
Using at command (lemmy.one)
submitted 1 year ago* (last edited 1 year ago) by sin_free_for_00_days@lemmy.one to c/bash@lemmy.ml

A lot of people haven't heard of the at command, and I just figured I'd post up a little bit about it. I use it as a semi-alarm clock type reminder.

Structure of command the way I use it:

echo "notify-send -u critical -t 0 'YOUR URGENT MESSAGE'" | at "2pm Jun 18"

I actually wrote a tiny function so that I don't have to remember the -u critical -t 0 part. That is to keep the notification box from timing out. Now tomorrow (June 18th) at 2pm, a message box will pop up with the text "YOUR URGENT MESSAGE".

There are options for running commands (like cron, but you don't have to edit, then delete your edit after running). It is great if you don't want to mess with cron, or another utility for a quick reminder.

My quick little function looks like this:

notify_me ()
{
    if [[ $# -ne 2 ]]; then
        echo 'Usage:  notify_me "message" time/date' 1>&2;
        echo '                   make sure to enclose message in quotes' 1>&2;
        echo "                   also enclose date if it's more than just a time" 1>&2;
        return;
    fi;
    echo "notify-send -u critical -t 0 '$1'" | at "$2"
}

So say I have to remind myself to call the wife when the work day is over. I'll do something like:

notify_me "Check with the ol' ball and chain" "4:45pm"

I'm sure if you wanted to, you could make a very quick zenity (or whatever you like) gui for it, if that's the way you roll. Now I'll get a nice little pop-up 15 minutes before I check out. Tiny little things like this are why I enjoy the command line so much.

For more uses, check out the man page, or the tldr. It's not complicated and I find it useful.

11
1
submitted 1 year ago by Plasma@lemmy.ml to c/bash@lemmy.ml
12
1
submitted 1 year ago by JollyRoberts@lemmy.ml to c/bash@lemmy.ml

Since the DNS lapsed for the wiki, someone grabbed a copy and put it up on github.

There is also the original webserver IP for /etc/hosts use, as the server is still there.

13
1
submitted 1 year ago* (last edited 1 year ago) by Plasma@lemmy.ml to c/bash@lemmy.ml

It's basically a builtin that you compile that can interface with routines in shared libraries, which means you could make GTK programs, network stacks, and more, all with bash and no external tools!

14
1
submitted 2 years ago by cypherpunks@lemmy.ml to c/bash@lemmy.ml
15
1
submitted 3 years ago* (last edited 3 years ago) by rmc0@lemmy.ml to c/bash@lemmy.ml

HI,

I don't use bar and status, only WM and some apps, so I use PS1 for see clock and battery status, clock change each time I enter a new prompt line but status battery change only with new shell

example :

PS1="$(cat /sys/class/power_supply/BAT0/capacity)% \A"

thanks

16
1
submitted 3 years ago* (last edited 3 years ago) by leanleft@lemmy.ml to c/bash@lemmy.ml

linux terminal

17
1
submitted 3 years ago* (last edited 3 years ago) by leanleft@lemmy.ml to c/bash@lemmy.ml

"We want to document one-liners for frequent (non-trivial) tasks executed in the shell"

18
1
submitted 3 years ago by leanleft@lemmy.ml to c/bash@lemmy.ml

linux shell manpage help

19
1
submitted 4 years ago* (last edited 4 years ago) by creativeBoarClimate@lemmy.ml to c/bash@lemmy.ml

I wasn't easily able to find a command line study timer so I made one.

I'm not a great programmer so I was wondering if I could get some feedback. Cheers!

20
1
submitted 4 years ago* (last edited 3 years ago) by Scupake@lemmy.ml to c/bash@lemmy.ml

A Static Site Generator Made by Taminaru in Bash!

The current features are:

  • Creating and editing posts in Markdown.

  • Using templates, variables.

  • Configuration, both local and universal (explained in README)

It's around 80% complete, so any contribution is appreciated (i would help if i wasn't lazy af lul).

Anyway here's the source code: https://codeberg.org/taminaru/BSG

Bash

0 readers
0 users here now

Talk about the Bash Shell and Bash scripting

founded 4 years ago
MODERATORS