LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2013, 06:44 AM   #1
äxl
Member
 
Registered: Feb 2013
Location: Germany, EU
Distribution: Debian (stable release)
Posts: 67

Rep: Reputation: 10
Lightbulb Bash script: Convert MS URL files to Linux desktop links


Hi folks! This is mainly for my own record but maybe it can help others.
I made some links in Windows to web pages but all I got was these .URL files.
Code:
[InternetShortcut]
URL=http://www.w3.org/
IDList=
HotKey=0
IconFile=C:\Users\User\AppData\Roaming\Mozilla\Firefox\Profiles\8rn9f680.User\shortcutCache\gt5hKbqP7+v5RX5n3nCzwW==.ico
IconIndex=0
They contain a URL= line but the Linux equivalent is slightly different.
Code:
[Desktop Entry]
Type=Link
URL=http://www.w3.org/show/217230
These three lines a sufficent for a working web link. Here's my bash script that will convert all URL files to .desktop files:
Code:
#!/bin/bash
#case-insensitive
shopt -s nocaseglob
#give folder as argument or execute in current directory
[ -n "${1}" -a -d "${1}" ] && cd "${1}"
for file in *.url
do
	URLLINE=$(grep -Ei url= "$file")
	FILENAME=`echo $(basename "$file") | cut -d'.' -f1`
echo -ne "[Desktop Entry]\nType=Link\n$URLLINE" > "$FILENAME.desktop"
done
As always save as file and make executable.

#Licence: CC BY-SA
#Sources: ubuntuuser.de, tldp.org, linuxquestions.org

Last edited by äxl; 11-15-2013 at 01:20 PM.
 
Old 11-11-2013, 08:37 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by äxl View Post
Code:
	FILENAME=`echo $(basename "$file") | cut -d'.' -f1`
echo -ne "[Desktop Entry]\nType=Link\n"$URLLINE > "$FILENAME.desktop"
Neat . A quick note: you can use bash parameter expansion instead of substitution for the file name... e.g.

Code:
#method #1 using parameter expansion: Remove matching suffix pattern.
echo -ne "[Desktop Entry]\nType=Link\n"$URLLINE > "${file%.url}.desktop"

#method #2 using parameter expansion: Pattern  substitution.
echo -ne "[Desktop Entry]\nType=Link\n"$URLLINE > "${file/%.url/.desktop}"
Also you should check to see if it is a directory before using the cd command. You should also quote the variable in case there's spaces provided in the argument. e.g.

Code:
[ -n "${1}" -a -d "${1}" ] && cd "${1}"
You should also include $URLLINE in the script within the quote. Always quote variables as a rule of thumb. e.g.

Code:
echo -ne "[Desktop Entry]\nType=Link\n${URLLINE}" > "${file%.url}.desktop"

Last edited by sag47; 11-11-2013 at 08:40 AM.
 
1 members found this post helpful.
Old 11-15-2013, 01:25 PM   #3
äxl
Member
 
Registered: Feb 2013
Location: Germany, EU
Distribution: Debian (stable release)
Posts: 67

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by sag47 View Post
Neat .
Thanks.

Quote:
A quick note: you can use bash parameter expansion instead of substitution for the file name...
Nice.
Got this from http://wiki.ubuntuusers.de/TS-Daten_archivieren so blame them.

Quote:
Also you should check to see if it is a directory before using the cd command. You should also quote the variable in case there's spaces provided in the argument. e.g.
Both aren't case-insensitive. #2 doesn't work for *.URL at all.
#1 converts but doesn't delete the "url" part of the name. But that's not really important.

Quote:
Code:
[ -n "${1}" -a -d "${1}" ] && cd "${1}"
Right. Thanks.

Quote:
You should also include $URLLINE in the script within the quote. Always quote variables as a rule of thumb. e.g.

Code:
echo -ne "[Desktop Entry]\nType=Link\n${URLLINE}" > "${file%.url}.desktop"
Hm. I had this before but it wasn't working then. Thanks.

Oh, and sometimes I still get "Could not dispay "[link]". The file is of unknown type".
Which can simply be corrected by opening the .desktop-file (which is a nuisance) and save.
 
Old 11-15-2013, 02:30 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by äxl View Post
Which can simply be corrected by opening the .desktop-file (which is a nuisance) and save.
Shot in the dark but since you open the .desktop file and save it then perhaps it's an EOL issue and using the dos2unix utility will fix that from your script.
 
1 members found this post helpful.
  


Reply

Tags
bash, script, windows



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to convert .avi to .flv files and then zipping them Iyyappan Linux - Server 14 08-23-2012 04:10 AM
[SOLVED] stuck at bash script to convert files with ffmpeg panseluta Linux - Newbie 6 03-10-2011 09:20 AM
Bash script to convert all mp3 files to aac eeepc4gsurf Linux - Desktop 7 11-13-2009 07:25 PM
Bash script to convert .mpg files to .avi files? Mike_Snyder Linux - Newbie 5 09-19-2009 01:33 PM
Tricky: Script to remove Dublets URL links into an HTML page? frenchn00b Programming 4 05-25-2009 05:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration