Showing posts with label debian6. Show all posts
Showing posts with label debian6. Show all posts

Sunday, November 25, 2012

Linux Environment Variables by using export or not


Using export and not using export - environment variables

http://lowfatlinux.com/linux-environment-variables.html

code=$HOME/projects/src/spew
and then, regardless of what directory you are in, you can issue
cd $code
to pop over quickly to the directory containing the source code for that way-cool spew program you're developing. (The cd command means "change directory.")
A variable assignment like this will work just fine, but its scope (visibility) is limited to the current shell. If you launch a program or enter another shell, that child task will not know about your environment variables unless you export them first.
Unless you know for sure that an environment variable will have meaning only in the current shell, it's a good idea to always useexport when creating variables to ensure they will be global in scope--for example,
export PS1="\u \$ "
export code=$HOME/projects/src/spew
And be sure to add these commands to your .profile file so you won't have to retype them eac h time you log in.



http://en.wikibooks.org/wiki/Guide_to_Unix/Environment_Variables
http://en.wikipedia.org/wiki/Environment_variable


http://stackoverflow.com/questions/1158091/bash-defining-a-variable-with-or-without-export


export makes the variable available to sub-processes.
That is,
export name=value
means that the variable name is available to any process you run from that shell process. If you want a process to make use of this variable, use export, and run the process from that shell.
name=value
means the variable scope is restricted to the shell, and is not available to any other process. You would use this for (say) loop variables, temporary variables etc.


Specifically export makes the variable available to child processes via the environment. – Beano Jul 21 '09 at 13:35







Thursday, November 1, 2012

