I am re-writing code to work with a newer system. I did not write the original code, but I'm trying to use the old code as a template. When I run the command by itself, it works fine. When I try running it as part of a script or function in the shell, it does not.
#!/bin/bash
function find_user() {
local txt
local user_list
local username
local displayName
echo "Find User"
echo "---------"
echo -n "Enter search text (e.g: lib): "
read txt
if [ -z "$txt" ] || ! validate_fullname "$txt"; then
echo "Search cancelled."
else
echo
user_list="$(samba-tool user list | grep -i ${txt} | sort)"
(
echo "Username Full_Name"
echo "-------- ---------"
# Dev Note: Get the username, then displayName parameter, replacing
# the spaces in displayName with an underscore. Do not need to look
# for a dollar sign anymore, as computers are not listed as "user"
# class objects in AD.
for a in "${user_list[@]}"; do
username=$a
displayName="$(
samba-tool user show ${a} | grep 'displayName:' | \
awk -F: '{gsub(/^[ \t]+/,"""",$2); gsub(/ ./,""_"",$3); print $2}' | \
tr ' ' '_')"
echo "${username} ${displayName}"
done
)| column -t
fi
}
When I try to run it, and enter the find_user function, it prompts for the search text (i.e. I can type js) and press Enter.
My issue is with the $displayName= part. It seems to generate an empty string when running in the script. If I run the command manually from the terminal (filling in, for example, jsmith as a substitute for ${a}), it outputs the full name correctly.
My system is running bash.
What is going on, and how can I fix this?
#!…), and to show how you callfind_user.find_userto start it$awithjsmithin the script, then the script would have worked, too.