What programing/scripting languages do you use and why?

Nice, an annoyance fixed - cheers!

Nice!

Me and my girl were talking last night while I whittled on this. Why isn’t there a social music player? Something along the line of:

Player notification: Jane and Joe are also listening to Plea For Tenderness right now
Me: Hey, you two have good taste.
Jane: Thanks. You too.
Joe: Have you heard Train Coming Round the Bend?
Jane: Yes, but it’s a good one.
Me: Not until now. It’s great. Thanks.
Jane: If you like that, you might like this playlist I made. sends embedded .m3u file

Most recco’s these days are from algorithms. Not straight from people, with social interaction. People used to listen to music socially.

Hmmm. It looks like yt-dlp adds it’s own “” around the url. I think the only way to solve it is to get the video id.

My girl is at work, so I get to tinker. This snippet seems to work for getting a video id from a youtube address. Maybe I’m overlooking something though. Tossing the full url into an array on the first IFS line, split at ‘=’. In the second line, taking in the second element of URL_SPLIT and splitting further at ‘&’ (in case of playlist and options in url), which puts the video id in VIDEO_ID. Something like that. A youtube url is provided in a comment for convenience of trying that it works.

#!/bin/bash
# https://www.youtube.com/watch?v=vjoCi7_xdWc

echo "Enter a youtube url:"
read FULL_URL
IFS="=" read -a URL_SPLIT <<< $FULL_URL
IFS="&" read -a VIDEO_ID <<< ${URL_SPLIT[1]}
echo "The video id is $VIDEO_ID"

So hopefully the first bug is fixed for zsh compatibility. But I found another to fix along the way. I used ‘AC/DC’ in a file name and it caused some trouble. See if I can patch that up. Expansions are a pain in the ass. I really need to sharpen up on that stuff.

Edit: Added a ‘Known issues’ comment on the forward slash issue. Use backlashes or pipes in file names instead on Linux. It may differ on Windows.

#!/bin/bash

# ---- What it does ----
# Makes audio playlists from yt-dlp output
# Prompts for youtube url, desired file name, and desired playlist name, all on new lines
# Creates a directory of the same name as the user provided playlist name in MUSIC_DIR
# Grabs the audio and renames it to desired name (suggested name format:  Artist - Song)
# Adds the new file name to the desired .m3u playlist file

# ---- Requirements ----
# Run at your own risk
# Uses yt-dlp (supersedes youtube-dl) to grab audio
# Save this file to whatever file name you like
# Give it executable permission (for linux:  chmod 755 file_name)
# Change value of MUSIC_DIR in line below to your desired playlists directory
MUSIC_DIR="/home/user1/Music/playlists"
# Add this script to your environment PATH to prevent headaches

# ---- Known issues ----
# Using forward slashes in file names causes problems on Linux.  
# Workaround:  Use backslash instead, as in 'AC\DC - BACK In Black'.  '|' also works.

# ---- Instructions ----
# Run, paste url, and enter
# Provide desired file name and enter (.mp3 is automatically appended)
# Provide desired playlist name and enter


# Get user info
echo -e "*** MUSIC PLAYLISTER ***\n"
echo -e "	Enter: youtube video address, file name (artist - song), playlist name\n"
read FULL_URL
read -r FILE
read PLAYLIST


MUSIC_DIR="/home/user1/Music/playlists"


clear
cd "$MUSIC_DIR"


# Create directory in MUSIC_DIR of same name as playlist if it doesn't exist
if ! [[ -d "$MUSIC_DIR/$PLAYLIST" ]];then
	mkdir -v "$PLAYLIST"
	cd "$MUSIC_DIR/$PLAYLIST"
fi


cd "$MUSIC_DIR/$PLAYLIST"


# Get audio file if it doesn't exist
if ! [[ -f "$MUSIC_DIR/$PLAYLIST/$FILE" ]];
then
	IFS="=" read -a URL_SPLIT <<< $FULL_URL
	IFS="&" read -a VIDEO_ID <<< ${URL_SPLIT[1]}
	yt-dlp -x --audio-format mp3 -o "$FILE".mp3 $VIDEO_ID
fi


# Add audio file to playlist, creating new playlist if it doesn't exist
echo "$FILE".mp3 >> "$PLAYLIST".m3u

Started on a menu thing.

#!/bin/bash
# Stupid little menu script for a stupid little playlist script
# Does nothing useful yet

cd /home/user1/Music/playlists
directory=$(pwd)
echo --- PLAYLIST MAKER ---
echo -e "You are in $directory\n"

echo "Select playlist option"
PS3="> "
select option in "Add to existing playlist" "Create new playlist" "Set new parent directory" "Exit";
do
	clear
	if [[ $REPLY -eq 1 ]];then
		echo "Your existing playlists:"
		ls -d --color */
		echo "Enter a playlist name."
		read playlist
		clear
		cd "$playlist"
		echo "Adding to" "$playlist"
	elif [[ $REPLY -eq 2 ]];then
		echo "What is the name for the new playlist?"
		read playlist
		mkdir "$playlist"
		echo "The playlist named" "$playlist" "was created."
		echo "Adding to" "$playlist"
	elif [[ $REPLY -eq 3 ]];then
		clear
		echo -e "Enter parent directory path:\n"
		read parent_directory
		clear
		echo "$parent_directory" > "$parent_directory"/playlist_maker.conf
		echo -e playlist_maker.conf in "$parent_directory" was updated."\n"
	elif [[ $REPLY -eq 4 ]];then
		break;
	fi
done

Good stuff, I’ve never seen that select option in thing before.

I think I haven’t seen it either. Apparently it is a bash built-in, which I didn’t think about at the time for compatibility with other shells. So no man page for it, and the page help select is just a paragraph, so not that helpful really. Searching for menu ideas brought it up in the results with some usage examples. By the way, I forgot about not needing the while loop when leaving off from this yesterday (removing the while loop now). The select option in takes care of looping. This page was way more helpful than the help page. https://linuxize.com/post/bash-select/

That brings up one of my big dislikes about the commandline on linux. I always wanted some practical usage examples in the documentation. Man pages can be very terse, dense, and cryptic. And other documentation resources available from the commandline can be too short or non-existent.

I also didn’t know about <<<, or I had forgotten about it / skimmed over it long ago. It is called a here-string. https://www.baeldung.com/linux/heredoc-herestring

Any experinces or impressions of Tcl?

I had heard the name but never looked at it, it looks like its claim to fame is Tk but I think I’d rather use Python for that.

I remember doing scripts in REAPER years ago that used tkinter and it didn’t have 100% cross platform compatibility at all times. That was the time going from Python 2 → 3 as well which caused pain. As did Python scripts interfering with input to REAPER. But that’s not relevant to just using it standalone.

Last I had left off, I stupidly got stuck in bash in an infinite loop. Here is a simplified snippet to demonstrate the problem. Can you spot it? In case bash syntax is unfamiliar, -ne is an integer operator for ‘not equal’.

#!/bin/bash
number=1
while [[ $number -ne 0 ]]
do
	echo "Enter a number "
	read $number
	echo "Number is $number"
done

First guess is that read reads a string which is never 0 because 0 is a number?

No, but you’re onto the right command.

read $number doesn’t read input into $number? It’s bash so it could be anything!

Does read create a new variable in the inner scope?

Bash treats number as an assignment, as in number=1.
Bash treats $number as an expansion to the value stored in number.

So then, for the command read $number in the while loop, bash never assigns anything. It just reads the value already assigned and on and on it goes.

Bash’s expansions got me again!

Once we know we know! :slight_smile:

That’s a strange one, so you have to read into a variable that doesn’t exist yet?

edit: oh, it’s the dollah!

No, you can read into a variable already created, but you read into number not $number. This was weird for me too for some reason.

Maybe we’ve been exposed to too much $p$h$p to notice right away? :smiley:

Yea. And it is logical, but I keep running into these little expansion issues and banging my head on rocks. In other languages you assign and get the assigned value via the same name, number.

That’s coding thug life, it’s good for the soul!