Skip to main content

Unix Briefing



Variables

x=3
y='Hello'

Note: there should be no space in assigning variables.

Print Variables:

echo $x
echo $y

More accurate printing variables is double quote your variables

echo "$x"
echo "$y"

You can print literal also with variables.

echo "Value of variable x is $x"


If  else statement

x=5
y=7
if [[ $x -lt $y ]]; then
echo "true"
else
echo "false"
fi


Note : For good practice, put condition in [[...]]. There is semi-colon ; after condition end. fi command indicates closing of if else statements.

For comparison, use -lt,-gt, -le, -ge comparison parameters for good practice


if ((x>y)); then --- this also works -- Try yourself

Nested if else can be done with using elif command

x=cool
if [ "$x" = "cool" ]
then
    echo "Cool Beans"
elif [ "$x" = "neat" ]
then
    echo "Neato cool"
else
    echo "Not Cool Beans"
fi


Notice above if and else also works


For loop 

for <<counter_variable>> in <<input_variable>>
do
//perform action
Done

Few examples:

for i in 1 2 3 4 5
do
echo "$i"
done

x="foo boo coo doo moo"
for z in $x
do
echo "$z"
done

for i in {1..5}
do
echo "$i"
done

Note : For loop start with for command, then do and ends with done




While loop

a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

Note: Same as for loop. Action performed between do and done command



Function in Unix

hello()
{
echo “Hello, World”
}

hello    --- command to excute – just type function name

Output: Hello, World

If you have to pass arguments in Function and return the output.

Sum()
{
return `expr $1 + $2`
}
Sum 2 5  ---- arguments passed
echo “Result of sum function is $?”  -- value is captured by $?

Output:
Result of sum function is 7

Note :
Arguments pass after calling function and separated with spaces.
$1 $2 $3 indicates the arguments you pass to function

Return value is captured by $?



Run script and passing arguments to it

You can run script by following command

./script_name.sh

You can pass the arguments also to script while which can be access through $1 $2… $n value.
In Arguments, you can pass any literal value or file also.
./script_name.sh hello foo boo test.sh

Here
$1 = hello
$2 = foo
$3 = boo
$4 = test.sh

You can read file using below snippet or you can access by other means also.

echo $4
while read line
do
echo $line
done < $4


After reading file you can perform multiple operation


$# gives the no of arguments passed to the script.

In above example, $# gives "4" value

$@ give the list of all arguments passed to the script.

In above example, $@ gives "hello foo boo test.sh" value

$0 gives the path of the filename from where file got executed.

Suppose you have directory /tmp/examples/script

under script folder you have script test.sh

test.sh
echo $0
echo `dirname $0`

if you run this script from tmp folder
./examples/script/test.sh

Output:

./example/script/test.sh
./example/script

one more example : 

dir="/from/here/to/there.txt"
dir="$(dirname $dir)"   # Returns "/from/hear/to"

dir="$(basename $dir)"  # Returns just "to"


Create file through Touch command

Touch:
          If file is already existing, it will update the time stamp.
          If the file does not exist, then 'touch file_name' creates a new file with 0 KB file size. 

touch demo.sh   --- This will create file with name demo.sh
Once created, open in vi editor to write the code snippet using shift+I command.

vi demo.sh

Once done with changes. Press esc (it will come out of insert mode) and then type
:wq to save and exit from file.

Give permission to the file

Chmod 777 demo.sh

Run file


./demo.sh  (dot slash file name)



Delete the content of file

 > script_name.sh  
This will delete the contents of file



Refer one script file from another script file

Two script file : Square.sh and Circle.sh

Square.sh
Message= “ today is great day”

In order to refer Square file in Circle file, you can use below commands

. Square.sh   (dot space script_name)
source Sqaure.sh


Circle.sh
echo “ I am in circle file”
. Square.sh
echo “$Message”



or

Circle.sh
echo “ I am in circle file”
source  Square.sh
echo “$Message”

Run Circle file

./Circle.sh
Output :

I am in circle file
 today is great day



Note : even you can refer functions of Square file.

Comments

Popular posts from this blog

Tune your JDeveloper 12c (12.1.3)

Is your JDeveloper 12c too slow, follow these 3 basic steps and increase perfomance of JDeveloper 12c  (12.1.3) Step 1:  Configure JVM settings in jdev.conf Path:  $MV_HOME$/jdeveloper/jdev/bin/jdev.conf # optimize the JVM for strings / text editing AddVMOption -XX:+UseStringCache AddVMOption -XX:+OptimizeStringConcat AddVMOption -XX:+UseCompressedStrings # if on a 64-bit system, but using less than 32 GB RAM, this reduces object pointer memory size AddVMOption -XX:+UseCompressedOops # use an aggressive garbage collector (constant small collections) AddVMOption -XX:+AggressiveOpts # for multi-core machines, use multiple threads to create objects and reduce pause times AddVMOption -XX:+UseConcMarkSweepGC AddVMOption -DVFS_ENABLE=true AddVMOption -Dsun.java2d.ddoffscreen=false AddVMOption -XX:+UseParNewGC AddVMOption -XX:+CMSIncrementalMode AddVMOption -XX:+CMSIncrementalPacing AddVMOption -XX:CMSIncrementalDutyCycleMin=0 AddVMOption -XX:CMSIncrementalDutyCycle=10 Step 2:

How to use Log4j logger in Simple few steps

Steps to follow : 1. Download Log4j Jar file  and add in Project Lib Path.        http://logging.apache.org/log4j/1.2/download.html 2. Create Log4j.properties file in your project and add below details.      log4j.rootLogger=DEBUG, Appender1,Appender2 log4j.appender.Appender1=org.apache.log4j.ConsoleAppender log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n log4j.appender.Appender2=org.apache.log4j.FileAppender log4j.appender.Appender2.File= applog.txt log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n 3. In the main method of Java program add below code.         Logger logger = Logger.getLogger(Conn.class);          String log4jConfigFile = System.getProperty("user.dir") + File.separator + "log4j.properties";         PropertyConfigurator.configure(log4jConfigFile);  

CHANGING THE INBOUND FILE ADAPTER ARCHIVE FILE NAME IN SOA

Default archive name will be filename_ digest _timestamp. This format was introduced with SOA 11g. To configure the File Adapter archive filename to use this format: filename_yyyymmdd_hh24mmss we have to use property “UseDigest” in JCA file. By default “UseDigest” is set to true, which tells the Inbound File Adapter that the archive file name format will be the default: filename_digest_timestamp. When the “UseDigest” property is set to false, the filename format will revert to: filename_timestamp. Modify your File Inbound JCA file, and add the following property: <property name=”UseDigest” value=”false”/>