Floating Yad Download Button

Moderator: Forum moderators

Post Reply
User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Floating Yad Download Button

Post by stemsee »

My opera browser keeps crashing when trying to download. So I just copy the url and paste in terminal after wget command.... this button automates the paste and wget command, but still have to copy download link first, then click Download button.

download.png
download.png (49.21 KiB) Viewed 2140 times

Code: Select all

#!/bin/sh
#
# determine COPY/PASTE command
if [ ! -z `busybox which xsel` ]; then
	export CLIP="xsel -o -p"
elif [ ! -z `busybox which xclip` ]; then
	export CLIP="xclip -o -p"
elif [ ! -z `busybox which clipit` ]; then
	export CLIP="clipit -p"
fi

yad --form --field="DOWNLOAD!gtk-refresh!copy a download link first:fbtn" "bash -c 'wget $($CLIP)'" --no-buttons --undecorated --on-top &
plinej
Posts: 243
Joined: Thu Jul 16, 2020 1:39 am
Has thanked: 7 times
Been thanked: 100 times

Re: Floating Yad Download Button

Post by plinej »

@stemsee, I like this and built on it a bit in case anyone is interested. If you run the code below, it will prompt you for your download directory and then just watch the clipboard. You'll get a popup when your clipboard is populated. The popup will show the link and then you can click "ok" to download, "cancel" to skip, "quit" to exit the program. The downloaders will be either aria2c, wget, or curl. You'll get a popup of the defaultterminal monitoring the download progress with the tail command and then a yad popup after the download with either a successful or failure message of your download.

Code: Select all

#!/bin/bash

run=true
get_clipboard() {
	if [ "`which xsel`" != "" ]; then
		xsel -o -p
	elif [ "`which xclip`" != "" ]; then
		xclip -o -p
	elif [ "`which clipit`" != "" ]; then
		clipit -p
	fi
}
yad_clip() {
	result=$(yad --button=gtk-ok:2 --button=gtk-cancel:3 --button=gtk-quit:1 --title="$(gettext 'clipboard downloader')" --text="$(gettext 'click okay to download, cancel to skip, quit to exit program')" --form --field="" "$clip" --geometry=550x50+500+0)
	r2=$?
	if [ "$r2" = 1 ]; then
		run=false
	fi
	if [ "$r2" = 2 ]; then
		output=`echo "$result" | cut -f 1 -d '|'`
		older=`find . -maxdepth 1 -type f | xargs -d '\n' ls -ltr | tail -n 1 | cut -f 2 -d '/'`
		if [ "$output" != "" ]; then
			if [ "`which aria2c`" != "" ]; then
				aria2c "$output" &> /tmp/tmp$$ &
			elif [ "`which wget`" != "" ]; then
				wget --content-disposition "$output" &> /tmp/tmp$$ &
			elif [ "`which curl`" != "" ]; then
				curl -JLOR "$output" &> /tmp/tmp$$ &
			fi
			downloadpid=$!
			defaultterminal -e tail -f /tmp/tmp$$
			while s=`ps -p $downloadpid -o s=` && [[ "$s" && "$s" != 'Z' ]]; do
				sleep 1
			done
			rm -f /tmp/tmp$$ 2>/dev/null
			newest=`find . -maxdepth 1 -type f | xargs -d '\n' ls -ltr | tail -n 1 | cut -f 2 -d '/'`
			if [ "$newest" = "$oldest" ]; then
				yad --no-markup --text="$(gettext 'download appears to have failed')" --text-align=center --center
			else
				find "$newest" -type f -empty -delete 2>/dev/null
				if [ -f "$newest" ]; then
					yad --no-markup --text="$newest $(gettext 'is the newest file in directory') `pwd`" --text-align=center --center
				else
					yad --no-markup --text="$(gettext 'download appears to have failed')" --text-align=center --center
				fi
			fi
		fi
	fi
}
dir=`yad --width=800 --height=400 --title "$(gettext 'Select directory to save downloads to')" --file --directory`
if [ -d "$dir" ]; then
	cd "$dir"
fi
clip=`get_clipboard 2>/dev/null | tail -n 1`
if [ "$clip" != "" ]; then
	yad_clip
