Value of variable is NULL, but test doesn't seem to recognize


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Value of variable is NULL, but test doesn't seem to recognize
# 1  
Old 04-14-2012
Value of variable is NULL, but test doesn't seem to recognize

Hello, Unix-forums!

My problem:
Code:
read -p "Enter any number, please" number
sleep 1
echo $number | tr -d 0-9
test -z $number && echo "Thank you" || echo "This is not a number"

Test always displays "This is not a number". It doesn't matter if I entered a or 1.

But if I order
Code:
echo $number

before the test command, it displays nothing, so the $number should be NULL, not?

Last edited by intelinside; 04-14-2012 at 08:37 AM..
# 2  
Old 04-14-2012
Try:

Code:
read -p 'Enter any number, please: ' number
sleep 1
number=$( tr -d 0-9 <<< "$number" )
test -z "$number" && 
  echo "Thank you" || 
    echo "This is not a number"

Actually you could avoid calling external commands in this case:

Code:
read -p 'Enter any number, please: ' number
sleep 1
test -z "${number//[0-9]}" && 
  echo "Thank you" || 
    echo "This is not a number"

P.S. The code above uses non standard shell features, but that shouldn't be
a problem, considering that the original post uses non standard features too Smilie
This User Gave Thanks to radoulov For This Post:
# 3  
Old 04-14-2012
Thank you. But can you tell me what's wrong with my code. I'd be really interested in that.

Edit: Solved. Looked closer to radoulov's syntax. You're my hero Smilie

Last edited by intelinside; 04-14-2012 at 12:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. IP Networking

DNS problem : ping doesn't recognize hostname

I have vmware on my windows PC ( hostname : acer ). vmware has RHEL 7 ( hostname : rhel7 ) installed recently. RHEL IP configuration IP : 192.168.5.128 Netmask : 255.255.255.0 ssh to rhel7 works from acer using putty resolve.conf cat /etc/resolv.conf nameserver 192.168.5.1 host... (12 Replies)
Discussion started by: hiten.r.chauhan
12 Replies

2. Shell Programming and Scripting

How to test that a string doesn't contain specific characters?

Hi ! :) I made this : #!/bin/bash rsa_dir="/etc/openvpn/easy-rsa/" rsa_key_dir="/etc/openvpn/easy-rsa/keys/" ccd_dir="/etc/openvpn/ccd/" regex_special_char='' cd $rsa_dir while read -p "Please can you enter the vpn's username : " username ] || ] || ] || ] do echo "Your entry... (10 Replies)
Discussion started by: Arnaudh78
10 Replies

3. Shell Programming and Scripting

[Solved] If doesn't evaluate the first test

Good afternoon, I am tearing hair out over this. It should be so simple. I have an if statement to evaluate whether or not replication is working. I am testing variables from mysql to see if they are both "Yes". I have put some echo statements in to see how far the code proceeds. It always... (6 Replies)
Discussion started by: jimm
6 Replies

4. Shell Programming and Scripting

Expect doesn't recognize a password prompt

Hi. Here is beginning of my script #!/usr/local/bin/expect -- set timeout 15 spawn /usr/local/account.sh -n modify expect "Password:" {send "mypassword\r"} But due to some terminal control sequences (or something else, dunno exactly) my password prompt is looking like this: and expect... (3 Replies)
Discussion started by: urello
3 Replies

5. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

6. Shell Programming and Scripting

how to check null variable

korn shell If then update_smartcare_user_password "$u_id" else echo "Not a database user" fi i get this error Syntax error at line *** : `then' is not expected. what should i do. I want to check whether $a is null or not. (2 Replies)
Discussion started by: sachin.gangadha
2 Replies

7. HP-UX

HP-UX 11.11: X doesn't recognize mouse and keyboard

hi folks, i've got a blank hp visualize C3000 workstation and installed HP-UX 11.11. When I want to start X, I get the following error message: # X Fatal server error: Couldn't open X pointer device! Is one attached? I've connected an mouse and a keyboard with an usb/ps2 connector.... (5 Replies)
Discussion started by: grisu
5 Replies

8. Shell Programming and Scripting

test Null variable

hi forum i beginning with script and i want test un null variable in a schell i just don t know the syntax here is a litle example y=test echo $y unset y echo $y (so here Y = Null) if Y=Null then echo "y is null" exit fi (1 Reply)
Discussion started by: kykyboss
1 Replies

9. Shell Programming and Scripting

Doesn't recognize the mv command

I'm nearly finished my program i've got everything in place and than when i run it it comes back with the reply mv: command not found. This is the code that seems to be causing the problem. elif then echo "There are more than one '$1' files in the system." echo "Please... (2 Replies)
Discussion started by: zoolz
2 Replies
Login or Register to Ask a Question