Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Bash sudo and zenity --password

  1. #11
    Join Date
    Aug 2008
    Location
    Sweden
    Beans
    305
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash sudo and zenity --password

    Hi, thanks for joining in and helping out!

    Well, idk if I've made some terrible misstake with the code-snippet I provided as I got the feeling that I'm getting missunderstood... The goal is to write the text $entry to the file $launcher

    gksudo --message 'Provide permission for the process. Type your password, or press Cancel.' -- tee "${launcher}"
    How do tee know what to write to $launcher using that way?

    $launcher is in this case "/usr/share/applications/minecraft.desktop", therefore the need for sudo

    Managed to do what I want with starting a shell as sudo, and write from inside there. Thank you for telling me how to check password first Paddy Landau!

    So does this look ok?

    Code:
    createMenyEntry() {
    
      local entry="
    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=Minecraft
    Comment=Minecraft is a game about placing blocks to build anything you can imagine. At night monsters come out, make sure to build a shelter before that happens. It also has music by C418!
    Exec=sh ${starterScript}
    Icon=${icon}
    Path=${minecraftDir}
    Terminal=false
    Categories=Game
    StartupNotify=true";
     
      local pswrd="$(gksudo --print-pass --message '<b>Provide password to add Minecraft to your application-menu.</b>' -- : 2>/dev/null )" 
    
       # Check for null entry or cancellation.
      if [[ ${?} != 0 || -z ${pswrd} ]]
      then
          echo "cancel of no password"
          return 1
      fi
    
       # Check that the password is valid.
      if ! sudo -kSp '' [ 1 ] <<<"${pswrd}" 2>/dev/null
      then
          echo "wrong password"
          return 1
      fi
    
      echo $pswrd | sudo -S sh -c "echo \"${entry}\" | sudo tee $launcher 2>&1>/dev/null"
      return 0
    }
    Last edited by DarkAmbient; September 6th, 2012 at 05:45 PM.
    This is my signature

  2. #12
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,265
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash sudo and zenity --password

    Quote Originally Posted by DarkAmbient View Post
    So does this look ok?
    Well, here are some comments to help you in your learning:

    • You have called your function createMenyEntry. Did you mean createMenuEntry?


    • There is a semi-colon, which you don't need, after the first local statement. (It won't hurt, but it does look a bit odd.)


    • Your two local entries can have the -r option (read-only) to clarify that they should not be changed after being set. This will tighten your code against bugs.


    • You have used echo to display errors — if you are running it in the terminal, why bother with gksudo? sudo would do, unless echo is simply there for debugging.


    • You use tee. Why not use echo?


    • Again, instead of using a pipe, use the Here String. It is cleaner (easier to understand), doesn't start a child process, and the result of the command is available to the script through ${?}.


    • Where you use $launcher, do remember to quote the variable in case the file name should have spaces. Even though it doesn't in this particular case, it's a good habit to get into: "${launcher}" (including the quotation marks).


    • Finally, your last statement is pretty complex:
      Code:
      echo $pswrd | sudo -S sh -c "echo \"${entry}\" | sudo tee $launcher 2>&1>/dev/null"
      Use a Here String instead of the first pipe, use echo instead of tee, and use &>/dev/null instead of 2>&1>/dev/null.

    But…

    This is Linux! Why not find a single command (if possible) to replace your entire function?

    Instead of simplifying your complex code, you can still bypass all that complexity with just a single command. Check this out:
    Code:
    gksudo --message '<b>Provide password to add Minecraft to your application-menu.</b>' -- bash -c "echo '${entry}' >'${launcher}'"
    HTH
    Last edited by Paddy Landau; September 6th, 2012 at 06:58 PM. Reason: Clarifications
    Problems with WINE?
    Full Circle Magazine :: Confused "allot"? :: Cheap Linux stickers
    In my day, we had outdoors in which to run, play, and socialise. Now we have computers to do those.

  3. #13
    Join Date
    Aug 2008
    Location
    Sweden
    Beans
    305
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash sudo and zenity --password

    Sweet, thanks for all the pointers and help!

    Well, first of, the createMenyEntry is a typo as meny is swedish for menu. And as you mentioned, the echo's in my script is mainly for debugging, I would like to keep it non-CLI:ish. But I'll update my script some more tomorrow following the rest of your pointers. I tried adding the one-line gksu-example your provided already, works nicely!

    I'm not to familiar with Here-strings yet tbh.. But now I got something to read about tomorrow!

    I've uploaded what I've done so far in this thread here, but will redo and probably re-upload again tomorrow.

    Now, bedtime!

    Thanks and good night
    This is my signature

  4. #14
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,265
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash sudo and zenity --password

    Good, it seems as though you have it solved. Please mark the thread as solved.

    Here Strings: Use man bash and search for Here Strings (I don't know what it might be called in Swedish), or see the on-line manual.

    To avoid CLI, use zenity instead of echo.

    Regarding zenity: If you are using it just for yourself, I recommend using yad instead (yad's repository). Unfortunately, yad is not in the standard repositories, so you still need zenity when writing for other people.
    Problems with WINE?
    Full Circle Magazine :: Confused "allot"? :: Cheap Linux stickers
    In my day, we had outdoors in which to run, play, and socialise. Now we have computers to do those.

  5. #15
    Join Date
    Aug 2008
    Location
    Sweden
    Beans
    305
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash sudo and zenity --password

    Marked as solved!

    Yea, I read about yad as an alternative somewhere. But I'm writing this installer mainly for people who has trouble with getting Minecraft to run, so it's a no go im afraid.

    Thanks again for all the help Paddy!
    This is my signature

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •