I am trying to make a script that will set variables based on a service. The script is small and simple. Logically it should work, but I am lost on what I am doing wrong. The command would be: setsebool -P Boolean # to set the boolean.
#!/bin/bash
read -p "Which service do you wish to see?" serv
IFS=$'\r\n' boolser=($(getsebool -a | grep $serv | cut -d- -f1))
for (( f=0; f <=`echo "${#boolser[@]}"`; f++ ));do
if [ ${boolser[$f]} > /dev/null ];then
boolbleed="${boolser[$f]}"
while true; do
read -p "Set $boolbleed (0/1)" yn
case $yn in
0) setsebool -P $boolbleed 0; break;;
1) setsebool -P $boolbleed 1; break;;
*) echo "$yn is not a 0 or a 1";;
esac
done
else
unset ${boolser[$f]}
fi
done
These are the results of running the script:
[root@testing Downloads]# ./sebool-vsftpd-remove
Which service do you wish to see?ftp
Set allow_ftpd_anon_write (0/1)0
Boolean allow_ftpd_anon_write is not defined
Set allow_ftpd_full_access (0/1)1
Boolean allow_ftpd_full_access is not defined
After running the script this is what is left over:
[root@testing Downloads]# getsebool -a | grep ftp
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> on
ftpd_connect_db --> on
ftpd_use_fusefs --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> on
tftp_anon_write --> on
tftp_use_cifs --> off
tftp_use_nfs --> off
Does anyone know why the boolean is not defined? When entering the code manually setsebool -P allow_ftpd_anon_write 1 it is successful.
You have a space on the end of your variable, and you've set IFS so that it's not being removed as part of word splitting. Here's a simplified test case that exibits your problem:
IFS=$'\r\n'
value=$(echo "hello - world" | cut -d - -f 1)
[ $value = hello ] && echo "works" || echo "fails"
The simplest solution is to cut your variables before the first space, rather than after the space but before the dash:
boolser=($(getsebool -a | grep $serv | cut -d ' ' -f1))