if [ -z $(echo $var | grep [0-9]) ]; then echo "NON NUMERIC"; fi

Non Numeric Check

use to execute a block of code only if $var is numeric

2
2009-06-04 07:41:26

These Might Interest You

  • Using the standard numeric comparison but suppressing the STDERR output acts as the simplest way to check a value is numeric. See sample output for some examples. Show Sample Output


    0
    if [ "$testnum" -eq "$testnum" 2>/dev/null ]; then echo It is numeric; fi
    jgc · 2009-10-09 14:57:27 4
  • if you don't do --numeric-ports, netstat will try to resolve them to names Show Sample Output


    1
    netstat -a --numeric-ports | grep 8321
    peter4512 · 2012-01-20 22:00:56 3
  • This creates an archive that does the following: rsync:: (Everyone seems to like -z, but it is much slower for me) -a: archive mode - rescursive, preserves owner, preserves permissions, preserves modification times, preserves group, copies symlinks as symlinks, preserves device files. -H: preserves hard-links -A: preserves ACLs -X: preserves extended attributes -x: don't cross file-system boundaries -v: increase verbosity --numeric-ds: don't map uid/gid values by user/group name --delete: delete extraneous files from dest dirs (differential clean-up during sync) --progress: show progress during transfer ssh:: -T: turn off pseudo-tty to decrease cpu load on destination. -c arcfour: use the weakest but fastest SSH encryption. Must specify "Ciphers arcfour" in sshd_config on destination. -o Compression=no: Turn off SSH compression. -x: turn off X forwarding if it is on by default. Flip: rsync -aHAXxv --numeric-ids --delete --progress -e "ssh -T -c arcfour -o Compression=no -x" [source_dir] [dest_host:/dest_dir]


    11
    rsync -aHAXxv --numeric-ids --delete --progress -e "ssh -T -c arcfour -o Compression=no -x" user@<source>:<source_dir> <dest_dir>
    somaddict · 2012-12-26 13:46:23 2
  • printf treats first char after single ' as numeric equivalent


    1
    ord() { printf "%d\n" "'$1"; }
    zude · 2009-10-17 22:02:52 0

  • 0
    for i in $(seq -w 0 100) ; do mv prefix$(( 10#$i )).jpg prefix${i}.jpg ; done
    madmike · 2013-02-03 23:23:01 0
  • Not everyone reads manpages. Aliasing this command will help with the task of doing audits with RKhunter. It will check for the latest version, update the definitions and then run a check on the system. Hint: alias that in your .bashrc to make life for your fingers easier.


    2
    rkhunter --versioncheck --update --propupd --check
    vxbinaca · 2009-12-15 00:23:45 1

What Others Think

Hmm... It doesn't really check if a variable is numeric or not, if just tells if there's a digit in there. I wouldn't consider "d42d" numeric. Change the test to this: if [ -z $(echo $var | grep "^[0-9]\+$") ]; then echo "NON NUMERIC"; fi Now, it will return "NON NUMERIC" for things like "d42", "4d2", "42d"
flatcap · 468 weeks ago
Ah, sorry, I have it mislabeled. It should be "execute a block of code only if $var is NON-numeric." However, flatcap, I also like your code too, perhaps it is more useful; I was just performing a bunch of ops on my computer that needed to determine if a number was purely non-numeric and included what I used.
AnusJenkins · 468 weeks ago
If you don't need a block, that is a good way: ( echo $var | grep -q '[^0-9]' ) && echo "NON NUMERIC" ...and the -q makes a better clean output.
aurium · 467 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: