How to convert string to integer in a shell script? -
i have file of 4gb in /var/tmp/test/dsk/file.1 in path. can see through solaris linux command follows:
-bash-4.4# du -h /var/tmp/test/dsk/file.1 4.0g /var/tmp/test/dsk/file.1
i want disk size did following.
disk=`du -h /var/tmp/test/dsk/file.1| awk '{print $1}'|tr -d '.g'`
which gives me output follows:
-bash-4.4# echo $disk 4.0
but want compare disk size value 50 , first need convert above output integer, i'm failing that, can in this, have searched not getting exact solution.
sorry don't see probem here :) tool bc accepts whatever format may give:
[ $(bc <<< "$disk>=50") -eq 1 ]
btw, might want convert human readable size output simple numeric byte size function like:
# converts human readable sizes integer kilobyte sizes # usage numericsize="$(humantonumeric $humansize)" function humantonumeric { local value="${1}" local notation local suffix local suffixpresent local multiplier notation=(k m g t p e) suffix in "${notation[@]}"; multiplier=$((multiplier+1)) if [[ "$value" == *"$suffix"* ]]; suffixpresent=$suffix break; fi done if [ "$suffixpresent" != "" ]; value=${value%$suffix*} value=${value%.*} # /1024 since convert kilobytes instead of bytes value=$((value*(1024**multiplier/1024))) else value=${value%.*} fi echo $value }
Comments
Post a Comment