fi
while [ "$run" = true ]; do
	sleep 1
	newclip=`get_clipboard 2>/dev/null | tail -n 1`
	if [ "$newclip" != "" ] && [ "$newclip" != "$clip" ]; then
		clip="$newclip"
		yad_clip
	fi
done
You can download my packages at https://archive.org/download/plinej-packages
User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

But where is the floating download button?

EDIT: oh I see .... just copying triggers the popup....even better! so place in /root/Startup or /etc/xdg/Startup

Also needs a filter to get a url from the clipboard, rather than the latest copied item

plinej
Posts: 243
Joined: Thu Jul 16, 2020 1:39 am
Has thanked: 7 times
Been thanked: 100 times

Re: Floating Yad Download Button

Post by plinej »

A filter for a url should be pretty easy to incorporate.

You can download my packages at https://archive.org/download/plinej-packages
plinej
Posts: 243
Joined: Thu Jul 16, 2020 1:39 am
Has thanked: 7 times
Been thanked: 100 times

Re: Floating Yad Download Button

Post by plinej »

@stemsee, This code will only pop up when your copied text contains any of the following - www - http - ftp.

Code: Select all

#!/bin/bash

run=true
get_clipboard() {
	if [ "`which xsel`" != "" ]; then
		xsel -o -p
	elif [ "`which xclip`" != "" ]; then
		xclip -o -p
	elif [ "`which clipit`" != "" ]; then
		clipit -p
	fi
}
yad_clip() {
	result=$(yad --button=gtk-ok:2 --button=gtk-cancel:3 --button=gtk-quit:1 --title="$(gettext 'clipboard downloader')" --text="$(gettext 'click okay to download, cancel to skip, quit to exit program')" --form --field="" "$clip" --geometry=550x50+500+0)
	r2=$?
	if [ "$r2" = 1 ]; then
		run=false
	fi
	if [ "$r2" = 2 ]; then
		output=`echo "$result" | cut -f 1 -d '|'`
		older=`find . -maxdepth 1 -type f | xargs -d '\n' ls -ltr | tail -n 1 | cut -f 2 -d '/'`
		if [ "$output" != "" ]; then
			if [ "`which aria2c`" != "" ]; then
				aria2c "$output" &> /tmp/tmp$$ &
			elif [ "`which wget`" != "" ]; then
				wget --content-disposition "$output" &> /tmp/tmp$$ &
			elif [ "`which curl`" != "" ]; then
				curl -JLOR "$output" &> /tmp/tmp$$ &
			fi
			downloadpid=$!
			defaultterminal -e tail -f /tmp/tmp$$
			while s=`ps -p $downloadpid -o s=` && [[ "$s" && "$s" != 'Z' ]]; do
				sleep 1
			done
			rm -f /tmp/tmp$$ 2>/dev/null
			newest=`find . -maxdepth 1 -type f | xargs -d '\n' ls -ltr | tail -n 1 | cut -f 2 -d '/'`
			if [ "$newest" = "$oldest" ]; then
				yad --no-markup --text="$(gettext 'download appears to have failed')" --text-align=center --center
			else
				find "$newest" -type f -empty -delete 2>/dev/null
				if [ -f "$newest" ]; then
					yad --no-markup --text="$newest $(gettext 'is the newest file in directory') `pwd`" --text-align=center --center
				else
					yad --no-markup --text="$(gettext 'download appears to have failed')" --text-align=center --center
				fi
			fi
		fi
	fi
}
dir=`yad --width=800 --height=400 --title "$(gettext 'Select directory to save downloads to')" --file --directory`
if [ -d "$dir" ]; then
	cd "$dir"
fi
clip=`get_clipboard 2>/dev/null | tail -n 1`
if [ "$clip" != "" ]; then
	if [ "`echo $clip | grep -iE 'http|ftp|www'`"  != "" ]; then
		yad_clip
	fi
fi
while [ "$run" = true ]; do
	sleep 1
	newclip=`get_clipboard 2>/dev/null | tail -n 1`
	if [ "$newclip" != "" ] && [ "$newclip" != "$clip" ]; then
		clip="$newclip"
		if [ "`echo $clip | grep -iE 'http|ftp|www'`"  != "" ]; then
			yad_clip
		fi
	fi
done
You can download my packages at https://archive.org/download/plinej-packages
User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

