LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-05-2011, 01:38 PM   #1
MODYSAMA
Member
 
Registered: Dec 2010
Posts: 144

Rep: Reputation: 0
Question Script Programing GetFolderSize


hi,
If I want to use that result to be saved in a variable, which type would it be in script?

For clearing:
let variable x and destination path is y/z


in prog.sh:
x=du -hb /y/z
echo $x
Quote:
Q1: What type of x?
Q2: What option instead of -hb to presedent the size in kib?

Linux beginner.

Last edited by MODYSAMA; 02-05-2011 at 03:15 PM.
 
Old 02-05-2011, 02:36 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
x="$( du -hb /y/z )"  # $() is used for embedded commands in most shells.
echo "$x"  #Needs to be quoted to protect the display of newlines.
A1. Shell variables don't have any defined type by default. How they are treated depends on their content and the context they are used in. But there are usually switches you can use when declaring them to give them certain attributes, such as integers, array variables, or lowercase- or uppercase-only. See man bash or the documentation of your shell for details.

A2. According to the du man page:
Code:
Display  values  are  in  units  of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE,  BLOCK_SIZE  and  BLOCKSIZE  environment  variables.
Otherwise,  units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

SIZE  may be (or may be an integer optionally followed by) one of following: KB
1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.
So du -h -B K seems to work.
 
1 members found this post helpful.
Old 02-06-2011, 07:36 AM   #3
MODYSAMA
Member
 
Registered: Dec 2010
Posts: 144

Original Poster
Rep: Reputation: 0
Question

Thank you.
Code:
# Checking folder size
emp=4096
#declare -i k
k="$( du -b )"
echo "$k"
if [ "$k" -gt "$emp" ]; then
echo "not empty Folder"
else
echo "empty Folder"
fi
Error:
Quote:
sok@sok-HP-ProBook-4520s:~/Scripts$ bash ./GetFolderSize.sh
5535 .
./GetFolderSize.sh: line 6: [: 5535 .: integer expression expected
empty Folder
Q: How is casting the size into integer,please?
 
Old 02-06-2011, 08:26 AM   #4
goodhombre
Member
 
Registered: Mar 2010
Location: Ungheni, Rep. Moldova
Distribution: Ubuntu
Posts: 84

Rep: Reputation: 21
Hi,

Try :
Code:
# Checking folder size
emp=4096
#declare -i k
k=$( du -b |awk '{print $1}')
echo "$k"
if [ $k -gt $emp ]; then
echo "not empty Folder"
else
echo "empty Folder"
fi
The issue was that du -b returns the name o directory too ("." - current directory )
Code:
/home/calin/scripts$k=$(du -b)
echo "k = $k"
k = 4531	.
 
1 members found this post helpful.
Old 02-06-2011, 08:36 AM   #5
MODYSAMA
Member
 
Registered: Dec 2010
Posts: 144

Original Poster
Rep: Reputation: 0
Many thanks. It works great.
Could you explain, what is the role of |awk '{print $1}', please?
 
Old 02-06-2011, 09:10 AM   #6
goodhombre
Member
 
Registered: Mar 2010
Location: Ungheni, Rep. Moldova
Distribution: Ubuntu
Posts: 84

Rep: Reputation: 21
Hi,

It prints (selects) only 1's column.
 
1 members found this post helpful.
Old 02-06-2011, 10:09 AM   #7
MODYSAMA
Member
 
Registered: Dec 2010
Posts: 144

Original Poster
Rep: Reputation: 0
Thank you, goodhombre!
 
Old 02-06-2011, 12:12 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
If the directory contains subdirectories, then du will return multiple lines and break the integer test. Try adding the -s option.

And instead of using awk, you can use bash's parameter substitution to strip off the unwanted parts of the variable string. I also think it would be a good idea to explicitly pass the directory you want to test to du.
Code:
# Checking folder size
emp=4096
tdir="${1:-$PWD}"  #use present working directory if no parameter given
shopt -s extquote

k=$( du -sb "$tdir" )
k="${k%%$'\t'*}"

echo "$k"

if [ "$k" -gt "$emp" ]; then
   echo "not empty Folder"
else
   echo "empty Folder"
fi
The output of du is tab-delimited, so you need to use $'\t' to insert a literal tab character in the script. This means you also need the extquote shell option enabled. However, the [:space:] regex character class could be used instead:
Code:
k="${k%%[[:space:]]*}"
It's also usually a good idea to quote variables inside single-bracket tests.

But if all you want to do is check whether a folder is empty or not, there are other ways to go about it. See here for an example that doesn't rely on any external tools: http://mywiki.wooledge.org/BashFAQ/004

Google should turn up many more.
 
1 members found this post helpful.
Old 02-07-2011, 07:01 AM   #9
MODYSAMA
Member
 
Registered: Dec 2010
Posts: 144

Original Poster
Rep: Reputation: 0
Thank you.
 
Old 02-07-2011, 07:45 AM   #10
MODYSAMA
Member
 
Registered: Dec 2010
Posts: 144

Original Poster
Rep: Reputation: 0
Excuse me,
Q1: Could you explain in details the following cmd:
Quote:
k="${k%%$'\t'*}
Q2: When to use Parenthesis {} and when $(())?
 
Old 02-07-2011, 08:59 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The basic pattern is explained in the parameter substitution link I gave you. The variable's contents are modified before expansion. With ${variable%%pattern}, it removes the longest section that matches the pattern from the end of the string that variable contains.

$'' is a quoting pattern that will expand backslash-escaped special characters like \n (newline) and \t (tab) into their literal equivalents. In order to be used inside of parameter substitution, the extquote shell option needs to be enabled.

So ${k%%$'\t'*} will match and remove the first tab it finds and everything following it. Only the number will be left. Then we just use that pattern to reset the variable to the new value.

I'm not sure I understand the second question exactly. Brackets, "{ }" are usually used to group commands together (without creating a subprocess), but there are other uses for them as well, such as brace expansion. Single parentheses, "( )" can also be used to group commands together, but it runs them in a subshell. "$(( ))" is a substitution pattern for arithmetical operations, just as "$( )" is the substitution pattern for command operations. Try running echo "$(( 1 + 1 ))".

I think you really need to study more about scripting on your own.
Here are a few useful bash scripting references:
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
 
1 members found this post helpful.
Old 02-07-2011, 02:01 PM   #12
MODYSAMA
Member
 
Registered: Dec 2010
Posts: 144

Original Poster
Rep: Reputation: 0
David the H. Thank you.
Thanks I need that as I said am just a Linux biginner.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Script question: Shell script in kde to log in on a server with ssh c4719929 Programming 18 01-31-2011 10:26 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 06:39 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration