Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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
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 ))".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.