Solved

Shell function

Posted on 2015-01-08
6
70 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

697 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