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
# 1  
Old 01-25-2011
Getting category when given the variable from external file to shell script

Hi, I have a script that interacts with a config file in the format:

Code:
[CategoryA]
file1.txt
file2.txt
 
[CategoryB]
file3.txt
file4.txt
file5.txt
 
[CategoryC]
 
[CategoryD]
file6.txt

I would like to return the Category, when given the file name.

Code:
FILENAME=file4.txt
CATEGORY=?

Anyone know how to do this?
# 2  
Old 01-25-2011
Nothing to it but to look through the file and check.

Code:
#!/bin/ksh

TOFIND="file4.txt"
CATEGORY=""

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

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

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

Should also work in BASH.
# 3  
Old 01-25-2011
Thank you for the quick reply. I get the following output:

Code:
 
./script.sh[10]: "${LINE:0:1}": bad substitution

Smilie
# 4  
Old 01-25-2011
I copy-pasted that from the working script so it shouldn't contain typos. Did you type that in or copy it?

What is your shell? What is your system?
# 5  
Old 01-25-2011
I copy/pasted.

Bash ver 3.00.16(1)
Running on AIX5.1
# 6  
Old 01-25-2011
That's very strange. The "${VAR:OFFSET:LENGTH}" syntax works even in bash 2.0, let alone 3.0. It even works in ASH. I don't know how they butchered it to get it working in AIX. Are you running it as /bin/bash or /bin/sh ?

Are you sure you don't have DASH installed? That's a BASH-alike that throws up on that syntax.

If you have ksh available could you try using it instead.
# 7  
Old 01-25-2011
Sorry, I should have been more explicit.

I'm using bash, but the script is using ksh (I did copy/paste your example).

I changed the first line to reference bash and it does work, unfortunately the rest of my script uses ksh and that can't change for various reasons.

I see here:

[Edit: You are only allowed to post URL once you have at least 5 posts. ]
h t t p : // w w w .issociate.de/board/post/265609/ksh_variable_substitution_quandry.html

that various versions of ksh can't use that substitution syntax. I've been trying to figure out the version of ksh we have but can't seem to ...

Last edited by MoreCowbell; 01-25-2011 at 04:16 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