Hi Guys,
I am new to shell scripting and trying out a simple shell script.
- Grep "MemTotal" from /proc/meminfo store it in "mem" variable.
- Extract digits in the same order from "mem" variable, store in to "mem_digit".
- Divide the captured value from 1024 to get in MB, store in "mem_mb".
- Display value from "mem_mb".
#!/bin/bash
mem=`(cat /proc/meminfo | grep MemTotal)`
mem_digit=`(echo "$var" | /usr/bin/tr -cd [:digit:])`
mem_mb=$(($mem_digit/1024))
echo "System RAM : $mem_mb MB"
While running this shell script i am getting below error:
[root@localhost bash]# ./demo.sh
./demo.sh: line 5: /1024: syntax error: operand expected (error token is "/1024")
System RAM : MB
But when i am trying same lines of code 1 by 1 on the direct console i am able to get the correct output.
[root@localhost bash]# mem=`(cat /proc/meminfo | grep MemTotal)`
[root@localhost bash]# mem_digit=`(echo "$var" | /usr/bin/tr -cd [:digit:])`
[root@localhost bash]# mem_mb=$(($mem_digit/1024))
[root@localhost bash]# echo " System RAM : $mem_mb MB"
System RAM : 1869 MB
Could you guys please point out if i have missed something in the script ?
-Ashish:confused: