LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Selecting preset variables by user input in a bash script (https://www.linuxquestions.org/questions/programming-9/selecting-preset-variables-by-user-input-in-a-bash-script-4175429215/)

Lateralus138 09-26-2012 11:19 PM

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


konsolebox 09-27-2012 12:19 AM

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>&-


Lateralus138 09-27-2012 01:04 AM

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...

konsolebox 09-27-2012 01:21 AM

Sorry my mistake. It should be:
Code:

[[ $dsname == +([[:alnum:]_ ]) ]]

Lateralus138 09-27-2012 01:28 AM

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:~$


konsolebox 09-27-2012 01:45 AM

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>&-


konsolebox 09-27-2012 01:52 AM

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.

Lateralus138 09-27-2012 01:57 AM

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?

konsolebox 09-27-2012 02:01 AM

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


Lateralus138 09-27-2012 02:17 AM

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.

konsolebox 09-27-2012 02:22 AM

Welcome. It was a pleasure.

Lateralus138 09-27-2012 02:27 AM

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?

konsolebox 09-27-2012 02:33 AM

Quote:

Originally Posted by Lateralus138 (Post 4790430)
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.


All times are GMT -5. The time now is 04:35 AM.