Visit Our UNIX and Linux User Community


Getting size of a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting size of a variable
# 1  
Old 05-28-2012
Getting size of a variable

if i do the following:

Code:
echo "hello, mr. sunshine" > /tmp/text.txt

du -s /tmp/text.txt

The text.txt file now contains "hello, mr. sunshine". So i did a du -s on the file to see how much space the text i inserted took.

now, instead of writing "hello, mr. sunshine" to a text file on the disk, i'd like to save it to a variable and calculate how much space it would take on disk without having to write to disk.

So, something along the lines of:

Code:
VAR="hello, mr. sunshine"

du -s $VAR  (this wont work of course, since the content of a $VAR is not a actual file).

Is what i'm asking possible?

I'm running Red Hat 6 and Sun 5.10 and i'm using bash.
# 2  
Old 05-29-2012
Could this help you ?
Code:
echo ${#VAR}   #display length of variable VAR

# 3  
Old 05-29-2012
Quote:
Originally Posted by pravin27
Could this help you ?
Code:
echo ${#VAR}   #display length of variable VAR

it could actually. i guess the next question would be, how many character makes up 1K?
# 4  
Old 05-29-2012
What about a
Code:
wc -c

command?
Seems strange the writing to a file to determine how big. Why not simply the word-count command?
# 5  
Old 05-29-2012
Quote:
Originally Posted by joeyg
What about a
Code:
wc -c

command?
Seems strange the writing to a file to determine how big. Why not simply the word-count command?
what i'm looking for is how much disk space would be consumed if what i have in variable was written to disk
# 6  
Old 05-29-2012
Quote:
what i'm looking for is how much disk space would be consumed if what i have in variable was written to disk
Well, that is a function of how much stuff is there. The wc command can give an indication of how large; for an exact answer you would need to know more about your disk system -- 512 byte blocks, 1024 byte blocks, etc...

Previous Thread | Next Thread
Test Your Knowledge in Computers #793
Difficulty: Easy
The base-2 numeral system is a positional notation with a radix of 3.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Large Variable content size caveats?

Hi, I wrote a shell script, which let me manage dns records through an api. The raw core-command looks like about this: output="$(curl -X GET https://mgt.myserver.de:8081/api/v1/servers/localhost/zones)"The output contains a list of all zones with all records and is about 800 Kilobytes... (9 Replies)
Discussion started by: stomp
9 Replies

2. UNIX for Advanced & Expert Users

Physical disk IO size smaller than fragment block filesystem size ?

Hello, in one default UFS filesystem we have 8K block size (bsize) and 1K fragmentsize (fsize). At this scenary I thought all "FileSytem IO" will be 8K (or greater) but never smaller than the fragment size (1K). If a UFS fragment/blocksize is allwasy several ADJACENTS sectors on disk (in a ... (4 Replies)
Discussion started by: rarino2
4 Replies

3. UNIX for Advanced & Expert Users

size for sum variable limitation on awk

Hello first, truth been told, I'm not even close to be advanced user. I'm posting here because maybe my question is complicated enough to need your expert help I need to use awk (or nawk - I don't have gawk) to validate some files by computing the total sum for a large numeric variable. It... (1 Reply)
Discussion started by: cwitarsa
1 Replies

4. Shell Programming and Scripting

The scripts not able to make the file to size 0, every times it go back to its original size

#!/bin/sh ########################################################################################################## #This script is being used for AOK application for cleaning up the .out files and zip it under logs directory. # IBM # Created #For pdocap201/pdoca202 .out files for AOK #1.... (0 Replies)
Discussion started by: mridul10_crj
0 Replies

5. UNIX for Dummies Questions & Answers

size of number variable in unix (awk)

Hi All, M writing a shell scripting n making use of awk. I am using a integer variable named size. When printing it's value, it is giving result as 4.34534e+10. Is there any way to declare variable of type LONG in awk or in shell scripting in general ? Regards, Abhishek Gera (3 Replies)
Discussion started by: AbhishekG
3 Replies

6. UNIX for Dummies Questions & Answers

Variable Size Problem

Hello Everyone, I am currently working with a DB2 (ver 8.1) table (Newly Created) and I am running a basic DB2 Select query into a variable. The field and it's size are VARCHAR(20). Since I need to pass the variable through some java I have noticed that when the results come back from the query... (0 Replies)
Discussion started by: Bob_Loblaw
0 Replies

7. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

8. AIX

dev/rmt 0.1 block size is 0; variable; must be 1024 fixed.

Hi. After the shutdown for SWIFT Alliance Server, tape backup process will be done. Unfortunately, I encountered this error message when I performed the database tape backup. The error was "/dev/rmt 0.1 block size is 0; variable; must be 1024 fixed. Consider reconfiguration through 'chdev'... (2 Replies)
Discussion started by: MariaLuisa
2 Replies

9. AIX

Variable Testing for size

How do you test a variable size to know if it is empty or not? Thanks (4 Replies)
Discussion started by: jango
4 Replies

10. Shell Programming and Scripting

Max size of variable

What is the maximum amount of characters that you can have in a varible name in the ksh shell? (1 Reply)
Discussion started by: lesstjm
1 Replies

Featured Tech Videos