Announcement

Collapse
No announcement yet.

In need of some help

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    In need of some help

    I need a little help im writing a bash script which does the following.

    Use kdialogue to grab username. Set to variable $username
    Use kdialogue to grab userpass. Set to variable $userpass
    Create a new user.
    Set user password.
    Add new user to jail ssh user group.
    Add user to chroot jail via jailkit.

    The problem is sudo passwd $username uses an interactive terminal prompt to request the new password and as im using gui elements the terminal window is hidden.

    How do i get the password used in variable $userpass to the interactive utility?
    Tutorials:
    Yoda's ownCloud Installation on Kubuntu 20.04

    #2
    Try pkexec instead of sudo.

    Comment


      #3
      Some starting points for kdialog that may make the task clearer or easier:

      https://develop.kde.org/deploy/kdialog/

      and a ton of password specific examples here
      https://mostlylinux.wordpress.com/ba...alog/#password

      Comment


        #4
        Ive got that run as root problem sorted but im left with another headache.

        Ive written a script that acts as a menu but when you select an option it just closes the terminal without executing the option see below for code.

        The-menu-Script

        Code:
        # !/bin/bash
        # this is the menu script
        source /opt/project-folder/cp.conf
        if [ $Installed == 'True'  ]
        then
        opt=$(kdialog --radiolist "What Would You Like To DO?:" 1 "option 1" off  2  "option 2" off 3 "Exit" off;);
        if [ "$?" = 0 ]; then
        	if [ "$opt" = 1 ]; then
        			sudo /bin/bash /opt/project-folder/Scripts/1.sh
            elif [ "$variable" = 2 ]; then
        			sudo /bin/bash /opt/project-folder/Scripts/2.sh
        	elif [ "$opt" = 3 ]; then
        		exit;
        	else
        		kdialog --error "ERROR";
        	fi;
        elif [ "$?" = 1 ]; then
        	kdialog --sorry "YOU CHOSE CANCEL";
        else
        	kdialog --error "ERROR";
        fi;
        else
        echo "you dident do any work"
        fi;
        1.sh

        Code:
        # !/bin/bash
        kdialog --msgbox "option 1"
        2.sh
        Code:
        # !/bin/bash
        kdialog --msgbox "option 2"
        I cant seem to find the problem, it is worth mentioning that the menu script runs with the terminal window hidden.

        Any ideas what could be causing the problem?
        Tutorials:
        Yoda's ownCloud Installation on Kubuntu 20.04

        Comment


          #5
          As posted, the shebang is malformed, which probably results in dash running the script. There should be no space between the # and the !. At least at some time in the past, the shebang had to be the first two characters of the file, which caused trouble with editors that would write a BOM to start the file.

          And, what's $variable? Shouldn't that be $opt?

          $? does not persist very long. If you use the ancient, IMO kludgy, test syntax using a single "[", in principle the "[" is a programme whose running sets $? (it's that which the if checks). IMO it's less error prone to use the [[ ]] syntax.

          But, running any gui programme as root is a bad idea. Perhaps you're just running kdialog for demonstration purposes, but there can be bad consequences you (nor I) have any idea about. Running with sudo gets a different environment, and gui programmes read and write files in complicated ways. For example, a font cache might be updated, or written afresh.
          Last edited by jlittle; Sep 16, 2021, 05:49 PM.
          Regards, John Little

          Comment


            #6
            thanks for pointing out my typo, after updating all the scripts.

            the menu runs
            i make a selection
            and the second script dosent run and my menu script ends

            im still a little fuzzy on why my second script fails to run
            Tutorials:
            Yoda's ownCloud Installation on Kubuntu 20.04

            Comment


              #7
              A few pointers:
              • scripts are simpler if they bail out on error conditions
              • the case ... esac construct is for choices like that
              • sudo -A might be what you were originally looking for
              • semicolons are not needed before the end of a line

              I'm not sure where you're going with this, and if I'd go that way, but a quick restructure:
              Code:
              #!/bin/bash
              # this is the menu script
              source ~/cp.conf
              if [[ $Installed != True  ]]; then
                  exit
              fi
              opt=$(kdialog --radiolist "What Would You Like To DO?:" \
                                               1 "option 1" off       \
                                               2 "option 2" off       \
                                               3 "Exit"     off         )
              exit_code=$?
              case "$exit_code" in
                  (0) ;; # ok
                  (1) kdialog --sorry "you chose cancel"
                      exit ;;
                  (*) kdialog --error "error"
                      exit ;;
              esac
              
              case "$opt" in
                  (1) /bin/bash ~/1.sh ;;
                  (2) /bin/bash ~/2.sh ;;
                  (*) exit ;;
              esac
              To use sudo -A, write a kdialog script, say /usr/local/bin/sudopass.sh
              Code:
              #!/bin/bash
              kdialog --password 'sudo wants a password'
              and use
              Code:
              export SUDO_ASKPASS=/usr/local/bin/sudopass.sh
              sudo -A [i]command[/i]
              Regards, John Little

              Comment


                #8
                Thanks John, i will give your solution a try, why is there an exit in the following.
                Code:
                if [[ $Installed != True  ]]; then
                    exit
                Wouldn't that just exit the script before the menu runs?
                Tutorials:
                Yoda's ownCloud Installation on Kubuntu 20.04

                Comment


                  #9
                  I changed "==" to "!=", so the script exits if Installed is not True
                  Regards, John Little

                  Comment


                    #10
                    thanks for explaing that to me
                    Tutorials:
                    Yoda's ownCloud Installation on Kubuntu 20.04

                    Comment

                    Working...
                    X