LinuxQuestions.org
Review your favorite Linux distribution.
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 09-26-2012, 11:19 PM   #1
Lateralus138
LQ Newbie
 
Registered: Sep 2012
Location: Decatur, Il. US
Distribution: Ubuntu
Posts: 11

Rep: Reputation: 0
Selecting preset variables by user input in a bash script


Hello, I am writing a script to make it quicker to create desktop (.desktop) shortcuts. So far this script is only for programs found in /usr/bin (although you can right click on the shortcut and change it how you like), but eventually I will make this universal the more I learn about scripting in Linux (I can create Windows batch scripts in my sleep). I don't really need shortcuts, I just make them for my girlfriend.

My question is:
What is the best method or how do I go about letting a user select a preset option and echoing that option into the file?

Example:

I would have set variables such as:
Code:
a=Application
f=Folder
l=Link
and the user would be asked to make a choice like:
Code:
read -p "Type of shortcut, choices are a=Application, f=Folder or l=Link:" stype
Then the selection would be fed into the document like:
Code:
echo Type=$stype >> ~/Desktop/$dsname.desktop
Here is my script so far which works for me, but just trying to improve it and make it more universal with better choices:
Code:
#!/bin/bash
#Create real desktop shorcuts (.desktop)
echo
echo    ================================
echo    ================================
echo    == Ian Pride\'s .desktop maker ==
echo    ================================
echo    ================================
echo      
read -p "Name your shortcut, single word only:" dsname
touch ~/Desktop/$dsname.desktop
sudo chmod 777 ~/Desktop/$dsname.desktop
echo [Desktop Entry] >> ~/Desktop/$dsname.desktop
echo Encoding=UTF-8 >> ~/Desktop/$dsname.desktop
echo Version=1.0 >> ~/Desktop/$dsname.desktop
clear
echo
read -p "Type of shortcut, choices are Application, Folder or Link:" stype
echo Type=$stype >> ~/Desktop/$dsname.desktop
clear
echo
read -p "Run in terminal, true or false?" term
echo Terminal=$term >> ~/Desktop/$dsname.desktop
ls /usr/bin >> /tmp/bin.txt
clear
echo
echo -e "A list of programs found in /usr/bin will now\nbe displayed. Copy the name of the program\nyou are making a shortcut for and paste into\nthe following Program Name prompt. Use ctrl+x\nto exit the next window"
read -p "Press Enter to continue" nul
nano /tmp/bin.txt
clear
echo
read -p "Program name:" path
echo Exec=/usr/bin/$path >> ~/Desktop/$dsname.desktop
echo Name=$dsname >> ~/Desktop/$dsname.desktop
ls ~/.icons >> /tmp/icons.txt
clear
echo
echo -e "A list of icons found in /home/<username>/\n.icons will now be displayed. Copy the name\nof the icon you are making a shortcut for and\npaste into the following Icon Name prompt.\nUse ctrl+x to exit the next window."
read -p "Press Enter to continue" nul
nano /tmp/icons.txt
clear
echo
read -p "Icon Name if it exists. (Place all icons in /home/<username>/.icons folder):" icon
echo Icon=~/.icons/$icon >> ~/Desktop/$dsname.desktop
clear
echo
read -p "Write any comments here:" comment
echo Comment=$comment >> ~/Desktop/$dsname.desktop
sudo rm -r /tmp/bin.txt
sudo rm -r /tmp/icons.txt
 
Old 09-27-2012, 12:19 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Well at my own preference I'd do it to something like this:
Code:
#!/bin/bash

# Create real desktop shorcuts (.desktop)

echo
echo "================================"
echo "================================"
echo "== Ian Pride's .desktop maker =="
echo "================================"
echo "================================"
echo

shopt -s extglob

until
	read -p "Name your shortcut, single word only:" dsname
	[[ $dsname == +([[alnum]]_ ) ]]
do
	echo "Please enter a valid shortcut name."
done

DESKTOP_FILE="~/Desktop/$dsname.desktop"

exec 4>"$DESKTOP_FILE"

>&4 echo "[Desktop Entry]"
>&4 echo "Encoding=UTF-8"
>&4 echo "Version=1.0"

# clear

echo
for (( ;; ))
	read -p "Type of shortcut, choices are a=Application, f=Folder or l=Link:" stype
	case "$dsname" in
	a)
		>&4 echo "Type=Application"
		break
		;;
	f)
		>&4 echo "Type=Folder"
		break
		;;
	l)
		>&4 echo "Type=Link"
		break
		;;
	*)
		echo "Please only enter a, f, or l."
		;;
	esac
done

# clear

echo
for (( ;; ))
	read -p "Run in terminal, true or false?" term
	case "$dsname" in
	[tT][rR][uU][eE])
		>&4 echo "Terminal=true"
		break
		;;
	[f][F][a][A][l][L][s][S][e][E])
		>&4 echo "Terminal=false"
		break
		;;
	*)
		echo "Please only enter true or false."
		;;
	esac
done

...

exec 4>&-
 
Old 09-27-2012, 01:04 AM   #3
Lateralus138
LQ Newbie
 
Registered: Sep 2012
Location: Decatur, Il. US
Distribution: Ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
I tried that and everytime I entered a name for my shortcut it just kept looping back to

Please enter a valid shortcut name.
Name your shortcut, single word only:Startup
Please enter a valid shortcut name.
Name your shortcut, single word only:Startup
Please enter a valid shortcut name.
Name your shortcut, single word only:Startup

etc...
 
Old 09-27-2012, 01:21 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Sorry my mistake. It should be:
Code:
[[ $dsname == +([[:alnum:]_ ]) ]]
 
Old 09-27-2012, 01:28 AM   #5
Lateralus138
LQ Newbie
 
Registered: Sep 2012
Location: Decatur, Il. US
Distribution: Ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
And with that I get

Code:
Name your shortcut, single word only:Startup
./desktop_bin_shortcut2: line 24: ~/Desktop/Startup.desktop: No such file or directory
./desktop_bin_shortcut2: line 26: 4: Bad file descriptor
./desktop_bin_shortcut2: line 27: 4: Bad file descriptor
./desktop_bin_shortcut2: line 28: 4: Bad file descriptor

./desktop_bin_shortcut2: line 34: syntax error near unexpected token `read'
./desktop_bin_shortcut2: line 34: `	read -p "Type of shortcut, choices are a=Application, f=Folder or l=Link:" stype'
ian@Hell:~$
 
Old 09-27-2012, 01:45 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Ok here's an update:
Code:
#!/bin/bash

# Create real desktop shorcuts (.desktop)

echo
echo "================================"
echo "================================"
echo "== Ian Pride's .desktop maker =="
echo "================================"
echo "================================"
echo

shopt -s extglob

until
	read -p "Name your shortcut, single word only: " dsname
	[[ $dsname == +([[:alnum:]_ ]) ]]
do
	echo "Please enter a valid shortcut name."
done

DESKTOP_FILE=~
DESKTOP_FILE="$DESKTOP_FILE/Desktop/$dsname.desktop"

exec 4>"$DESKTOP_FILE"

>&4 echo "[Desktop Entry]"
>&4 echo "Encoding=UTF-8"
>&4 echo "Version=1.0"

# clear

echo
for (( ;; )); do
	read -p "Type of shortcut, choices are a=Application, f=Folder or l=Link: " stype
	case "$stype" in
	a)
		>&4 echo "Type=Application"
		break
		;;
	f)
		>&4 echo "Type=Folder"
		break
		;;
	l)
		>&4 echo "Type=Link"
		break
		;;
	*)
		echo "Please only enter a, f, or l."
		;;
	esac
done

# clear

echo
for (( ;; )); do
	read -p "Run in terminal, true or false? " term
	case "$term" in
	[tT][rR][uU][eE])
		>&4 echo "Terminal=true"
		break
		;;
	[f][F][a][A][l][L][s][S][e][E])
		>&4 echo "Terminal=false"
		break
		;;
	*)
		echo "Please only enter true or false."
		;;
	esac
done

...

exec 4>&-
 
Old 09-27-2012, 01:52 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
This
Code:
DESKTOP_FILE=~
DESKTOP_FILE="$DESKTOP_FILE/Desktop/$dsname.desktop"

exec 4>"$DESKTOP_FILE"
Could be just
Code:
exec 4>~"/Desktop/$dsname.desktop"
But I thought that the instance of string would be used multiple times so I thought about saving it on a variable for convenience.

I also thought if you could also use $HOME:
Code:
exec 4>"$HOME/$dsname.desktop"
But I'm not sure what would be the impact if $HOME is not set.

Last edited by konsolebox; 09-27-2012 at 01:56 AM.
 
Old 09-27-2012, 01:57 AM   #8
Lateralus138
LQ Newbie
 
Registered: Sep 2012
Location: Decatur, Il. US
Distribution: Ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
Ok that works until you get to the terminal part for true and false and then I get a loop again:

Run in terminal, true or false? false
Please only enter true or false.
Run in terminal, true or false? false
Please only enter true or false.


and also in order for the file to change from Startup.desktop to just Startup and actually execute the permissions must be changed and in my script I did this successfully with

Code:
sudo chmod 777 ~/Desktop/$dsname.desktop
Can I assume I can add this line after the point of file creation in your script? As long as it's not in a function?
 
Old 09-27-2012, 02:01 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Actually was just a presentation of concept so that you could get an idea about which improvements you could incorporate to your code. Feel free to do anything about it.

Anyway about the permission thing, I think it's only user that needs the write bit, so perhaps it should be just 755 if not 750 or 700.
Code:
[f][F][a][A][l][L][s][S][e][E]) # should of course be [fF][aA][lL][sS][eE])
---- Add ----

Also I don't think it's necessary to use sudo just to change the permissions. The one who executes the script is the owner of the file after all.

And also, add it after closing the file
Code:
exec 4>&-
# change file permissions here

Last edited by konsolebox; 09-27-2012 at 02:06 AM.
 
Old 09-27-2012, 02:17 AM   #10
Lateralus138
LQ Newbie
 
Registered: Sep 2012
Location: Decatur, Il. US
Distribution: Ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
Ok I am starting to see how it's working now and will play with it to integrate it inot my script. This is totally different from any of the scripting tutorials or reference guides I've seen so I have a great deal more learning to do lol. Thanx for your help.
 
Old 09-27-2012, 02:22 AM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Thumbs up

Welcome. It was a pleasure.
 
Old 09-27-2012, 02:27 AM   #12
Lateralus138
LQ Newbie
 
Registered: Sep 2012
Location: Decatur, Il. US
Distribution: Ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
Sorry, one more thing even if I add an Exec=/usr/bin/gnome-session-properties and have set the permisions it has not removed the .desktop from being visible. The program starts up just fine though. Maybe after I reboot it may dissapear?
 
Old 09-27-2012, 02:33 AM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by Lateralus138 View Post
Sorry, one more thing even if I add an Exec=/usr/bin/gnome-session-properties and have set the permisions it has not removed the .desktop from being visible. The program starts up just fine though. Maybe after I reboot it may dissapear?
Hmm.. sorry, but I haven't encountered a problem like that before. Actually, I don't know much about .desktop files. I only made those that were placed in /usr/share/applications. Was the permission set to 755? Perhaps another daemon from a different user is reading it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 predefined user input loopstr Programming 6 08-10-2009 01:10 PM
[SOLVED] How to get "case" to overwrite preset variables in a bash script Mogget Programming 4 02-24-2009 01:19 PM
calling user input in bash script dbmacartney Linux - General 3 05-19-2008 06:55 AM
mask user input in a bash script PlatinumRik Linux - Software 1 06-15-2004 10:06 AM
User input using a BASH script... causticmtl Programming 5 07-13-2003 09:59 PM

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

All times are GMT -5. The time now is 09:23 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