LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-03-2012, 09:58 PM   #1
DemonWav
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
Shell Script troubles


First off I want to start off saying that I am working on this script for an Android phone, so the commands are generally limited to busybox. If busybox can't do it, then neither can I. This is the entire script I am working on:
Code:
#!/sbin/sh

if [ -e /dev/block/mtdblock0 ] ; then
	if [ -d /data ] ; then
		/tmp/busybox mount -t ext4 /dev/block/mmcblk0p2 /data
	else
		/tmp/busybox mkdir mkdir /data
		/tmp/busybox mount -t ext4 /dev/block/mmcblk0p2 /data
	fi
	
	if [ -h /data/data/ ] ; then
		/tmp/busybox touch /tmp/nofix
	else
		if [ -d /datadata ] ; then
			/tmp/busybox mount -t yaffs2 /dev/block/mtdblock6 /datadata
		else
			/tmp/busybox mkdir /datadata
			/tmp/busybox mount -t yaffs2 /dev/block/mtdblock6 /datadata
		fi
		/tmp/busybox rm -rf /datadata/*
		FREE_SPACE=$(/tmp/busybox df -h | /tmp/busybox awk '$1 == "/dev/block/mtdblock6" {print $3}') ; USED_SPACE=$(/tmp/busybox du -sh /data/data/ | /tmp/busybox awk '{print $1}')
		NEW_TERM=$(echo $FREE_SPACE $USED_SPACE | /tmp/busybox awk '{print $1 $2}')
		FREE_SPACE=$(echo $NEW_TERM | /tmp/busybox awk '{print $1}') ; USED_SPACE=$(echo $NEW_TERM | /tmp/busybox awk '{print $2}')
		if [ $FREE_SPACE -gt $USED_SPACE ] ; then
			/tmp/busybox touch /tmp/revert_pass
		else
			/tmp/busybox touch /tmp/revert_fail
		fi
	fi
else
	exit 1
fi
All goes well and good, until I get to line 22:
Code:
NEW_TERM=$(echo $FREE_SPACE $USED_SPACE | /tmp/busybox awk '{print $1 $2}')
Basically my problem is I need two integers to do the test in line 24:
Code:
if [ $FREE_SPACE -gt $USED_SPACE ] ; then
and the output from the command on line 21:
Code:
FREE_SPACE=$(/tmp/busybox df -h | /tmp/busybox awk '$1 == "/dev/block/mtdblock6" {print $3}') ; USED_SPACE=$(/tmp/busybox du -sh /data/data/ | /tmp/busybox awk '{print $1}')
is something like 132.5M for FREE_SPACE and 129.7M for USED_SPACE (I basically just made up those numbers, but they are numbers like that, 3 digits before the decimal, a digit after the decimal, and an M). What I am attempting to do with line 22 is try to maybe do 3 backspaces, if you will, on each of the two results. so 132.5M becomes 132 and 129.7M becomes 129. I don't need to know the decimal, it is really unimportant for my purposes. If I can accomplish that then I will be set for the rest of the script, but I really have no idea how to do that (or if its possible at all).

Last edited by DemonWav; 01-03-2012 at 10:23 PM.
 
Old 01-03-2012, 11:33 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by DemonWav View Post
Code:
FREE_SPACE=$(/tmp/busybox df -h | /tmp/busybox awk '$1 == "/dev/block/mtdblock6" {print $3}')
USED_SPACE=$(/tmp/busybox du -sh /data/data/ | /tmp/busybox awk '{print $1}')
Why not use df -k and du -sk instead? That should give the sizes in integer kilobytes.

You could also guard against integer overflow, using
Code:
FREE_SPACE=$(/tmp/busybox df -k | /tmp/busybox awk '$1 == "/dev/block/mtdblock6" {print int($3/1024) }')
USED_SPACE=$(/tmp/busybox du -sk /data/data/ | /tmp/busybox awk '{print int($1/1024) }')
in which case they're both in megabytes (MiB, or units of 1024*1024=1048576 bytes). (The awk expression also makes sure they're integers; no decimals. You may wish to use int($1/1024+512) and int($3/1024+512) if you want correct rounding; the above rounds towards zero.)

Last edited by Nominal Animal; 01-03-2012 at 11:35 PM.
 
Old 01-04-2012, 12:49 AM   #3
DemonWav
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Dude, you're awesome! Thanks! Yeah, I'm kinda new at this (well, new at making these more complex scripts) and I had never heard of awk before until I started looking around for some way to accomplish my goal. Or df or du, so I wasn't all to sure with what I could do with them. But what you suggested worked perfectly, thanks for the help!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script running in another shell script and then exit dreamervlk Linux - General 3 09-16-2011 06:40 AM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration