You are not logged in.

#1 2016-06-13 10:13:22

Snap
Member
Registered: 2015-10-02
Posts: 465

Need some advice with a bash script

My bash scripting skills are still pretty minimal. Making constant progress but still crap.

I have the weird habit of collecting bookmarks as *.desktop files better than inside browsers or lists. As the collected bookmarks count becomes huge I find easier to deal with them placing these desktop files on the corresponding or related directories along with anything else related, documents, pictures, etc... For example. If I have something like a link to a vim tutorial or a vim cheatsheet they will live inside ~/Linux/Progs/Vim. Anything vim related will be also there. Or anything cooking related will live inside ~/Recetas (I love cooking).

Something like this: (lives in "~/Guitar/Lutherie/Repair Guides")

Frank_Ford.desktop

[Desktop Entry]
Icon=emblem-symbolic-link
Type=Link
Name=http://www.frets.com/FretsPages/pagelist.html#Luthier
Exec=x-www-browser -new-tab http://www.frets.com/FretsPages/pagelist.html#Luthier

I normally make them manually in a text editor from a blank template.desktop file adding the link addresses and giving it a name. I'm trying to make this easier with a script. This is my dumb attempt:

#!/bin/bash

printf "%s\n" "" "Enter the link" ""
read link
printf "%s\n" "" "Give it a name" ""
read name
cat > ~/Downloads/"$name".desktop << "EOF"
[Desktop Entry]
Icon=emblem-symbolic-link
Type=Link
Name="$link"
Exec=x-www-browser -new-tab "$link"
EOF

It obviously doesn't work. It seems cat is not the right tool for this. It strictly copies into the file what it's given to it.

How would you, bash savvy guys, approach this?

Thanks in advance.

Last edited by Snap (2016-06-13 10:26:18)

Online

#2 2016-06-13 10:27:36

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Need some advice with a bash script

Just delete the double quotes around EOF in the line starting with 'cat'

info bash wrote:

   No parameter and variable expansion, command substitution,
arithmetic expansion, or filename expansion is performed on WORD.  If
any characters in WORD are quoted, the DELIMITER is the result of quote
removal on WORD, and the lines in the here-document are not expanded.
If WORD is unquoted, all lines of the here-document are subjected to
parameter expansion, command substitution, and arithmetic expansion,
the character sequence `\newline' is ignored, and `\' must be used to
quote the characters `\', `$', and ``'.

Offline

#3 2016-06-13 10:40:51

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2015-09-29
Posts: 450

Re: Need some advice with a bash script

For example:

#!/bin/bash

printf "%s\n" "" "Enter the link" ""
read link

printf "%s\n" "" "Give it a name" ""
read name
outfile="$HOME/Downloads/${name}.desktop"

printf "[Desktop Entry]
Icon=emblem-symbolic-link
Type=Link
Name=${link}
Exec=x-www-browser -new-tab ${link}" > "${outfile}"

HTH

EDIT: Ninja'd by xaos ...  sad  He is so much smarter than me  wink

Last edited by iMBeCil (2016-06-13 10:41:58)


Postpone all your duties; if you die, you won't have to do them ..

Offline

#4 2016-06-13 10:47:47

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Need some advice with a bash script

Here is how I would do this: (retaining the spaces as you had them)

#!/bin/bash
read -p "Enter the link:

" -r link
read -p "
Give it a name:

" -r name
cat > ~/Downloads/"$name".desktop << EOF
[Desktop Entry]
Icon=emblem-symbolic-link
Type=Link
Name="$link"
Exec=x-www-browser -new-tab "$link"
EOF

Saves calling the printf command twice.

PS: Don't delete the empty lines. They are part of the 'prompt' string.

Offline

#5 2016-06-13 16:09:46

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,434

Re: Need some advice with a bash script

You got me htmling now, how is this for homepage (with some colorfull buttons, all credits to bootstrap4).

Last edited by brontosaurusrex (2016-06-15 12:39:32)

Offline

#6 2016-06-14 07:57:36

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Need some advice with a bash script

Hey, Doctor. Thanks a lot for your help and the quote on the quotations. LOL. Understood. Lesson learned.

What I still don't get is the prompt part. man read is not very helpful about the switches (options?) you are using there. Please, can you point me into the right direction?

@ iMBeCil: I do not know what makes yours worse than the xaos52 take (which I don't doubt it's  one of the best possible options to do this for sure), but the replacement of cat for printf is exactly what I was asking about. Thanks for your contribution. I need to learn the coreutils better.

Online

#7 2016-06-14 09:09:01

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Need some advice with a bash script

'read' is a bash built-in command.
Use 'help read' in a terminal window to get more info.

Offline

#8 2016-06-14 09:11:50

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2015-09-29
Posts: 450

Re: Need some advice with a bash script

Snap wrote:

What I still don't get is the prompt part. man read is not very helpful about the switches (options?) you are using there. Please, can you point me into the right direction?

Try

$ help read

You are probably using bash built-in 'read' command.

Snap wrote:

@ iMBeCil: I do not know what makes yours worse than the xaos52 take (which I don't doubt it's  one of the best possible options to do this for sure), but the replacement of cat for printf is exactly what I was asking about. Thanks for your contribution. I need to learn the coreutils better.

You're welcome.

Xaos solution? His initial answer pinpoint exact mistake you have made. My solution changed lot of your code. In this sense, he is much more economical  wink
As for the 'coreutil' ... Whenever I forget bash details, I visit Advanced Bash-Scripting Guide. It is definitely worth reading and/or having around.

Edit: aaaaargh, not again!  monkey  monkey  monkey

Last edited by iMBeCil (2016-06-14 09:12:39)


Postpone all your duties; if you die, you won't have to do them ..

Offline

#9 2016-06-14 09:38:06

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Need some advice with a bash script

wink

Offline

#10 2016-06-14 13:13:54

Sector11
Tpyo Knig
From: 77345 ¡#
Registered: 2015-08-20
Posts: 5,111

Re: Need some advice with a bash script

Off Topic but related.

Not sure why brontosaurusrex's post is here but I am glad it is.
In his link about a homepage I saw:

href="http://www.shellcheck.net/"

So I checked it out.  WOW!  And in the repos as well:

 14 Jun 16 @ 09:54:27 ~
  $ shellcheck /home/sector11/bin/degree

In /home/sector11/bin/degree line 20:
if [ $choice -eq 1 ]
     ^-- SC2086: Double quote to prevent globbing and word splitting.


In /home/sector11/bin/degree line 27:
elif [ $choice -eq 2 ]
       ^-- SC2086: Double quote to prevent globbing and word splitting.

 
 14 Jun 16 @ 09:58:00 ~
  $ 

However the site online "links" those: 'SC2086' type error codes and is a bit more thorough This is the same bash script checked online:

$ shellcheck myscript
 
Line 18:
read choice
^-- SC2162: read without -r will mangle backslashes.
 
Line 20:
if [ $choice -eq 1 ]
     ^-- SC2086: Double quote to prevent globbing and word splitting.
 
Line 23:
read tc
^-- SC2162: read without -r will mangle backslashes.
 
Line 27:
elif [ $choice -eq 2 ]
       ^-- SC2086: Double quote to prevent globbing and word splitting.
 
Line 30:
read tf
^-- SC2162: read without -r will mangle backslashes.

$ 

Now I have to check all my scripts  smile

Thank you Snap for bringing this up and Thank you brontosaurusrex for your post.


BunsenLabs Forum Rules ---== I'm a Conky 1.9'er ==---
System:    Host: s12 Kernel: 3.16.0-4-amd64 x86_64 (64 bit gcc: 4.8.4)
Desktop: Openbox 3.5.2 dm: (startx) Distro: Debian GNU/Linux 8

Offline

#11 2016-06-15 04:14:55

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Need some advice with a bash script

Thanks for the guidance and explanations, guys. Doing my homework. This one is helpful too.

https://www.gnu.org/software/bash/manua … h-Builtins

@ bronto & S11: Huh, I need to redo a darn lot of scripts. That's a great tool. Thanks!

Last edited by Snap (2016-06-15 04:54:17)

Online

#12 2016-06-16 06:00:03

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Need some advice with a bash script

This script is wonderful (thanks again, xaos52) It makes easier and faster making these launchers instead of making them in a text editor every time. Probably nobody else wants to use desktop launchers like these, but just in case, here's the way I use them.

Made a different version for links to video clips. I call it mclip:

#!/bin/bash
read -p "Enter the link:

" -r link
read -p "
Give it a name:

" -r name
cat > ~/Downloads/"$name".desktop << EOF
[Desktop Entry]
Icon=mpv
Type=Link
Name="$link"
Exec=mpv --force-window --ytdl-format=mp4 --fs "$link"
EOF

And have them both bound to keystrokes launching my default terminal emulator running a command like this:

xfce4-terminal --tab --drop-down -T "mlink" -x mlink

Using this kind of desktop links from GUI file managers is easypeasy. But launching them directly from the CLI or from CLI file managers like Ranger need a workaround. Here you can find options for this. Tried them all and the nicest one is the one by Six using awk. Check the link above and scroll down to The Manual Alternative. It can be used as a script:

#!/bin/sh
$(awk '/^Exec=/ {sub("^Exec=", ""); gsub(" ?%[cDdFfikmNnUuv]", ""); command=$0} END{if (command!="") {exit system(command)} else {if (success!=1) {if (FILENAME == "-") {printf "ERROR: Failed to identify Exec line\n" > "/dev/stderr"} else {printf "ERROR: Failed to identify Exec line in \047%s\047\n", FILENAME > "/dev/stderr"}; exit 1}}}' “$1”)

Or as a bash function:

function yourfavoritenamehere() { awk '/^Exec=/ {sub("^Exec=", ""); gsub(" ?%[cDdFfikmNnUuv]", ""); command=$0} END{if (command!="") {exit system(command)} else {if (success!=1) {if (FILENAME == "-") {printf "ERROR: Failed to identify Exec line\n" > "/dev/stderr"} else {printf "ERROR: Failed to identify Exec line in \047%s\047\n", FILENAME > "/dev/stderr"}; exit 1}}}' "$@" ;}

Now for Ranger you'll need the script better than the function. Surely there is a better way to do it but this is how I got it working. As usual, place the script above somewhere in your $PATH and flag it executable. Then add this to your Ranger's rifle.conf

ext desktop		= deskopen "$1"

Ranger will still consider *.desktop files as text files and want to open them with the default editor as the first option (0) which I find ok, a pager as second option (1) and now deskopen will show as a third option (2) if you hit the r key. So just directly hitting the 2 and return will launch these links nicely.

Maybe all this is helpful for someone.

Cheers.

Last edited by Snap (2016-06-16 06:34:21)

Online

#13 2016-06-16 06:44:34

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Need some advice with a bash script

For the lazy (like me) I've tampered the (formerly clean and tidy) sctripts like this:

mlink

#!/bin/bash
# Borrowed from xaos52 with some xdotool ideas from gotbletu
# browser to focus; show list using wmctrl -l
BROWSER_FOCUS="Pale Moon"

# focus browser on top
wmctrl -a "$BROWSER_FOCUS"

# copy link to clipboard
sleep 0.5
xdotool key ctrl+l
sleep 0.5
xdotool key ctrl+c
# focus drop-down terminal
xdotool key F12
read -p "
Give it a name:

" -r name
cat > ~/Downloads/"$name".desktop << EOF
[Desktop Entry]
Icon=emblem-symbolic-link
Type=Link
Name="$(xclip -selection clipboard -o | cut -d\& -f1)"
Exec=x-www-browser -new-tab "$(xclip -selection clipboard -o | cut -d\& -f1)"
EOF

mclip

#!/bin/bash
# Borrowed from xaos52 with some xdotool ideas from gotbletu
# browser to focus; show list using wmctrl -l
BROWSER_FOCUS="Pale Moon"

# focus browser on top
wmctrl -a "$BROWSER_FOCUS"

# copy link to clipboard
sleep 0.5
xdotool key ctrl+l
sleep 0.5
xdotool key ctrl+c
# focus drop-down terminal
xdotool key F12
read -p "
Give it a name:

" -r name
cat > ~/Downloads/"$name".desktop << EOF
[Desktop Entry]
Icon=mpv
Type=Link
Name="$(xclip -selection clipboard -o | cut -d\& -f1)"
Exec=mpv --force-window --ytdl-format=mp4 --fs "$(xclip -selection clipboard -o | cut -d\& -f1)"
EOF

They need xdotool and wmctrl. They are intended for xfce4-terminal (drop-down). You may need to adapt it for you drop-down key of choice or invoquing your terminal of choice instead (drop-down capable or not). I use the Palemoon browser. Don't forget to edit the $BROWSER_FOCUS variable to fit your thing.

Cheers.

Last edited by Snap (2016-06-16 07:03:32)

Online

#14 2016-06-16 10:05:27

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Need some advice with a bash script

Nice ones, Snap smile
Thanks for posting them.

Offline

#15 2016-06-17 06:06:14

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Need some advice with a bash script

Glad you liked them. Anyway, credit where it belongs. I just put things together.

Online

#16 2016-06-17 14:27:58

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,434

Re: Need some advice with a bash script

@Snap: mclip does what exactly?

getfav, a little module which could be used as a grabber for (fav)icons and used with Icon=/path/to/that.png in desktop files.

# getfav url name
# example:
getfav https://duckduckgo.com duck
# from script: 
favicon=$(getfav $link $name)

favicons.th.png

Last edited by brontosaurusrex (2016-06-17 15:52:46)

Offline

#17 2016-06-18 06:03:16

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Need some advice with a bash script

^ Just makes a desktop file launcher for an URL. A sick way of dealing with bookmarks as I've explained in the OP. I prefer them this way. Easier to deal with when your bookmarks count reaches the gazillion. They're also browser agnostic as long as you set an x-www-browser in Debian or $BROWSER for other distros (Just edit the scripts accordingly).

mclip does the same as mlink, but instead of launching the link into a web browser it streams video clips directly with mpv.

Thanks for getfav. Interesting tool

Last edited by Snap (2016-06-18 06:07:30)

Online

#18 2018-03-12 17:37:56

martix
Kim Jong-un Stunt Double
Registered: 2016-02-19
Posts: 1,245

Re: Need some advice with a bash script

As a side note: On Pale Moon there is the add-on "ViewMarks". It uses icons for bookmarks and is very convenient to use.

Offline

Board footer

Powered by FluxBB