간단한 bash 스크립트를 위한 간단한 GUI 작성

간단한 bash 스크립트를 위한 간단한 GUI 작성

"http"로 시작하는 URL 주소 목록과 관련된 일부 시스템 변경을 수행하는 bash 스크립트가 있으며 이에 대한 GUI를 작성하려고 합니다.

나는 이것의 마지막 부분에 붙어 있습니다 :


changes="$(cat /home/$USER/.updates.log | grep http)"
if [ "$changes" != 0 ]; then
    zenity --question --text "Changes found in:\n$changes\n\nWould you like to update now?"
        if [ $? = 0 ]
        then
# password
sudo_password="$(gksudo --print-pass --description 'MyScript' -- : 2>/dev/null)"
# check for null entry or cancellation
if [[ ${?} != 0 || -z ${sudo_password} ]]
then
    exit 4
fi
if ! sudo -kSp '' [ 1 ] <<<"${sudo_password}" 2>/dev/null
then
    exit 4
fi
# notify
notify-send "Applying updates..." -i gtk-dialog-info -t 1000 -u normal &
# proceed to update
cuser="${SUDO_USER:-$USER}"
sudo -Sp ''  sudo /usr/local/bin/MyScript <<<"${sudo_password}"
# option to view log
new_update="$(cat /home/$USER/.updates.log | grep 'MyScript completed at ' | awk -F ' at ' '{print $2}')"
zenity --question --text "MyScript updated at $new_update\n\nWould you like to view the log file now?"
if [ $? = 0 ]
then
# display log
    zenity --text-info --filename=/home/$USER/.updates.log --width 680 --height 680
fi
fi
fi

사실, 나에게 까다로운 부분은 다음과 같습니다.

if [ "$changes" != 0 ]; then

파일에 "http"로 시작하는 줄이 포함되어 있지 않으면 "업데이트를 찾을 수 없습니다. 종료 중..."과 같은 메시지를 표시하고 싶지만 이는 zenity 문제 대화 상자에 빈 줄을 생성할 뿐입니다. 이 줄을 수정하고 "else" 아래에 다른 명령을 추가해야 할 것 같지만 어떻게, 어디에 있는지 모르겠습니다...

답변1

필요한 데이터가 없거나 그것이 무엇을 하는지조차 모르기 때문에 스크립트의 나머지 부분을 테스트할 수는 없지만 다음 줄은 확실히 잘못된 것입니다.

changes="$(cat /home/$USER/.updates.log | grep http)"

이것은 저장됩니다산출grep$changes문자열을 찾은 횟수가 아니라 반환된 실제 줄 수입니다 . 예를 들어:

$ cat file 
one http
two http
three http
$ changes=$(cat file | grep http)
$ echo "$changes" 
one http two http three http

위에서 볼 수 있듯이 $changes파일에서 일치하는 각 줄은 단순히 변수로 연결됩니다. 원하는 것은 다음과 같습니다( cat그런데 grep파일 이름을 입력으로 사용할 필요가 없습니다).

$ changes=$(grep -c http file)
$ echo $changes 
3

이 스위치를 사용하면 줄 자체가 아닌 일치하는 줄의 개수가 인쇄 -c됩니다 . grep또는 출력을 전달하여 wc -l행 수를 계산할 수 있습니다.

changes=$(grep http file | wc -l)

어느 쪽이든 괜찮습니다. 이제 0보다 큰지 확인할 수 있습니다 $changes.

if [ "$changes" -gt 0 ]]; then 
        ...
fi

변경 사항을 표시하려면 원래 방법을 사용하되 0과 비교하지 마세요. 대신 -z변수가 비어 있는지 확인하는 방법을 사용하세요.

changes=$(grep http /home/$USER/.updates.log)
## If $changes is empty
if [ -z "$changes" ]
then
     notify-send "Found no updates; exiting..." -i gtk-dialog-info -t 1000 -u normal &
     exit
else
     zenity --question --text "Changes found in:\n$changes\n\nWould you like to update now?"
    ...
fi

답변2

질문에 대해어디나머지 조건을 배치할 때 팁은 항상 들여쓰기를 준수하는 것입니다. 그러면 작업이 확실히 더 쉬워집니다 :)

아직 테스트하지는 않았지만(틀렸을 수도 있음) 시작하려면 다음과 같이 시도해 보겠습니다.

changes="$(cat /home/$USER/.updates.log | grep http)"
if [ "$changes" != 0 ]; then
  zenity --question --text "Changes found in:\n<i>$changes</i>\n\nWould you like to update now?"
  if [ $? = 0 ]; then
    ## password
    sudo_password="$(gksudo --print-pass --description 'MyScript' -- : 2>/dev/null)"
    ## check for null entry or cancellation
    if [[ ${?} != 0 || -z ${sudo_password} ]]; then
      exit 4
    fi
    if ! sudo -kSp '' [ 1 ] <<<"${sudo_password}" 2>/dev/null; then
      exit 4
    fi
    ## notify
    notify-send "Applying updates..." -i gtk-dialog-info -t 1000 -u normal &
    ## proceed to update
    cuser="${SUDO_USER:-$USER}"
    sudo -Sp ''  sudo /usr/local/bin/MyScript <<<"${sudo_password}"
    ## option to view log
    new_update="$(cat /home/$USER/.updates.log | grep 'MyScript completed at ' | awk -F ' at ' '{print $2}')"
    zenity --question --text "MyScript updated at <b><i>$new_update</i></b>\n  \nWould you like to view the log file now?"
    if [ $? = 0 ]; then
      ## display log
      zenity --text-info --filename=/home/$USER/.updates.log --width 680 --height 680
    fi
  fi
## Here is where you can choose what to do if there are no results
else
  zenity --text-info "No updates found; exiting..." 
fi

관련 정보