It's working nicely! :thumbup2:

User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

@plinej
I've been using the script for a day and these are my thoughts: 1) when it checks newst against oldest does it take into account that duplicate downloads get appended with (n), where n is a number. Better to force overwrite imo. And then no need for the test, or the popup. A timeout popup say download successful should be enough.

2) If I want to make successive downloads it seems I cannot at least until the last one is complete. By back-grounding the download task other downloads can occur, which i think will be more convenient.

3) the popup gui with the link might be better with --on-top so you don't miss it, as happened to me, while trying to download rapid fire!

plinej
Posts: 243
Joined: Thu Jul 16, 2020 1:39 am
Has thanked: 7 times
Been thanked: 100 times

Re: Floating Yad Download Button

Post by plinej »

@stemsee, I made a pet package called "ydown". The main script calls another script now. You'll only need to set your download directory once on the initial run. If you want to change it you'll need to click the preferences button on the main popup when a link is copied. You'll be able to download multiple files at once.

ydown-0.1.pet
(2.27 KiB) Downloaded 191 times
You can download my packages at https://archive.org/download/plinej-packages
User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

It's a really good implementation @plinej!

I'm glad my initial idea, triggered your creativity, to produce your app! It's definitely yours and you should claim copyright in the script. And also maintain and improve it too :lol:

I made it fatdog compatible

Code: Select all

#!/bin/bash
# Copyright (C) 2023 plinej
# developed from an idea by stemsee
#
version=0.1

if [ "`busybox which yad`" = "" ]; then
	echo yad $(gettext 'not installed, exiting.')
	exit
fi
	
[ "$TERM" ]	&& defaultterminal="$TERM"

array=(wget defaultterminal)
for item in ${array[*]}
do
	if [ "`busybox which $item`" = "" ]; then
		yad --text="$item $(gettext 'needs to be installed to use this program.')" --text-align=center --center --on-top
		exit
	fi
done

get_clipboard() {
	if [ "`busybox which xsel`" != "" ]; then
		xsel -o -p
	elif [ "`busybox which xclip`" != "" ]; then
		xclip -o -p
	elif [ "`busybox which clipit`" != "" ]; then
		clipit -p
	else
		yad --text="$(gettext 'install either') xsel, xclip, clipit $(gettext 'to use this program.')" --text-align=center --center --on-top
		exit
	fi
}
yad_clip() {
	if [[ "`echo $clip | grep ^[0-9] | wc -c`" = 8 ]]; then
		newclip=`echo "$clip" | awk 'BEGIN{FS="[^A-Za-z0-9_]"} {for(i=1;i<=NF;i++) {if($i ~ /([[:digit:]][[:alpha:]])|([[:alpha:]][[:digit:]])/) {print $i} } }'`
		if [ "$clip" = "$newclip" ]; then
			clip=http://bit.ly/$clip
		fi
	fi
	if [[ "`echo $clip | grep -iE 'http|ftp|www'`" != "" ]]; then
		result=$(yad --button=gtk-ok:2 --button=gtk-preferences:5 --button=gtk-cancel:3 --button=gtk-quit:1 --title="$(gettext 'clipboard downloader')" --text="$(gettext 'click okay to download, cancel to skip, quit to exit program')" --form --field="" "$clip" --geometry=550x50+500+0 --on-top)
		r2=$?
		if [ "$r2" = 1 ]; then
			run=false
		fi
		if [ "$r2" = 5 ]; then
			dir=""
			rm -f ~/.ydown 2>/dev/null
			get_dir
			r2=2
		fi
		if [ "$r2" = 2 ]; then
			file=""
			export output=`echo "$result" | cut -f 1 -d '|'`
			if [ "$output" != "" ]; then
				if [ "`echo $output | grep 'file=/'`" != "" ]; then
					export file=`echo $output | grep 'file=/' | sed 's/file=\//|/' | cut -f 2 -d '|'`
				fi
				if [ "$file" = "" ]; then
					export file=`timeout 4 wget --server-response -q -O - "$output" 2>&1 | grep "Content-Disposition:" | tail -1 | cut -f 3 -d "'"`
				fi
				if [ "$file" = "" ]; then
					export file=`basename "$output"`
				fi
				/usr/share/ydown/download &
			fi
		fi
	fi
}
get_dir (){
	if [ -f ~/.ydown ]; then
		dir=`cat ~/.ydown`
	fi
	if ! [ -d "$dir" ]; then
		dir=`yad --width=800 --height=400 --title "$(gettext 'Select directory to save downloads to')" --file --directory`
	fi
	if [ -d "$dir" ]; then
		cd "$dir"
		echo "$dir" > ~/.ydown
	else
		cd ~
	fi
}

run=true
get_dir

clip=`get_clipboard 2>/dev/null | tail -n 1`
if [ "$clip" != "" ]; then
	yad_clip
fi
while [ "$run" = true ]; do
	sleep 1
	newclip=`get_clipboard 2>/dev/null | tail -n 1`
	if [ "$newclip" != "" ] && [ "$newclip" != "$clip" ]; then
		clip="$newclip"
		yad_clip
	fi
done
plinej
Posts: 243
Joined: Thu Jul 16, 2020 1:39 am
Has thanked: 7 times
Been thanked: 100 times

Re: Floating Yad Download Button

Post by plinej »

@stemsee, sounds good. I've been tinkering away at it but will add in your changes. Fatdog requires 'busybox which' instead of just which?

You can download my packages at https://archive.org/download/plinej-packages
User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

plinej wrote: Tue Apr 18, 2023 7:44 pm

Fatdog requires 'busybox which' instead of just which?

Fatdog has the full which binary in path. Full which when not finding an executable returns a string basically saying '*executable not fine in $PATH', so it never returns an empty string which screws up some tests. Solution is either specify busybox which, or redirection 'which xterm 2>/dev/null' .... but to change an entire script easily it's easier to replace 'which' with 'busybox which' than to replace 'which "*.elf"' with 'which "*.elf" 2>/dev/null' depending on your skill of course!.... for me it's easier.

User avatar
fredx181
Posts: 3279
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 411 times
Been thanked: 1421 times
Contact:

Re: Floating Yad Download Button

Post by fredx181 »

True that 'which' gives a message on Fatdog when executable not found, but still it works for me as expected on Fatdog without using 'busybox which'.
Try: [ "`which bla`" = "" ] && echo "bla not found"

Code: Select all

 # [ "`which bla`" = "" ] && echo "bla not found"
which: no bla in (/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/opt/qt5/bin)
bla not found

never returns an empty string

Somehow it does return an empty string, otherwise the echo message would not appear.

EDIT: Same result when doing e.g. [ -z "$(which bla)" ] && echo "bla not found"

User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

that's interesting @fredx181 also my version of fd doesn't have a busybox which link in path, i have only one result.

The output of full which is an error message, that's why 2>/dev/null redirection works, because it's an error redirection, right.

I only discovered these 'problems' after trying your scripts in fd since a long time ago. So I got in the habit of specifying busybox which, and I think it is the safest option across all possible distros, because the behaviour is predictable. It's not for 'fun' that I raise this issue, I have had scripts which wouldn't run as desired because of not being an empty string, and busybox which solved all such issues.

But at least more coders are aware of the issue.

User avatar
MochiMoppel
Posts: 1298
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 480 times

Re: Floating Yad Download Button

Post by MochiMoppel »

stemsee wrote: Thu Apr 20, 2023 11:21 am

So I got in the habit of specifying busybox which, and I think it is the safest option across all possible distros, because the behaviour is predictable.

@stemsee @fredx181 Just curious: Why do you use which at all? It's neither reliable nor efficient. The bash built-in type is faster and the output is predictable: Returns full path if file is in PATH, no output if it is not.

Code: Select all

#!/bin/bash
if [[ $(type -p xsel) ]] ; then
	export CLIP="xsel -o -p"
elif [[ $(type -p xclip) ]] ; then
	export CLIP="xclip -o -p"
elif [[ $(type -p clipit) ]]; then
	export CLIP="clipit -p"
fi

BTW: If you use the #!/bin/sh shebang in FD your script will run in dash, not bash, which may produce unexpected results unless you test your code in both shells - or stick to bash.

Last edited by MochiMoppel on Thu Apr 20, 2023 1:28 pm, edited 1 time in total.
User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

es, type -p replaces which successfully. But is there also a full type binary lurking somewhere with it's own special behaviour?

