Test function not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test function not working
# 1  
Old 04-18-2013
Test function not working

i am writing the following snippet and cannot figure out why the variable measType remains 0. it should be 1

I also tried "\#analog" and '\#analog' in the test statement and it still doesn't work. What is going on?

Code:
bash$ measType=0
bash$ read line < 2iconfig.ini
bash$ echo $line
#analog
bash$ if [ "$line" = "#analog" ]; then
> measType=1
>fi
bash$ echo $measType
0
bash$

# 2  
Old 04-18-2013
Can you post the output of below command:
Code:
echo "$line" | od -c

This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-18-2013
Code:
bash$ echo "$line" | od -c
0000000 # a n a l o g \n
0000011
bash$

---------- Post updated at 10:57 AM ---------- Previous update was at 10:45 AM ----------

i found my problem! My text file had a \n at the end. that was being compared...since this is a non-printable asci character, i did not see the problem when i was printing it out. Thanks for teh suggestion of using the od command....just curious what do 0000000 and 0000011 mean?

Last edited by oahmad; 04-18-2013 at 11:49 AM.. Reason: wrong command, forgot the $
# 4  
Old 04-18-2013
0000000 & 0000011 represents bytes.

Refer this page for usages and examples.
# 5  
Old 04-18-2013
0000000 is byte position 0, beginning of file.
0000011 is byte position octal 011 (decimal nine).

Also:
Code:
$ read line < 2iconfig.ini
$ [ "$line" = "#analog" ]
$ echo $?

You will get 0 or 1 depending on whether the test worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong test interpretation for simple function.

Hello everyone, I have written simple script below to check if ip is added to interface #!/usr/local/bin/bash IFCONFIG="/sbin/ifconfig" SERVICE="/usr/sbin/service" IP="79.137.X.X" GREP=$(${IFCONFIG} | grep ${IP}) ip_quantity_check () { echo ${GREP} | wc -l } if ];... (2 Replies)
Discussion started by: bryn1u
2 Replies

2. UNIX for Advanced & Expert Users

Test -e not working as expected (by me)

I ran into the following and still do not understand entirely the rationale behind this. If someone could explain why things are as they are I'd be thankful. The following was tested on AIX 7.1 with ksh88, but i suspect that to be ubiquitous. In an installation routine i had to create a set of... (6 Replies)
Discussion started by: bakunin
6 Replies

3. UNIX for Advanced & Expert Users

if and test not working

All, I am getting error when run file check with test operator . Why is it showing the error if then echo 'file found' fi ksh: -r: unknown test operator I know i can use the below code to test the file but why the above is not working if test -r filename ... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

4. Shell Programming and Scripting

explanation of test function

I have found a code some where, which looks like if (test $value) then <do something> fi I am not understanding what is test doing here. I have seen test with !,-eq, -e etc. But, the above appears to be a new one to me. Can anyone please expalin me. (4 Replies)
Discussion started by: mady135
4 Replies

5. Solaris

How i test the UDP port is working?

Hi anybody know how to test out the UDP port working? cos UDP cannot use telnet to test... Sumemr (2 Replies)
Discussion started by: summerpeh
2 Replies

6. UNIX for Advanced & Expert Users

test for function name?

Hi, I found this: https://www.unix.com/unix-advanced-expert-users/27318-how-see-function-shell.html which indicates I can use typeset -f to list the functions. But it also lists the contents of the functions. Is there a lighter weight way to see what is defined? Is there an internal... (5 Replies)
Discussion started by: duderonomy
5 Replies

7. Programming

C function to test existence of a login

Hi everybody, I need to check in C program whether a given login is known on the system. Is there any system function that could do this ? So far, all I could find is getpwnam(), which answers my problem by parsing the local password database. But won't work if a user is authenticated by... (2 Replies)
Discussion started by: xavier054
2 Replies

8. Shell Programming and Scripting

test command is not working

#!/bin/ksh size=3978132853 limit=100 if ;then echo exceeded limit fi This does not work though if i reduce 3978132853 to 397813285 it works any ideas and work around appreciated (SunOS 5.9 Generic_117171-02 sun4u sparc SUNW,Ultra-80 ) (2 Replies)
Discussion started by: zam
2 Replies

9. Shell Programming and Scripting

function to test if file is open

I need to write a function that will work in sh/ksh shell that will test to see if a file has already been opened for writting by another user has anyone written something like this? (3 Replies)
Discussion started by: johnsonbryce
3 Replies

10. Programming

C function to test string or integer

Hi everyone , Is there any predefined C function that tests whether an input is string or an integer? Thank's in advance :) (3 Replies)
Discussion started by: qqq
3 Replies
Login or Register to Ask a Question