Solved

Shell function

Posted on 2015-01-08
6
73 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 500 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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é.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

724 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