linux shell script how to check environment variable exist (and define a default if it doesn't)


linux shell script how to check environment variable exist - Google Search

Scenario: You have a long command line e.g.
python sth.py --asdf $asdf --qwer $qwer --zxcv $zxcv

and you're getting the $xxxx from somewhere else.. but it could be empty..
in which case all your --xxxx get --yyyy as their arguments... not good...



linux shell script how to check environment variable exist (and define a default if it doesn't)
- 2 syntax (OLD, NEW)



http://www.cyberciti.biz/faq/bash-ksh-if-variable-is-not-defined-set-default-variable/

output=${1-text}
echo $output

Common mistake:
$output=${1-text}
If you do this, errmsg is := not defined not found etc. (becoz left hand side is blank)
Correct is this
output=${asdf-text}
or
output=${output-text}
Note, without the $ sign in front


Or, Syntax 2:

OR (see my comment below):
output=${1:-text}
echo $output




Vivek Gite July 27, 2009 at 7:59 am
@Ronald,
The original Bourne shell only supported above syntax and it works with all shells to keep portability. POSIX shells (KSH and BASH) offer a slight variant (as mentioned in bash man page):
${1:-text}
I should have mentioned both syntax..
HTH




Ronald Fischer July 27, 2009 at 7:11 am
It surprises me that this works (and, at least, on bash 3, it does). I have always used the form
${VAR:-VALUE} in such a case, not ${VAR-VALUE}, but it seems that both work. The man pages of bash describe, however, ONLY the variant with a colon, so I wonder whether omitting the colon just exploits an undocumented feature, which might be gone with the next version of bash. Or did I miss here something in ‘man bash’?




Shell Scripting: If Variable Is Not Defined, Set Default Variable



If var is defined AND NOT EMPTY, use var, otherwise set a default variable under Bash. For e.g. my script needs a parameter for output variable. It can be text or html. I set it as follows in my script







2
http://stackoverflow.com/questions/2981878/bash-checking-for-environment-variables

In Bash (and ksh and zsh), if you use double square brackets you don't need to quote variables to protect against them being null or unset.
$ if [ $xyzzy == "x" ]; then echo "True"; else echo "False"; fi
-bash: [: ==: unary operator expected
False
$ if [[ $xyzzy == "x" ]]; then echo "True"; else echo "False"; fi
False
There are other advantages.






3: useful
What's the best way to check that environment variables are set in Unix shellscript
http://stackoverflow.com/questions/307503/whats-the-best-way-to-check-that-environment-variables-are-set-in-unix-shellscr

Your question is dependent on the shell that you are using.
Bourne shell leaves very little in the way of what you're after.
BUT...
It does work, just about everywhere.
Just try and stay away from csh. It was good for the bells and whistles it added, compared the Bourne shell, but it is really creaking now. If you don't believe me, just try and separate out STDERR in csh! (-:
There are two possibilities here. The example above, namely using:
${MyVariable:=SomeDefault}
for the first time you need to refer to $MyVariable. This takes the env. var MyVariable and, if it is currently not set, assigns the value of SomeDefault to the variable for later use.
You also have the possibility of:
${MyVariable:-SomeDefault}
which just substitutes SomeDefault for the variable where you are using this construct. It doesn't assign the value SomeDefault to the variable, and the value of MyVariable will still be null after this statement is encountered.
HTH.
cheers,
Rob








Friday, October 19, 2012

how to tar an archive without getting the intermediate directories?

how to tar an archive without getting the intermediate directories?

http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/

tar czf filename.tar.gz somefolder/
tar xzf filename.tar.gz





3
How I did it in my jenkins shell script:


cd importtool/src/ && python get_latest_schema.py
cd ../../ && tar cvzf importtool.tar.gz -C importtool/ src/


What this does is, tells:
1) tar to -C change the current directory to importtool/, and then tar its file/folder 'src/'.
2) since the working dir is / , the file importtool.tar.gz comes out at the same level as importtool/







2
Prevent parent directories from being tarred
http://stackoverflow.com/questions/4031416/prevent-parent-directories-from-being-tarred

How do I tar a directory of files and folders without including the directory itself?
http://stackoverflow.com/questions/939982/how-do-i-tar-a-directory-of-files-and-folders-without-including-the-directory-it

Q

Basically I just want to tar all the files in a directory, but not get all the parent directories in the archive.
I've tried -C, but I guess I'm not using it right.
tar -cjf archive.tar.bz2 -C /var/some/log/path ./*
This results in tar trying to add all the files in the CWD. Using the full path as last argument doesn't prevent the dirs from being added.
Seems simple enough, but can't figure it out. Somehow tar does not tar ./* as being relative to -C, although it should change to that dir.
Help appreciated.

A

The parent directory (/var/some/log) is included, since /var/some/log/path/.. is included when you do ./*. Try just doing
tar -cjf archive.tar.bz2 -C /var/some/log/path .
Test run:
$ find tmp/some_files
tmp/some_files
tmp/some_files/dir1
tmp/some_files/dir1/dir1file
tmp/some_files/hello
tmp/some_files/world
tmp/some_files/dir2
tmp/some_files/dir2/dir2file
$ tar -cvjf archive.tar.bz2 -C tmp/some_files/ .
./
./dir1/
./dir1/dir1file./hello./world./dir2/
./dir2/dir2file
$ cd tmp/unpacked/tmp/unpacked$ mv /home/aioobe/archive.tar.bz2 .
/tmp/unpacked$ tar -xvjf archive.tar.bz2 ./
./dir1/
./dir1/dir1file./hello./world./dir2/
./dir2/dir2file/tmp/unpacked$ ls
archive.tar.bz2  dir1  dir2  hello  world/tmp/unpacked$ 

That's somewhat better, but now it ads "." as a parent directory in the tar. I want only the files. – Joe Oct 27 '10 at 16:25
No, the "./" prefix is just notation. It contains the files in the "root" of the archive. – aioobe Oct 27 '10 at 16:51
Ah. I see. My bad. – Joe Oct 27 '10 at 17:01



Q

I typically do:
tar -czvf my_directory.tar my_directory
What if I just want to include everything (including any hidden system files) in my_directory, but not the directory itself? I don't want:
my_directory
   --- my_file
   --- my_file
   --- my_file
I want:
my_file
my_file
my_file


A1

cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd .. 
should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:
$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2

This will also work on files with spaces or other special characters. Good job! – PanCrit Jun 3 '09 at 20:45

A2

Use the -C switch of tar:
tar -czvf my_directory.tar.gz -C my_directory .
The -C my_directory tells tar to change the current directory to my_directory, and then .means "add the entire current directory" (including hidden files and sub-directories).

+1 thank you! It was the damn '.' I was missing. so aggravating – JCotton May 5 '11 at 2:08

Friday, October 12, 2012

set +x = @ECHO OFF, set -x = @ECHO ON.

in linux. Nuff said.

Linux shell scripting: If-ELSE, comparison operators syntax, [semicolon ] ;

how to write if else in a shell script?

http://www.freeos.com/guides/lsst/ch03sec02.html

http://www.freeos.com/guides/lsst/ch03sec03.html


http://codewiki.wikidot.com/shell-script:if-else




Shell scripts use fairly standard syntax for if statements. The conditional statement is executed using either the test command or the [ command. In its most basic form an if statement is:
#!/bin/bash

if [ "$1" -eq "cool" ]
then
    echo "Cool Beans"
fi
(Notice that the fi is simply if spelled backwards). To add an else, we just use standard syntax.
#!/bin/bash

if [ "$1" -eq "cool" ]
then
    echo "Cool Beans"
else
    echo "Not Cool Beans"
fi
Adding an else-if statement structure is used with the elif command.
#!/bin/bash

if [ "$1" -eq "cool" ]
then
    echo "Cool Beans"
elif [ "$1" -eq "neat" ]
    echo "Neato cool"
else
    echo "Not Cool Beans"
fi
An if statement does not require two parameters. You can use single flags as well. The following code tests to see if the first parameter is a file or not.
#!/bin/bash

if [ -f "$1" ]
then
    echo "$1 is a file"
else
    echo "$1 is not a file"
fi
There are many different ways that an conditional statement can be used. These are summarized here:
String Comparison Description
Str1 = Str2 Returns true if the strings are equal
Str1 != Str2 Returns true if the strings are not equal
-n Str1 Returns true if the string is not null
-z Str1 Returns true if the string is null
Numeric Comparison Description
expr1 -eq expr2 Returns true if the expressions are equal
expr1 -ne expr2 Returns true if the expressions are not equal
expr1 -gt expr2 Returns true if expr1 is greater than expr2
expr1 -ge expr2 Returns true if expr1 is greater than or equal to expr2
expr1 -lt expr2 Returns true if expr1 is less than expr2
expr1 -le expr2 Returns true if expr1 is less than or equal to expr2
! expr1 Negates the result of the expression
File Comditionals Description
-d file True if the file is a directory
-e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
-f file True if the provided string is a file
-g file True if the group id is set on a file
-r file True if the file is readable
-s file True if the file has a non-zero size
-u True if the user id is set on a file
-w True if the file is writable
-x True if the file is an executable






test command or [ expr ]

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]

Example:
Following script determine whether given argument number is positive.
$ cat > ispostive
#!/bin/sh
#
# Script to see whether argument is positive
#
if test $1 -gt 0
then
echo "$1 number is positive"
fi




2
################


 For Mathematics, use following operator in Shell Script
Mathematical Operator in  Shell Script MeaningNormal Arithmetical/ Mathematical StatementsBut in Shell
   For test statement with if commandFor [ expr ] statement with if command
-eqis equal to5 == 6if test 5 -eq 6if [ 5 -eq 6 ]
-ne is not equal to5 != 6if test 5 -ne 6if [ 5 -ne 6 ]
-lt is less than5 < 6if test 5 -lt 6if [ 5 -lt 6 ]
-le is less than or equal to5 <= 6if test 5 -le 6if [ 5 -le 6 ]
-gt is greater than5 > 6if test 5 -gt 6if [ 5 -gt 6 ]
-ge is greater than or equal to5 >= 6if test 5 -ge 6if [ 5 -ge 6 ]

NOTE: == is equal, != is not equal.
For string Comparisons use
OperatorMeaning
string1 = string2string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
string1 string1 is NOT NULL or not defined 
-n string1 string1 is NOT NULL and does exist
-z string1 string1 is NULL and does exist
Shell also test for file and directory types
TestMeaning
-s file   Non empty file
-f file   Is File exist or normal file and not a directory 
-d dir    Is Directory exist and not a file
-w file   Is writeable file
-r file    Is read-only file
-x file   Is file is executable
Logical Operators
Logical operators are used to combine two or more condition at a time
Operator            Meaning
! expression Logical NOT
expression1  -a  expression2 Logical AND
expression1  -o  expression2 Logical OR




##########################3


Nested if-else-fi

You can write the entire if-else construct within either the body of the if statement of the body of an else statement. This is called the nesting of ifs.
$ vi nestedif.sh
osch=0

echo "1. Unix (Sun Os)"
echo "2. Linux (Red Hat)"
echo -n "Select your os choice [1 or 2]? "
read osch

if [ $osch -eq 1 ] ; then

     echo "You Pick up Unix (Sun Os)"

else #### nested if i.e. if within if ######
           
       if [ $osch -eq 2 ] ; then
             echo "You Pick up Linux (Red Hat)"
       else
             echo "What you don't like Unix/Linux OS."
       fi
fi



http://www.freeos.com/guides/lsst/
http://www.dreamsyssoft.com/sp_ifelse.jsp


If/Else

In order for a script to be very useful, you will need to be able to test the conditions of variables. Most programming and scripting languages have some sort of if/else expression and so does the bourne shell. Unlike most other languages, spaces are very important when using an if statement. Let's do a simple script that will ask a user for a password before allowing him to continue. This is obviously not how you would implement such security in a real system, but it will make a good example of using if and else statements.

#!/bin/sh
# This is some secure program that uses security.

VALID_PASSWORD="secret" #this is our password.

echo "Please enter the password:"
read PASSWORD

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
 echo "You have access!"
else
 echo "ACCESS DENIED!"
fi
Remember that the spacing is very important in the if statement. Notice that the termination of the if statement is fi. You will need to use the fi statement to terminate an if whether or not use use an else as well. You can also replace the "==" with "!=" to test if the variables are NOT equal. There are other tokens that you can put in place of the "==" for other types of tests. The following table shows the different expressions allowed.

Comparisons:
-eqequal to
-nenot equal to
-ltless than
-leless than or equal to
-gtgreater than
-gegreater than or equal to

File Operations:
-sfile exists and is not empty
-ffile exists and is not a directory
-ddirectory exists
-xfile is executable
-wfile is writable
-rfile is readable

Let's try using a couple of these in a script. This next script will ask for a user name, if there is not a file that exists with the name "username_DAT", the script will prompt the user for their age, it will then make sure that they are old enough to use this program and then it will write their age to a file with the name "username_DAT". If the file already exists, it will just display the age of the user.

#!/bin/sh

# Prompt for a user name...
echo "Please enter your name:"
read USERNAME

# Check for the file.
if [ -s ${USERNAME}_DAT ]; then
        # Read the age from the file.
        AGE=`cat ${USERNAME}_DAT`
        echo "You are $AGE years old!"
else
        # Ask the user for his/her age
        echo "How old are you?"
        read AGE

 if [ "$AGE" -le 2 ]; then
  echo "You are too young!"
 else 
  if [ "$AGE" -ge 100 ]; then
   echo "You are too old!"
  else
          # Write the age to a new file.
          echo $AGE > ${USERNAME}_DAT
         fi
        fi
fi
Run this program a couple of times. First run it and give it the user name of "john". When it asks for an age, enter the age "1". Notice that it will say that you are too you and then exit. Now run the program again with the name "john" and the age 200. This time the script will tell you that you are too old and exit. Now run the the script again with the name of "john", enter the age 30. The script exits normally this time, the program created a file called "john_DAT" which contains the text "30". Finally run the program one more time and give it the name "john". This time it will not prompt you to enter an age, instead it will read the age from a file and say "Your are 30 years old!".
We introduced something else new in this script. On line 10 of the file, we see the code:

 AGE=`cat ${USERNAME}_DAT`
This is how you execute a command and put the text output from the command into a variable. The unix command cat reads the file named ${USERNAME}_DAT and outputs it to the console. Instead of putting it to the console in our script, we wrap the command with the character `, this puts the text into our variable AGE.

You can test multiple expressions at once by using the || (or) operator or the && (and) operator. This can save you from writing extra code to nest if statements. The above code has a nested if statement where it checks if the age is greater than or equal to 100. This could be changed as well by using elif (else if). The structure of elif is the same as the structure of if, we will use it in an example below. In this example, we will check for certain age ranges. If you are less than 20 or greater than 50, you are out of the age range. If you are between 20 and 30 you are in your 20's and so on.

#!/bin/sh

# Prompt for a user name...
echo "Please enter your age:"
read AGE

if [ "$AGE" -lt 20 ] || [ "$AGE" -ge 50 ]; then
 echo "Sorry, you are out of the age range."
elif [ "$AGE" -ge 20 ] && [ "$AGE" -lt 30 ]; then 
 echo "You are in your 20s"
elif [ "$AGE" -ge 30 ] && [ "$AGE" -lt 40 ]; then 
 echo "You are in your 30s"
elif [ "$AGE" -ge 40 ] && [ "$AGE" -lt 50 ]; then 
 echo "You are in your 40s"
fi




Wednesday, October 10, 2012

In Linux, how to know how much space all directories and sub-dirs are using?


In linux, how to know how much space all directories and sub-dirs are using?

du -h --max-depth=1





du -h --max-depth=2
if you want to show for the sub directories one level below, too.



how to know how much disk space a directory takes in unix
http://www.thewebhostinghero.com/tutorials/linux-disk-usage-command.html
http://www.cyberciti.biz/faq/check-free-space/


Sometimes it’s useful to be able to find out how much disk space a directory is taking. While there are some web interfaces or desktop applications that allows you to do that, it’s always useful to know how it can be done through a command shell.

The DU Command

To find out how much space is being used, you can use the DU (Disk Usage) command. Here a few examples of the DU command usage.
Listing a disk space summary of directory
# du -sh [directory]
Listing the disk space used for a directory and its subdirectories, sorted by disk usage
# du -h [directory] | sort -rn
Take note that the “h” switch allows displays the disk usage in human readable format (ie. kylobytes, megabytes, etc.).




Linux / Unix: Checking Free Disk Space

by  on AUGUST 16, 2007 · 28 COMMENTS· last updated at AUGUST 16, 2007
Q. How do I check free disk space in Linux or UNIX operating system? I've migrated from Windows NT to Linux and looking forward to get more information about free disk space.
A. Both Linux and UNIX offers two commands for checking out free disk space:
(a) df command : Report file system disk space usage
(b) du command : Estimate file space usage

df command examples - to check free disk space

Type df -h or df -k to list free disk space:
$ df -h
OR
$ df -k
Output:
Filesystem             Size   Used  Avail Use% Mounted on
/dev/sdb1               20G   9.2G   9.6G  49% /
varrun                 393M   144k   393M   1% /var/run
varlock                393M      0   393M   0% /var/lock
procbususb             393M   123k   393M   1% /proc/bus/usb
udev                   393M   123k   393M   1% /dev
devshm                 393M      0   393M   0% /dev/shm
lrm                    393M    35M   359M   9% /lib/modules/2.6.20-15-generic/volatile
/dev/sdb5               29G   5.4G    22G  20% /media/docs
/dev/sdb3               30G   5.9G    23G  21% /media/isomp3s
/dev/sda1              8.5G   4.3G   4.3G  51% /media/xp1
/dev/sda2               12G   6.5G   5.2G  56% /media/xp2
/dev/sdc1               40G   3.1G    35G   9% /media/backup
he df utility displays statistics about the amount of free disk space on the specified file system or on the file system of which file is a part. Values are displayed in 512-byte per block counts. -H option is called as "Human-readable" output. It use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to four or fewer using base 10 for sizes i.e. you see 30G (30 Gigabyte).

du command examples

du shows how much space one ore more files or directories is using.
$ du -sh
103M
-s option summarize the space a directory is using and -h option provides "Human-readable" output.

GUI program

Above programs are good if GUI is not installed or you are working with remote system over ssh. Linux / UNIX comes with KDE and Gnome desktop system. You will find Free Disk Space Applet located under GUI menus.