Tag Info

New answers tagged

2

It depends on the command. By convention, a standalone dash in place of a filename, would mean 'stdin' or 'stdout'. E.g. tar cvfz - ./path-to-tar | ssh somehost tar xvfz - However this is merely a standard sort of a flag for this purpose - what it actually means is 'whatever the person who wrote the command thought it should'.


3

You can also use the split+glob operator (leave a variable unquoted) (and which you used for no apparent reason in your question): IFS=: # configure the split part to use : as the delimiter set -f # disable the glob part set -- $1 # $1 is split on : and parts are stored in $1, $2... X=$1 Y=$2 You can also do: printf '%s\n' "$1" | { IFS=: read -r ...


4

In dash, each command in Pipelines run in subshell (zsh and AT&T ksh, the rightmost command in pipelines doesn't), so variables X and Y are no longer existed when your command done. Simply, you can use Parameter Expansion, try: $ set -- foo:123 $ X=${1%:*} $ Y=${1#*:} The example is used for interactive session. Inside your script, you don't need ...


0

In POSIX system, you can use expr: $ a=a $ expr "$a" - 0 >/dev/null 2>&1 $ [ "$?" -lt 2 ] && echo Integer || echo Not Integer


0

You can use the -eq test on the string, with itself: $ dash -c 'a="a"; if [ "$a" -eq "$a" ] ; then echo number; else echo not a number; fi' dash: 1: [: Illegal number: a not a number $ dash -c 'a="0xa"; if [ "$a" -eq "$a" ] ; then echo number; else echo not a number; fi' dash: 1: [: Illegal number: 0xa not a number $ dash -c 'a="-1"; if [ "$a" -eq "$a" ] ; ...


5

Whether dash, bash, ksh, zsh, POSIX sh, or posh ("a reimplementation of the Bourne shell" sh) ; the case construct is the most widely available and reliable: case $1 in (*[!0-9]*|"") false ;; (*) true ;; esac


0

Perhaps with expr? if expr match "$1" '^\([0-9]\+\)$' > /dev/null; then echo "integer" else echo "non-integer" fi


0

Try using it as an arithmetic expansion, and see if it works. Actually, you need to be a bit stricter than that, because arithmetic expansions would ignore leading and trailing spaces, for instance. So do an arithmetic expansion, and make sure that the expanded result matches the original variable exactly. check_if_number() { if [ "$1" = "$((${1}))" ] ...


10

The following detect integers, positive or negative, and work under dash and are POSIX: Option 1 echo "$1" | grep -Eq '^[+-]?[0-9]+$' && echo "It's an integer" Option 2 case "${1#[+-]}" in ''|*[!0-9]*) echo "Not an integer" ;; *) echo "Integer" ;; esac Or, with a little use of the : (nop) command: ! case ${1#[+-]} in ...



Top 50 recent answers are included