User avatar
MochiMoppel
Posts: 1298
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 480 times

Re: Floating Yad Download Button

Post by MochiMoppel »

Even if there were a binary called type in PATH, your shell wouldn't execute it because the built-in has always precedence

User avatar
fredx181
Posts: 3279
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 411 times
Been thanked: 1421 times
Contact:

Re: Floating Yad Download Button

Post by fredx181 »

stemsee wrote: Thu Apr 20, 2023 11:21 am

that's interesting @fredx181 also my version of fd doesn't have a busybox which link in path, i have only one result.

The output of full which is an error message, that's why 2>/dev/null redirection works, because it's an error redirection, right.

I only discovered these 'problems' after trying your scripts in fd since a long time ago. So I got in the habit of specifying busybox which, and I think it is the safest option across all possible distros, because the behaviour is predictable. It's not for 'fun' that I raise this issue, I have had scripts which wouldn't run as desired because of not being an empty string, and busybox which solved all such issues.

But at least more coders are aware of the issue.

OK, but still I don't understand how it works for you on FatDog, just curious, does [ -z "$(which blablablah)" ] && echo "blablablah not found" give message "blablablah not found" for you or not ? (as I said it does for me).
If it does, then there's no real problem with 'which' I'd say. (but other ways would be better indeed, thanks @MochiMoppel )

plinej
Posts: 243
Joined: Thu Jul 16, 2020 1:39 am
Has thanked: 7 times
Been thanked: 100 times

Re: Floating Yad Download Button

Post by plinej »

@fredx181, It's because the error message is printing to STDERR while the code is looking for STDOUT so it should work as long as you're using BASH.

You can download my packages at https://archive.org/download/plinej-packages
plinej
Posts: 243
Joined: Thu Jul 16, 2020 1:39 am
Has thanked: 7 times
Been thanked: 100 times

Re: Floating Yad Download Button

Post by plinej »

I uploaded ydown-0.2 @ viewtopic.php?t=8550

You can download my packages at https://archive.org/download/plinej-packages
step
Posts: 561
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 60 times
Been thanked: 207 times
Contact:

Re: Floating Yad Download Button

Post by step »

@fredx181 I too was curious to see what various shells do in Fatdog. Here's the result:

Code: Select all

# bash -c '[ -z "$(which blablablah)" ] && echo "blablablah not found"'
which: no blablablah in (/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin)
blablablah not found
# sh -c '[ -z "$(which blablablah)" ] && echo "blablablah not found"'
which: no blablablah in (/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin)
blablablah not found
# ash -c '[ -z "$(which blablablah)" ] && echo "blablablah not found"'
which: no blablablah in (/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin)
blablablah not found
# dash -c '[ -z "$(which blablablah)" ] && echo "blablablah not found"'
which: no blablablah in (/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin)
blablablah not found
# zsh -c '[ -z "$(which blablablah)" ] && echo "blablablah not found"'
#
# fish -c '[ -z "$(which blablablah)" ] && echo "blablablah not found"'
fish: $(...) is not supported. In fish, please use '(which)'.
[ -z "$(which blablablah)" ] && echo "blablablah not found"
      ^
# fish -c '[ -z "(which blablablah)" ] && echo "blablablah not found"'
#

zsh and fish work differently but as documented in their manuals.

User avatar
MochiMoppel
Posts: 1298
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 480 times

Re: Floating Yad Download Button

Post by MochiMoppel »

step wrote: Thu Apr 20, 2023 8:43 pm

Izsh and fish work differently but as documented in their manuals.

I wonder if all shells would work the same when you don't check the output string of the which command, which is irrelevant here anyway, but rather its exit code. IMO this would be cleaner and more reliable code.

Code: Select all

   sh -c 'which blablablah >/dev/null || echo "blablablah not found"'
 fish -c 'which blablablah >/dev/null || echo "blablablah not found"'
flzsh -c 'which blablablah >/dev/null || echo "blablablah not found"'
step
Posts: 561
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 60 times
Been thanked: 207 times
Contact:

Re: Floating Yad Download Button

Post by step »

MochiMoppel wrote: Fri Apr 21, 2023 12:55 am

I wonder if all shells would work the same when you don't check the output string of the which command, which is irrelevant here anyway, but rather its exit code. IMO this would be cleaner and more reliable code.

Code: Select all

   sh -c 'which blablablah >/dev/null || echo "blablablah not found"'
 fish -c 'which blablablah >/dev/null || echo "blablablah not found"'
flzsh -c 'which blablablah >/dev/null || echo "blablablah not found"'

I think so. Every *nix program has an exit code, and every shell (that I know of) returns it when the program terminates. I don't know if all shells return the exit code in the $? variable and provide the || and && operators, but the ones in use in this forum do (the Bourne shell derivatives, of which fish isn't). In addition to the way you wrote the above code, which I would certainly use in my own scripts, I also use other forms depending on the context, such as

Code: Select all

command -v cmd > /dev/null || echo cmd not found # builtin; can also use 'type' instead of 'command'
if command -v cmd > /dev/null; then
  echo cmd found
else
  echo cmd not found
fi

I prefer the if-then-else form because it gives me complete control on the value that $? can take after the 'fi' keyword. That is, I can add more commands in the then and else bodies and affect $? with those. This makes the whole if-then-else chainable with || and &&, or simply by checking the value of $? after 'fi'. Another advantage is that it makes using the shell's -e flag easier and more clear.
As you mentioned earlier in this thread, writing stuff like [ -z "$(cmd ...)" ] is more complex, less portable and less-efficient (because it spawns a sub-shell needlessly).

One use case for preferring 'which' to a similar builtin is when you want to take the first of a series of equally viable commands. That is, 'which cmd1 cmd2 cmd3 cmd4' outputs the first installed among cmd1,...,cmd4. This could be cmd2 on a system, cmd1 on another, and so on. Extension: When the cmds are equivalent but need different arguments to be so, 'case-esac' can be used on the output of a single 'which' invocation.

I tested the code you wrote above with fish and zsh and they both print "blablablah not found". Fish also prints an error message from which. Zsh doesn't because which is a builtin. However, zsh's 'where' is the idiom for 'which', which leads us to who's on first for a closing.

User avatar
MochiMoppel
Posts: 1298
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 480 times

Re: Floating Yad Download Button

Post by MochiMoppel »

step wrote: Fri Apr 21, 2023 5:43 am

One use case for preferring 'which' to a similar builtin is when you want to take the first of a series of equally viable commands. That is, 'which cmd1 cmd2 cmd3 cmd4' outputs the first installed among cmd1,...,cmd4. This could be cmd2 on a system, cmd1 on another, and so on.

I would not count on it :lol:
Here on F96-CE 'which' is symlinked to busybox, and this works as I would expect, i.e. it returns all viable commands

Code: Select all

# which edlin leafpad geany mp
/usr/bin/leafpad
/usr/bin/geany
/usr/bin/mp
#
# type -p edlin leafpad geany mp
/usr/bin/leafpad
/usr/bin/geany
/usr/bin/mp

Sometimes I wish I could get the first command more easily than employing something like

Code: Select all

# type -p edlin leafpad geany mp | head -1
/usr/bin/leafpad
step
Posts: 561
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 60 times
Been thanked: 207 times
Contact:

Re: Floating Yad Download Button

Post by step »

I didn't count on 'which' to _only_ output the first available cmd. I rather counted on the reader to understand that the first output line is the first available cmd from the argument list.

MochiMoppel wrote: Fri Apr 21, 2023 6:37 am

Sometimes I wish I could get the first command more easily than employing something like

Code: Select all

# type -p edlin leafpad geany mp | head -1
/usr/bin/leafpad

That's easy enough for most. This is also easy for some:

Code: Select all

set -- `type -p edlin leafpad geany mp`; echo $1

It is slightly more efficient and preserves all output lines. It overwrites script arguments ($*) but that's easily taken care of by wrapping the set command inside a function that uses its arguments for the type command, and uses $1 for further processing, for instance to set a global variable, like

Code: Select all

set_edit_cmd() {
  set -- `type -p edlin leafpad geany mp`
  edit_cmd=$1
}
set_edit_cmd
echo "edit command: $edit_cmd"

I apologize to the original poster for going off-topic. I will refrain from doing so from now on.

User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

@fredx181

fredx181 wrote: Thu Apr 20, 2023 5:09 pm

OK, but still I don't understand how it works for you on FatDog, just curious, does [ -z "$(which blablablah)" ] && echo "blablablah not found" give message "blablablah not found" for you or not ? (as I said it does for me).
If it does, then there's no real problem with 'which' I'd say. (but other ways would be better indeed, thanks @MochiMoppel )

test.png
test.png (12.75 KiB) Viewed 1969 times
Last edited by stemsee on Fri Apr 21, 2023 11:18 am, edited 1 time in total.
User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

step wrote:

I apologize to the original poster for going off-topic. I will refrain from doing so from now on.

Hey, who would not be happy to increase their knowledge from others more advanced!?!

User avatar
fredx181
Posts: 3279
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 411 times
Been thanked: 1421 times
Contact:

Re: Floating Yad Download Button

Post by fredx181 »

stemsee wrote: Fri Apr 21, 2023 11:15 am

@fredx181

fredx181 wrote: Thu Apr 20, 2023 5:09 pm

OK, but still I don't understand how it works for you on FatDog, just curious, does [ -z "$(which blablablah)" ] && echo "blablablah not found" give message "blablablah not found" for you or not ? (as I said it does for me).
If it does, then there's no real problem with 'which' I'd say. (but other ways would be better indeed, thanks @MochiMoppel )

test.png

Ok, so the message shows for you.
Because you said that there's a problem with some of my scripts, I was concerned, as I have some shared with "just which" inside (that's why I pushed a little to get to the bottom).
Now I'm not that concerned anymore, but still don't understand what exactly was the problem that you experienced :?:

User avatar
stemsee
Posts: 788
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 8
Has thanked: 188 times
Been thanked: 133 times

Re: Floating Yad Download Button

Post by stemsee »

fredx181 wrote: Fri Apr 21, 2023 2:53 pm

Ok, so the message shows for you.
Because you said that there's a problem with some of my scripts, I was concerned, as I have some shared with "just which" inside (that's why I pushed a little to get to the bottom).
Now I'm not that concerned anymore, but still don't understand what exactly was the problem that you experienced :?:

Not a problem, per se, It was my experience that to get your scripts to work in fd, the minimum I 'had' to do was the busybox which substitution. Probably the only testing string that caused a problem was 'if [ -z `which blahblahblah` ]; then' , it could be that dash and not bash was being used! My knowledge is lacking.....but i was able to get the scripts working, whereas before they would not work! Maybe fatdog changed, or busybox or which got compiled with updated options in the meantime, and I never noticed ... so now no substitution is required.

Anyway, 'type -p' substitution is also ok, maybe better. But I found I cannot test for 'type' using 'type -p type' or 'which type' ! So that's interesting.

User avatar
MochiMoppel
Posts: 1298
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 480 times

Re: Floating Yad Download Button

Post by MochiMoppel »

stemsee wrote: Sat Apr 22, 2023 8:24 am

Anyway, 'type -p' substitution is also ok, maybe better. But I found I cannot test for 'type' using 'type -p type' or 'which type' ! So that's interesting.

Of course you can test. type -p type (small letter p!) tells you which file would be executed when you use the type command. If there is no file (which is normal) the output is empty.
If you *would* have a file called 'type' in your PATH, the -P option (capital P!) would return the path of this file. To see builtins and files use type -a type

As always it helps to study the fine print:

Code: Select all

# help type
type: type [-afptP] name [name ...]
    Display information about command type.
    
    For each NAME, indicate how it would be interpreted if used as a
    command name.
    
    Options:
      -a	display all locations containing an executable named NAME;
    		includes aliases, builtins, and functions, if and only if
    		the `-p' option is not also used
      -f	suppress shell function lookup
      -P	force a PATH search for each NAME, even if it is an alias,
    		builtin, or function, and returns the name of the disk file
    		that would be executed
      -p	returns either the name of the disk file that would be executed,
    		or nothing if `type -t NAME' would not return `file'
      -t	output a single word which is one of `alias', `keyword',
    		`function', `builtin', `file' or `', if NAME is an alias,
    		shell reserved word, shell function, shell builtin, disk file,
    		or not found, respectively
    
    Arguments:
      NAME	Command name to be interpreted.
    
    Exit Status:
    Returns success if all of the NAMEs are found; fails if any are not found.
Post Reply

Return to “Utilities”