I am currently writing a bash (version 3.00.16(1)) script on a Solaris 10 system and am having a bit of a problem with a return value from a function. My function is to return the numeric permissions on a file (basic stuff, I'm not worried about sticky bits or anything of that nature). I would use the 'stat' command to pull this information back, but it doesn't exist on Solaris 10, and I don't want to have to add any packages for this to work as the script will be run on our servers, and potentially others out in the field. Here is my script (named test.sh because I wanted to figure it all out before shoving this bit of code into a much larger script, though I am considering making it a separate program for use in other programs).
Code:
#!/bin/bash
function get_number()
{
temp_num=0
[ "${1:0:1}" != "-" ] && temp_num=$(( ${temp_num} + 4 ))
[ "${1:1:1}" != "-" ] && temp_num=$(( ${temp_num} + 2 ))
[ "${1:2:1}" != "-" ] && temp_num=$(( ${temp_num} + 1 ))
return ${temp_num}
} # End get_number()
function get_permissions()
{
get_number ${1:1:3}
user_n=$?
get_number ${1:4:3}
group_n=$?
get_number ${1:7:3}
world_n=$?
echo User=${user_n} \| Group=${group_n} \| World=${world_n}
#rtn_val=${user_n}${group_n}${world_n}
rtn_val=$(( (${user_n}*100) + (${group_n}*10) + ${world_n} ))
echo RTN_VAL=${rtn_val}
return ${rtn_val}
} # End get_permissions
echo -n 'Enter a file name: '
read file_name
get_permissions $(ls -ld ${file_name} | awk '{print $1}')
per_n=$?
echo ${file_name}: ${per_n}
I have two files in this directory that I have tried this script against. Here are the files, along with the output from the code. Please note that I get the same output regardless of with "rtn_val=" line I use.
Code:
$ ls -l
total 4
-rw-r--r-- 1 root root 12 Jul 23 10:08 abc.txt
-rwx------ 1 root root 770 Jul 23 10:10 test.sh
$ ./test.sh
Enter a file name: abc.txt
User=6 | Group=4 | World=4
RTN_VAL=644
abc.txt: 132
$ ./test.sh
Enter a file name: test.sh
User=7 | Group=0 | World=0
RTN_VAL=700
test.sh: 188
As you can see, the get_number function is working just fine, and returning what it should. But when the get_permissions function returns the ${rtn_val} value to the main part of the script, I get a completely different number. Has anyone seen this before, or have an idea as to what is going on?