Solved

Shell function

Posted on 2015-01-08
6
58 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
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify 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 37

Expert Comment

by:Gerwin Jansen
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now