Getting category when given the variable from external file to shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting category when given the variable from external file to shell script
# 8  
Old 01-25-2011
You must have ksh88 for it not to support : substring syntax.

here are the hacks you can do to get something like a substring in ksh88.

---------- Post updated at 02:22 PM ---------- Previous update was at 02:16 PM ----------

using the substring function from that thread:
Code:
#!/bin/ksh

TOFIND="file4.txt"
CATEGORY=""

function substring
{
    typeset string="$1" out
    typeset -i offset=$2 length=$3

    while (( offset > 0 ))
    do
        string="${string#?}"
        (( offset = offset - 1 ))
    done

    while (( length > 0 ))
    do
        out="$out${string%${string#?}}"
        string="${string#?}"
        (( length = length - 1 ))
    done

    print "$out"
}

while read LINE
do
        [ -z "$LINE" ] && continue

        if [ $(substring "${LINE}" 0 1) == "[" ]
        then
                CATEGORY="$(substring "$LINE" 1 $((${#LINE}-2)))"
                continue
        fi

        if [ "$LINE" == "$TOFIND" ]
        then
            echo "category is $CATEGORY"
            break
        fi
done < conf.file

Warning, this may be slow.

I now have pdksh installed to test compatibility with truly obnoxiously old shells... I really find it hard to imagine a shell that doesn't have the : syntax. did they just use the cut external all the time or what?
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 01-25-2011
Fantastic, thank you very much for your help.
# 10  
Old 01-25-2011
An alternative with regular variable expansion:
Code:
case $LINE in
  \[*)
      CATEGORY=${LINE#\[Category}
      CATEGORY=${CATEGORY%]} ;;
  $TOFIND)
      echo "category is $CATEGORY"
      break          
esac


Last edited by Scrutinizer; 01-26-2011 at 07:14 PM..
# 11  
Old 01-26-2011
One last question relevent to this post:

If the entry is NOT found, how can I echo "not found"?

This seems like it should be profoundly simple, yet I've been trying to get it to work and I'm stumped. Any ideas?

---------- Post updated 01-26-11 at 08:52 AM ---------- Previous update was 01-25-11 at 04:47 PM ----------

Any ideas?

I've been trying to determine EOF but can't find a way.

Should I just do a grep before looping and skip the loop if it's not present?

If the entry is there as a comment however, it would cause problems....

Smilie
# 12  
Old 01-26-2011
You could use a boolean:
Code:
TOFIND="file4.txt"
CATEGORY=
FOUND=false
while read line
do
  case $line in
    \[*)
        CATEGORY=${line#\[Category}
        CATEGORY=${CATEGORY%]} ;;
    $TOFIND)
        echo "category is $CATEGORY"
        FOUND=true
        break          
  esac
done < conf.file
if ! $FOUND; then
  echo "not found"
fi


Last edited by Scrutinizer; 01-26-2011 at 07:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

3. Shell Programming and Scripting

How to run the Shell Script from external directory using java?

Hi, I have created a Shell Script and invoke through java using Process Builder It's working fine, if (Shell script file ) in the same directory as java file. By Problem: How to run the Shell Script file( resides in external directory) using java. What configuration i have... (1 Reply)
Discussion started by: nanthagopal
1 Replies

4. Shell Programming and Scripting

passing file extension using external variable

Hi, How can I modify the FILETYPE command ? I want to provide the file extension, like txt, root .? Thanks, #!/bin/bash FROM=$1 TO=$2 FILETYPE=$3 ... (4 Replies)
Discussion started by: nrjrasaxena
4 Replies

5. Shell Programming and Scripting

How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how?? I am using below code but its not working. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

6. Shell Programming and Scripting

[Solved] Value of a variable is not recognised for commands comes from external file

Hi, my script is setting a variable with value and this variable is present in my another command that is coming from external file and this command is internally called after this variable is set. but while execution of this command, the value is not retrieved properly. say, my script... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

querry ..including external shell script

is there any way to include one shell script into other like include in c apart from putting it in a function (3 Replies)
Discussion started by: mobydick
3 Replies

8. Shell Programming and Scripting

Shell script to read file into variable

the script i am trying to write will allow my server to give itself an ip address. So far i am up to the following but i'm stuck. tracert -m 1 > traceroute.txt 1 routername (ipaddr) 2.094 ms 1.789 ms 1.243 ms i want to get ipaddr as a variable and use it to write the ifcfg-eth... (7 Replies)
Discussion started by: aspect_p
7 Replies

9. UNIX for Dummies Questions & Answers

accessing shell script variable in file

Hi, I have a shell script in which there is a file conn_$temp where $temp has the pid of the shell script. in this shell script i have an embedded awk script that must read the file while ((getline < "conn_$temp") > 0) However due to the "$temp" in the file name, the awk script is... (6 Replies)
Discussion started by: HIMANI
6 Replies

10. Shell Programming and Scripting

How to parse config variables from external file to shell script

How do i use a config.txt to recursively pass a set of variables to a shell script eg my config.txt looks like this : path=c://dataset/set1 v1= a.bin v2= b.bin path=c://dataset/set2 v1= xy.bin v2= abc.bin .................. and so on . and my testscript : (2 Replies)
Discussion started by: pradsh
2 Replies
Login or Register to Ask a Question