Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions in Shell Scripting 2

Status
Not open for further replies.

skuthe

Programmer
Sep 10, 2002
33
US
Hi,
How do I write functions in a unix shell script and pass parameters to them ?

Kindly answer with examples.

Thanks in advance.
 
Here is an example, hope you get the idea.

function _name_() {
num=$1
namedobj=${2:-"slim shady"}
x=0
while test $x -lt $num
do
x=`expr $x + 1`
cut -c1-$x < <(echo $namedobj)
done

return $?
}
OP:
> _name_ 4
s
sl
sli
slim
 
Here are 4 other examples.

Regards,

Ed

1.
# yesorno: This function takes one argument and if the
# value is Y or y returns 1 else it returns 0. Pressing
# <CR> returns 0;
# usage: $(yesorno $answer)
function yesorno {
case $1 in
y|Y) echo 1;;
*)
echo 0;;
esac
} # end yesorno

printf &quot;Answer the question: Y/N: &quot;
read answer
ret_val=$(yesorno $answer)
echo $ret_val

2.
# user_exist: This function takes an argument which is the
# user id, greps the first field of /etc/passwd and sets
# the exit code, 1 if user exists and 0 if not.
# sample the exit code $? from calling program.
function user_exist
{
if grep &quot;^$1:&quot; /etc/passwd > /dev/null 2>&1
then return 1
else return 0
fi
} # end user_exist

Sample the exist code immediately after returning from the user_exist
function:

loginame=alexb
user_exist $loginame
if [ &quot;$?&quot; -eq 0 ]
then printf &quot;User %s does NOT exist\n&quot; $loginame
else printf &quot;User %s exists\n&quot; $loginame
fi

3.

# down_shift: This function takes one argument, downshifts it
# to lowercase, and returns the string
# usage: string=$(down_shift $string)
function down_shift {
local string

string=`echo $1 | tr '[A-Z]' '[a-z]'`
# alternate tr command
#string=`echo $1 | tr &quot;[:upper:]&quot; &quot;[:lower:]&quot;`
echo $string
} # end down_shift


string=&quot;ALLLOWERCASE&quot;
string=$(down_shift $string)
echo $string

4.
# is_digit: This function matches an all digits regular expression
# against an argument and returns 1 if the argument is digits else 0
# if isn't.
# Note the use of nawk. Use gawk for the Bash shell.
function is_digit
{
echo $1|nawk ' { if ($0 ~ /^[0-9]+$/)
print 1
else
print 0 } '
} # end is_digit

num=&quot;345&quot;
ret_val=$(is_digit $num)
echo $ret_val
 
my exemple, shell-funcs have the same rules as the shell itself: the first 8 argv are remembered in $1 $2 .. $8
$9 remember all other'

#!/bin/sh
printme()
{
while [ $# -ne 0 ]
do echo $1; shift
done
}

printme aa bb cc dd ee ff gg ........ zz vox clamantis in deserto.
 
Marsd:

Thanks for the kind words. Just for that, here's another example.
This function is a wrapper around tar being used to copy a source
directory to a destination. You need to be careful about passing
strings to a ksh function. This function's argument is a
string with two directories. I use set to break $1 into two
arguments, and I make sure the directories exist before the copy
takes place.

Regards,

Ed

# copy $1 (src) to $2 (destination)
function cp_directory {
set $1 # break $1 argument

if [ ! -d $1 ]
then
return 1 # source doesn't exist
fi
if [ ! -d $2 ]
then
return 1 # destination doesn't exist
fi

cd $1; tar -cf - .|(cd $2; tar -xf - ); return $?
}

cp_directory &quot;/usr/eds/olddir /jet/eds/newdir&quot;
if [ $? -eq 0 ]
then
echo &quot;copy complete&quot;
else
echo &quot;copy failure&quot;
fi
 
olded: dangerous
suppose: cp_directory /usr/eds/olddir /usr/eds/olddir.

you don't need to split $1 if you pass args NOT quoted.
my i propose: tar cfb - 126k . |(cd $2;tar xfp -)

in a previous example:
if [ &quot;$?&quot; -eq 0 ] #numerical comp of a string??

why disturb (n)?awk to do this ????
function is_digit
{
case x$1 in x[0-9]*[0-9]) return 1;; esac
return 0
} # end is_digit
vox clamantis in deserto.
 
If I can't use awk, can I use expr?

# return 1 if argument is numeric else 0
function is_digit {

if [ $# -ne 1 ]
then
echo 0
fi
expr &quot;$1&quot; + 1 > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo 1
else
echo 0
fi

}

num=&quot;345&quot;
ret_val=$(is_digit $num)
echo $ret_val
 
Thank you guys for all your valuable suggestions.
I appreciate it.

I was on vacation and therefore it took long for me to acknowledge your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top