Sample Header Ad - 728x90

bash script environment/result ambiguity

0 votes
1 answer
192 views
I'm running Arch Linux with i3wm. $ uname -a Linux lappie 5.3.1-arch1-1-ARCH #1 SMP PREEMPT Sat Sep 21 11:33:49 UTC 2019 x86_64 GNU/Linux $ i3 --version i3 version 4.17.1 (2019-08-30) © 2009 Michael Stapelberg and contributors I have this scriptfile function.sh that scans the wifi with wpa_cli. It checks the return of > scan and returns the results if the scan was successful. #!/bin/bash logfile=/$HOME/.logfile echo "function" >> $logfile echo "" >> $logfile interface="wlp2s0" function scan_wifi { scan=$(sudo wpa_cli -i $interface scan) echo "scan: $scan" >> $logfile case "$scan" in "OK") echo "Scanning wifi" >> $logfile scan_res=$(sudo wpa_cli -i $interface scan_results | awk 'NR>1 {OFS="\n>>"; print $NF}') echo "results: $scan_res" >> $logfile echo $scan_res ;; "FAIL") echo "FAILED TO SCAN" >> $logfile echo "FAILED TO SCAN" ;; *) echo "ERROR: empty or undefined event for $interface" >> $logfile exit 1 ;; esac } echo "return: $(scan_wifi)" >> $logfile echo "" >> $logfile echo "" >> $logfile function.sh locates in my $HOME directory. I also have a file /usr/local/bin/networkmenu containing the following: #!/bin/sh bash /home/speklap/function.sh Until now everything is ok. I've added the /user/local/bin/networkmenu to the sudoers file to execute without password. speklap ALL=(ALL) NOPASSWD: /usr/local/bin/networkmenu In the terminal as user: $ networkmenu .logfile: function scan: OK Scanning wifi results: thuis return: thuis But when executing with a shortcut through i3, it doesn't work. .config/i3/config: bindsym $mod+n exec networkmenu Results in a .logfile: function scan: ERROR: empty or undefined event for wlp2s0 return: Why is that, why can't i3 invoke the script like I can in the terminal? What am I doing wrong? EDIT: if someone can come up with a better title, feel free to change. I had no clue on how to google this and my results were poorly and a lot of false positives EDIT: outputting to /tmp/nm.log as per comment of @cas + logfile=//home/speklap/.logfile + echo functions + echo '' + interface=wlp2s0 ++ scan_wifi +++ sudo wpa_cli -i wlp2s0 scan sudo: no tty present and no askpass program specified ++ scan= ++ echo 'scan: ' ++ case "$scan" in ++ echo 'ERROR: empty or undefined event for wlp2s0' ++ exit 1 + echo 'return: ' + echo '' + echo '' So now we know what the problem is, sudo: no tty present and no askpass program specified I can fix this by adding wpa_cli to the sudoers file? Or is there a better solution. As in changing networkmenu to sudo bash /$HOME/function.sh?
Asked by Swedgin (113 rep)
Oct 4, 2019, 08:09 PM
Last activity: Oct 5, 2019, 01:24 PM