Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shell function

Posted on 2015-01-08
6
Medium Priority
?
84 Views
Last Modified: 2015-01-19
Hi,

I have a txt file which has cat and product list
1,Main
2,Dress
3,Electronic

Is this function looks good?
cat_value() #
{
   id="$1"; shift
   value= 'grep $1 '/root/shop/custom/data.txt' | awk -F "," { print $2 }'
   return $value
   exit 0
}

Open in new window


I need a sample shell script where i will pass the id value (variable) and it shows output as below. I dont know how to call the function and get the value .

eg: For id 2 , the value is Dress.

Also advice how it will handle if i pass 2 arguments to the function.

Thanks
0
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:Mike Lloyd
ID: 40539249
I'm not completely following, but awk and sed will be your friend on this one. Can you please clarify a bit more?  What you want to do could be done in just one line with some fancy sed and awk.

This isn't going to work: value= 'grep $1 '/root/shop/custom/data.txt' | awk -F "," { print $2 }'

Use this: value=`grep $1 /root/shop/custom/data.txt | awk -F "," { print $2 }`
0
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 40539275
#!/bin/bash
cat_value() 
{
   while [ $# -gt 0 ]
   do
       grep "^$1," '/root/shop/custom/data.txt' | awk -F "," '{ print $2 }'
       shift
   done
}

result=$(cat_value 2)

# For multiple values
result=$(cat_value 1 2)

Open in new window

0
 
LVL 5

Author Comment

by:magento
ID: 40539476
Tintin,

One question why there is no return value in the function ? That means we dont need to type return and we can call the function directly , am i right ?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 48

Expert Comment

by:Tintin
ID: 40539480
In shell scripts, 'return' is used to return the exit status and not a string.

eg:

function foo {
   if cmd
   then
         return 0
   else
        return 1
   fi
}

foo

echo "Function foo returned a status of $?"

#or check if it was successful, by doing

if foo
then
     echo "Function foo was successful"
else
    echo "Function foo failed"
fi

Open in new window

0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40541291
If you want the function to return both a string and a status, just echo the return value that you want and set the status, like this:
cat_value() #
{
   id="$1"; shift
   value =$(grep ${id} /root/shop/custom/data.txt | awk -F "," '{ print $2 }')
   echo ${value}
   return 0
}

Open in new window


If that grep did not result in anything, you could check for that and return a '1' status:
cat_value() #
{
   id="$1"; shift
   value = $(grep ${id} /root/shop/custom/data.txt | awk -F "," '{ print $2 }')
   if [ "${value}" = "" ]
   then
      return 1
   else
      echo ${value}
      return 0
   fi
}

Open in new window


Call your function like this:

cat_value somestring

where somestring is a string to find in your data.txt
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40543489
cat_value() #
{
   id="$1"; shift
   value = $(grep ${id} /root/shop/custom/data.txt | awk -F "," '{ print $2 }')
   if [ "${value}" = "" ]
   then
      return 1
   else
      echo ${value}
      return 0
   fi
}

Open in new window

is really just a long hand way of writing (assuming you are using bash)

cat_value() #
{
   grep "$1" /root/shop/custom/data.txt | awk -F "," '{ print $2 }' 
   return ${PIPESTATUS[0]}
}

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question