String issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String issue
# 1  
Old 07-04-2013
String issue

Hi,

Facing a strange problem with the string and also an issue related to trailing space.


Code:
# cat test.sh
#! /bin/sh
typeset -a arr

read_params()
{
#while read line
until [ -z "$1" ]
do
    if [ `expr index "$1" =` != 0 ] ; then
        var="${1%%=*}"
        for key in ${1//,/ }
        do
                val="${key#${var}=}"
                val="${val%=*}"
                if [ ! -z arr["$var"] ]
                then
                        arr["$var"]="${arr["$var"]} $val"
                else
                        arr["$var"]="$val"
                fi
        done
            eval $var=\"${arr["$var"]}\"
    fi
shift
done
#done < $1

for k in "${!arr[@]}"
do
        printf "%s %s\n" "$k" "${arr["$k"]}"
done

}

main()
{
read_params `cat file`

echo "$temp  and $lib and $"r.etc" "

}

main

Code:
# cat file
temp=/usr=25,/usr/lib=12
bin=12
r.etc=45
lib=12:6

Code:
# sh -x test.sh
+ typeset -a arr
+ main
++ cat file
+ read_params temp=/usr=25,/usr/lib=12 bin=12 r.etc=45 lib=12:6
+ '[' -z temp=/usr=25,/usr/lib=12 ']'
++ expr index temp=/usr=25,/usr/lib=12 =
+ '[' 5 '!=' 0 ']'
+ var=temp
+ for key in '${1//,/ }'
+ val=/usr=25
+ val=/usr
+ '[' '!' -z 'arr[temp]' ']'
+ arr["$var"]=' /usr'
+ for key in '${1//,/ }'
+ val=/usr/lib=12
+ val=/usr/lib
+ '[' '!' -z 'arr[temp]' ']'
+ arr["$var"]=' /usr /usr/lib'
+ eval 'temp="' /usr '/usr/lib"'
++ temp=' /usr /usr/lib'
+ shift
+ '[' -z bin=12 ']'
++ expr index bin=12 =
+ '[' 4 '!=' 0 ']'
+ var=bin
+ for key in '${1//,/ }'
+ val=12
+ val=12
+ '[' '!' -z 'arr[bin]' ']'
+ arr["$var"]=' /usr /usr/lib 12'
+ eval 'bin="' /usr /usr/lib '12"'
++ bin=' /usr /usr/lib 12'
+ shift
+ '[' -z r.etc=45 ']'
++ expr index r.etc=45 =
+ '[' 6 '!=' 0 ']'
+ var=r.etc
+ for key in '${1//,/ }'
+ val=45
+ val=45
+ '[' '!' -z 'arr[r.etc]' ']'
test.sh: line 17: r.etc: syntax error: invalid arithmetic operator (error token is ".etc")

Expected output:
Code:
echo "$temp  and $lib $"r.etc" "

/usr /usr/lib and 12:16 45

with out trailing spaces for each variable means "temp, lib and r.etc"

I need to use the same logic which was written in the above code. Could you please let me know the way how to get expected output from the above code.
# 2  
Old 07-04-2013
Wasnt a good idea using a dot in a variable name... replacing . by _ in so you have r_etc instead should help...
# 3  
Old 07-04-2013
With the evil eval (see many earlier threads) you must consider who can write to the file in question. You are trusting the content to be suitably executable. Syntax errors can cause very confusing results.

What are you trying to achieve here? I've not deciphered it all, but it seems at first glance to be setting variables for your environment within the script before you do any real processing, could you just source the file like this:-
Code:
#!/bin/sh
typeset -a arr

. $file       # Setting environment here

echo "temp=\"$temp\", lib=\"$lib\" and r_etc=\"$r_etc\""

# Real processing continues here ....

Note that there is no space on the first line between #! and /bin/sh

Of course, you have to ensure that the file is syntactically correct, and it will not allow a mid statement comment. I've added in the echo with the variable name suggested by vbe. It uses escaped double quotes so that they appear in the output. If you don't escape them, the shell assumes that they are to be processed as start/end of string, so that can make your message a bit confusing. Showing the quotes also shows the boundary of your variable, just in case you want it to include a space at the end.


Does that help?



Robin
Liverpool/Blackburn
UK
# 4  
Old 07-04-2013
Quote:
Originally Posted by rbatte1
With the evil eval (see many earlier threads) you must consider who can write to the file in question. You are trusting the content to be suitably executable. Syntax errors can cause very confusing results.

What are you trying to achieve here? I've not deciphered it all, but it seems at first glance to be setting variables for your environment within the script before you do any real processing, could you just source the file like this:-
Code:
#!/bin/sh
typeset -a arr

. $file       # Setting environment here

echo "temp=\"$temp\", lib=\"$lib\" and r_etc=\"$r_etc\""

# Real processing continues here ....

Note that there is no space on the first line between #! and /bin/sh

Of course, you have to ensure that the file is syntactically correct, and it will not allow a mid statement comment. I've added in the echo with the variable name suggested by vbe. It uses escaped double quotes so that they appear in the output. If you don't escape them, the shell assumes that they are to be processed as start/end of string, so that can make your message a bit confusing. Showing the quotes also shows the boundary of your variable, just in case you want it to include a space at the end.


Does that help?



Robin
Liverpool/Blackburn
UK
Thanks for quick response and for the information provided.

As per my requirements, my file should contains a variable with the name as "r.etc". Is there any hack for this which I can use it in my code.

one more thing the output which I am getting at present(with eval command) contains a trailing space. it has to be removed from there it self where I am executing eval command.

Need some hacks.
# 5  
Old 07-04-2013
The use of a dot in the variable name is a problem, as mentioned by vbe. You might be able to pop in some escape to use it if you really want to, but I've never bothered. Can you really not replace the . with _?


I'm still confused as to what you are attempting. How much control do you have over the input file?

If this is the fixed format, could you not just:-
Code:
temp=` grep "^temp=" file  | cut -f2 -d"=" | tr "," " "`
lib=`  grep "^lib=" file   | cut -f2 -d"="`
r_etc=`grep "^r.etc=" file | cut -f2 -d"="`

echo "temp=\"$temp\" lib=\"$lib\" r_etc=\"$r_etc\""

echo "$temp and $lib $r_etc"

Smilie

I know you might want to follow someone else's style, but it would be better for you know understand the logic and code if you are asked to support it. Keep the goal in mind and keep it simple. Smilie

Is this getting you anywhere?


Robin
# 6  
Old 07-04-2013
Quote:
Originally Posted by munna_dude
Thanks for quick response and for the information provided.

As per my requirements, my file should contains a variable with the name as "r.etc". Is there any hack for this which I can use it in my code.

... ... ...

Need some hacks.
Need a hack. Fine, it is simple. Write your own shell! Standard shells are required to accept user-defined variable names that contain the 52 uppercase and lowercase letters (a-z, A-Z), the 10 digits (0-9) and underscore (_). The shell is allowed to accept other characters in user-defined names, but is not required to do so. I have not seen any standards conforming shell that accepts a period (.) in a user-defined variable name.
# 7  
Old 07-04-2013
Quote:
Originally Posted by Don Cragun
I have not seen any standards conforming shell that accepts a period (.) in a user-defined variable name.
I agree, but, in the spirit of "need some hacks!", with ksh93's compound variables you can somewhat fake it. The dot is a delimiter, not actually a character in an identifier name, and compound variables use a more complicated assignment syntax.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Issue in inserting null string in array

I am getting some values from a file and putting them in an array..but the null strings are not getting passed to the array. So during printing the elements ,the null string is not showing in the output. during array size calculation it is also excluding null.Please let me know how to do it. # cat... (2 Replies)
Discussion started by: millan
2 Replies

2. Shell Programming and Scripting

Issue Spliting String in Python

I have a string like below Note: I have have a single to any number of comma "," seperated string assigned to jdbc_trgt variable. I need to split jdbc_trgt using comma(,) as the delimiter. I tried the below but it fails as i dont know how can i read each split string iterately. for... (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

Issue deleting all lines (having a specific string) in a file

I'm trying to create a script. There are 2 files - fileA.log & fileB.log fileA.log has the below data : aaaa cccc eeee fileB.log has the below data : cjahdskjah aaaa xyz jhaskjdhas bbbb abc ajdhjkh cccc abc cjahdskjah ... (7 Replies)
Discussion started by: Pandee
7 Replies

4. Programming

Unicode String Issue

I am storing some unicode characters "лфи" in a char array. When I view(x/30s <variable name>) the values in gdb it show me something like: 0x80ac47c: "?\004>\004 " 0x80ac482: "A\0048\004;\004L\004D\004>\004=\004:\0045\004/" Why it is happening so and what are these \004 representing? (1 Reply)
Discussion started by: rupeshkp728
1 Replies

5. Shell Programming and Scripting

bash integer & string issue

Hi guys, I need for the bash code below a little bit help: cat script.sh #!/bin/bash 5_MYVALUE="test" echo "$5_MYVALUE" If I try to run the script, got follow failure: ./script.sh ./script.sh: line 4: 5_MYVALUE=test: command not found _MYVALUE My questions are how... (4 Replies)
Discussion started by: research3
4 Replies

6. Shell Programming and Scripting

Display the string issue

Hi, i ran following bdf command and found below out with space uses. prd@prd02/usr/apps/cti>bdf | grep /usr/apps /dev/vg01/lvol6 17379328 9873783 7036696 58% /usr/apps /dev/vg01/SYBASE 1228800 978631 234597 81% /usr/apps/sybase 2150400 1108516 976776 ... (3 Replies)
Discussion started by: koti_rama
3 Replies

7. Shell Programming and Scripting

Issue with String Comparison (if)

Hi, I was trying to do a string comparison using if. However, the comparison result is getting treated as a executable statement. I'm not sure where I'm making the mistake! $ typeset TEST_VAR='YUP' $ if ; then echo 'Got It!'; fi; ksh: : not found. Any help is appreciated! (3 Replies)
Discussion started by: waterdrop
3 Replies

8. Shell Programming and Scripting

bash version or string issue

Hi all, I'm not sure but I guess, that is a bash version issue. The script working fine on "GNU bash, version 3.2.25(1)-release Ubuntu". #!/bin/bash while IFS=">" read a id val do if ] then VAL=${id%<*}; ID=${id#*</} echo $VAL echo $ID sed... (5 Replies)
Discussion started by: research3
5 Replies

9. Shell Programming and Scripting

string manipulation issue

I have myMethod that gives me available,used,free disk space in KB. I parse the used disk space using awk. That gives me something like 830,016. I want the output to be 830016 so that I can add 100000 to it. In other words I would like to use used_space variable in numeric calculations (using... (5 Replies)
Discussion started by: illcar
5 Replies

10. Shell Programming and Scripting

String concatenation issue in ksh

Hello All, I'm tryying to concatenate string and variables value in ksh, but i'm unable to do it, can someone please help in rectifying my error, here is the code i have written, #!/usr/bin/ksh -x cat $1 | while read fileline do val1= echo $fileline | awk -F, '{print $1}' val2= echo... (3 Replies)
Discussion started by: arvindcgi
3 Replies
Login or Register to Ask a Question