?
Solved

Shell function

Posted on 2015-01-08
6
Medium Priority
?
79 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 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