LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   While loop not behaving ....... (https://www.linuxquestions.org/questions/programming-9/while-loop-not-behaving-899476/)

jv2112 08-25-2011 07:02 PM

While loop not behaving .......
 
I am trying to create menu based assistant for pacman (Arch Package Manager). The problem I am having is when a command is selected it starts but never finishes. It either returns back to the command prompt or loops back to the first section and sometimes it will loop through questions generated by the pacman install sequences. I have stared at this script and can't figure it out. :scratch: :newbie:

Any direction would be appreciated. :hattip:

Thank You

Code:

  1 #! /bin/bash
  2
  3 x=0
  4
  5 while [ $x = 0 ]
  6 do
  7        clear
  8        figlet "Pacman Assistant"
  9
 10        echo "Please enter Sudo/Root Password"
 11        read -s priv
 12        clear
 13
 14 echo "What pacman action do you want to take ?"
 15
 16 echo "  "1-Syu:Sync/Upgrade"
 17        "2-Syy:Force-Update"
 18        "3-Sync/Upgrade:AUR"
 19        "4-Install/Update Packages""
 20 read ans
 21
 22 # System Upgrade
 23
 24 case "$ans" in
 25                1)
 26                echo $priv | sudo -S pacman -Syu
 27                sleep 5
 28                x=1
 29                continue
 30                ;;
 31                2)
 32                echo $priv | sudo -S pacman -Syy
 33                x=1
 34                continue
 35                ;;
 36                3)
 37                echo $priv |sudo -S clyde -Syua
 38                x=1
 39                continue
 40                ;;
 41                4)
 42                echo  "What package do you want to work with ?"
 43                read pac
 44                clear
 45                echo "What do you want to do with $pac ?"
 46
 47 echo "          1-Install
 48                2-Remove
 49                3-Search"
 50
 51                read pac2
 52                ;;
 53                *)
 54                clear
 55                figlet Not an option.
 56                sleep 5
 57
 58        esac
 59
 60
 61 # Individual Package Management
 62
 63 case "$pac2" in
 64                1)
 65                echo $priv | sudo -S pacman -S $pac
 66                sleep 5
 67                continue
 68                ;;
 69
 70                2)
 71                echo $priv | sudo -S pacman -R $pac
 72                continue
 73                ;;
 74
 75                3)
 76                echo $priv | sudo -S pacman -Ss $pac
 77                continue
 78                ;;
 79
 80 esac
 81
 82 # Pacman helpfull options -->
 83
 84 #-Syu --> Synchronize with repositories before upgrading packages that are out of date on the local system.
 85 #-S  --> Install specific package(s) from the repositories
 86 #-U      Install specific package not from the repositories but from a file
 87 #-R      Remove the specified package(s), retaining its configuration(s) and required dependencies
 88 #-Rns    Remove the specified package(s), its configuration(s) and unneeded dependencies
 89 #-Si      Display information about a given package in the repositories
 90 #-Ss      Search for package(s) in the repositories
 91 #-Qi      Display information about a given package in the local database
 92 #-Qs      Search for package(s) in the local database
 93 #-Syy    Force refresh of all package lists after updating /etc/pacman.d/mirrorlist
 94
 95 #-Sy && sudo abs      Update and refresh the local package and ABS databases against repositories
 96 #-S --asdeps          Install given package(s) as depende
 97 done
~                                                                                                                                                                                     
~                                                                                                                                                                                     
~                                                                                                                                                                                     
~                                                                                                                                                                                     
~                                                                                                                                                                                     
~                                                                                                                                                                                     
~                                                                                                                                                                                     
                                                                                                                                                                      53,3-17      Bot


andrewthomas 08-25-2011 10:27 PM

Quote:

Originally Posted by jv2112 (Post 4453570)
Code:

24 case "$ans" in
 25                1)
 26                echo $priv | sudo -S pacman -Syu
 27                sleep 5
 28                x=1
 29                continue
 30                ;;
 31


The problem with this is that if you have used sudo within the time frame that it caches the password, it will not prompt for a password.

A better solution would be:

Code:

                    1)
                    sudo -k
                    echo $priv | sudo -v -S
                    sudo pacman -Syu
                    sleep 5
                    x=1
                    continue
                    ;;

Furthermore, sudo is not going to work if you enter the root password from a user account.

jv2112 08-26-2011 05:14 AM

Awesome. Thanks for the help. I made some tweaks based on your advice andrewthomas. :hattip:

Now when I get to the point of listing a package searched for ( lines 95 - 100 ) I try and send to more ( tried less to) and the input does not work ( arrow keys ) any ideas ? :study:

Any and all input welcome :scratch:

Thanks again.

Code:

  1 #! /bin/bash
  2
  3 x=0
  4
  5 while [ $x = 0 ]
  6 do
  7        clear
  8        figlet "Pacman Assistant"
  9        echo -e "\e[00;44m****************************************\e[00m"
 10
 11 echo -e "\e[108;44mWhat pacman action do you want to take ?\e[00m"
 12
 13 echo -e  "\e[100;37m
 14 "1-Syu:Sync/Upgrade"
 15
 16 "2-Syy:Force-Update"
 17
 18 "3-Sync/Upgrade:AUR"
 19
 20 "4-Install/Update Packages"
 21
 22 "5-Quit Pacman Assistant"
 23
 24 \e[00m"
 25 read ans
 26
 27 # System Upgrade
 28
 29 case "$ans" in
 30                1)
 31                sudo -kvS # -kvS ---> Clears cache to ensure process.
 32                sudo pacman -Syu
 33                sleep 5
 34                continue
 35                ;;
 36                2)
 37                sudo -kvS
 38                sudo pacman -Syy
 39                sleep 5
 40                continue
 41                ;;
 42                3)
 43                sudo -kvS
 44                sudo clyde -Syua
 45                sleep 5
 46                continue
 47                ;;
 48                4)
 49                echo  "What package do you want to work with ?"
 50                read pac
 51                clear
 52                echo "What do you want to do with $pac ?"
 53
 54 echo "          1-Install
 55                2-Remove
 56                3-Search"
 57
 58                read pac2
 59                ;;
 60
 61
 62                5)
 63                clear
 64                figlet Exiting ....
 65                sleep 5
 66                exit
 67                x=1
 68                ;;
 69
 70                *)
 71                clear
 72                figlet Not an option $USER .
 73                sleep 5
 74                continue
 75 esac
 76
 77
 78 # Individual Package Management
 79
 80 case "$pac2" in
 81                1)
 82                sudo -kvS
 83                sudo clyde -S $pac
 84                sleep 5
 85                continue
 86                ;;
 87
 88                2)
 89                sudo -kvS
 90                sudo pacman -R $pac
 91                sleep 5
 92                continue
 93                ;;
 94
 95                3)
 96                sudo -kvS
 97                sudo clyde -Ss $pac
 98                more
 99                sleep 25
100                ;;
101
102 esac
103
104 # Post color listing
105 # for i in {0..110};do echo -e "\e[00;${i}m${i}";done
106
107 # Pacman helpfull options -->
108
109 #-Syu --> Synchronize with repositories before upgrading packages that are out of date on the local system.
110 #-S  --> Install specific package(s) from the repositories
111 #-U      Install specific package not from the repositories but from a file
112 #-R      Remove the specified package(s), retaining its configuration(s) and required dependencies
113 #-Rns    Remove the specified package(s), its configuration(s) and unneeded dependencies
114 #-Si      Display information about a given package in the repositories
115 #-Ss      Search for package(s) in the repositories
116 #-Qi      Display information about a given package in the local database
117 #-Qs      Search for package(s) in the local database
118 #-Syy    Force refresh of all package lists after updating /etc/pacman.d/mirrorlist
119
120 #-Sy && sudo abs      Update and refresh the local package and ABS databases against repositories
121 #-S --asdeps          Install given package(s) as depende
122 done


grail 08-26-2011 06:14 AM

more what? Remember that a script is just a culmination of many things you would execute on the command line. So execute 96 - 98 manually and see where your issue lies.

jv2112 08-26-2011 07:01 PM

:doh: --> Needed to quote my variables..:newbie: ........... All set.

Thank You everyone for the help !! :hattip:



Code:

  1 #! /bin/bash
  2
  3 ep ()
  4 {
  5
  6 espeak -s 160 -v en-uk -p 30 --stdout | aplay &> /dev/null
  7
  8 }
  9
 10
 11 x=0
 12
 13 while [ $x = 0 ]
 14 do
 15        clear
 16        figlet "Pacman Assistant"
 17        echo -e "\e[00;44m**********************************************\e[00m"
 18
 19 echo -e "\e[108;44m What pacman action do you want to take "$USER" ?\e[00m"
 20
 21        echo -e "\e[00;44m**********************************************\e[00m"
 22
 23
 24 echo -e  "\e[100;37m
 25 "1-Syu:Sync/Upgrade."
 26
 27 "2-Syy:Force-Update."
 28
 29 "3-Sync/Upgrade:AUR."
 30
 31 "4-Work with a package."
 32
 33 "5-Quit Pacman Assistant."
 34
 35 \e[00m"
 36
 37 echo  "What pac man action do you want to take "$USER" " | ep
 38 read ans
 39
 40 # System Upgrade
 41
 42 case "$ans" in
 43                1)
 44                sudo -kvS # -kvS ---> Clears cache to ensure process.
 45                sudo pacman -Syu
 46                sleep 5
 47                continue
 48                ;;
 49                2)
 50                sudo -kvS
 51                sudo pacman -Syy
 52                sleep 5
 53                continue
 54                ;;
 55                3)
 56                sudo -kvS
 57                sudo clyde -Syua
 58                sleep 5
 59                continue
 60                ;;
 61                4)
 62                echo -e "\e[108;44mWhat package do you want to work with ?\e[00m"
 63                read pac
 64                clear
 65                echo -e  "\e[108;44mWhat do you want to do with "$pac" ?\e[00m"
 66
 67 echo -e "\e[100;37m
 68 1-Install
 69 2-Remove
 70 3-Search\e[00m \e[108;44m( Uses less to page. Press (q) to quit)\e[00m"
 71
 72                read pac2
 73                ;;
 74
 75
 76                5)
 77                clear
 78                figlet Exiting ....
 79                sleep 5
 80                exit
 81                x=1
 82                ;;
 83
 84                *)
 85                clear
 86                figlet Not an option i"$USER" .
 87                sleep 5
 88                continue
 89 esac
 90
 91
 92 # Individual Package Management
 93
 94 case "$pac2" in
 95                1)
 96                sudo -kvS
 97                sudo clyde -S "$pac"
 98                sleep 5
 99                continue
100                ;;
101
102                2)
103                sudo -kvS
104                sudo pacman -R "$pac"
105                sleep 5
106                continue
107                ;;
108
109                3)
110                sudo -kvS
111                sudo clyde -Ss "$pac" | less
112                ;;
113
114 esac
115
116 # Post color listing
117 # for i in {0..110};do echo -e "\e[00;${i}m${i}";done
118
119 # Pacman helpfull options -->
120
121 #-Syu --> Synchronize with repositories before upgrading packages that are out of date on the local system.
122 #-S  --> Install specific package(s) from the repositories
123 #-U      Install specific package not from the repositories but from a file
124 #-R      Remove the specified package(s), retaining its configuration(s) and required dependencies
125 #-Rns    Remove the specified package(s), its configuration(s) and unneeded dependencies
126 #-Si      Display information about a given package in the repositories
127 #-Ss      Search for package(s) in the repositories
128 #-Qi      Display information about a given package in the local database
129 #-Qs      Search for package(s) in the local database
130 #-Syy    Force refresh of all package lists after updating /etc/pacman.d/mirrorlist
131
132 #-Sy && sudo abs      Update and refresh the local package and ABS databases against repositories
133 #-S --asdeps          Install given package(s) as depende
134 done



All times are GMT -5. The time now is 08:18 AM.