LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2009, 09:28 AM   #1
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
bash - problem with a return value


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?
 
Old 07-23-2009, 10:45 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Return code is only 8 bit or up to 255 I think. Better return the value instead to another variable like RETURN or __.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - ssh login and return babag Programming 8 06-03-2008 04:53 PM
bash - return home path babag Programming 2 06-01-2008 04:59 PM
Bash script: return code ftp TalkingMarble Linux - Newbie 3 05-09-2008 09:37 AM
Bash function return rejeep Programming 15 08-24-2007 04:21 AM
Bash return question merchtemeagle Programming 2 11-02-2005 10:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration