My Shell Script Usage Collection

sgadmin created the topic: My Shell Script Usage Collection
Count Total number of files in Directory and Subdirectory
> find . -type f | wc –l

Count Specific extention files in Directory and Subdirectory
> find . -type f -name \*.mnp |wc –l

Count only Directory
> find . -type d | wc –l

Tagged :

How to read a .properties file through script

rajeshkumar created the topic: How to read a .properties file through script

The easy way to do it is to note that a Java properties file has the same format as a basic shell script.

However, there’s a trick to it. If you just run the properties file like so:

sh appl.properties

The assignments will be made at the sub-level, then discarded when the properties file (script) ends execution.

So to get the properties in a calling script, you need to use the “source” command:

. appl.properties

Note that the space after the initial dot is very important!

To reference shell variable assignments, you use the “$” to indicate variable substitution.

So, to put it all together:

# #!/bin/sh
# # Sample shell script to read and act on properties
#
# # source the properties:
# . appl.properties
#
# # Then reference then:
# echo "My name is $name and I'm $age years old."

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Shell script to run x times

rajeshkumar created the topic: shell script to run x times
Problem:

First i need to cd to this directory $SWDIR/util
Second i need to run the following either 4 times or 20 times

./swadm add_process 1 BG Y

how can i put this in a script

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re:shell script to run x times
Solution

# Change directory
cd $SWDIR/util

# Set the variable n, which should store the number of iterations,
# either to 4 when condition "bla_bla" or else to 20
if bla_bla then
n=4
else
n=20
fi

# loop n times and do something in that loop
# (initialize the variable i with 1, then loop as long as i is
# smaller than or equal the variable n. increase i by 1 on
# every looping)
for (( i=1; i<=$n; i++ )) do ./swadm add_process 1 BG Y done

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Whats the difference between running a shell scrip

scmuser created the topic: Whats the difference between running a shell scrip
Whats the difference between running a shell script as ./script.sh and sh script.sh

have a script that looks like this

#!/bin/bash

function something() {
echo “hello world!!”
}

something | tee logfile

I have set the execute permission on this file and when I try running the file like this

$./script.sh

it runs perfectly fine, but when I run it on the command line like this

$sh script.sh

It throws up an error. Why does this happen and what are the ways in which I can fix this.

scmuser replied the topic: Re:Whats the difference between running a shell scrip
Running it as ./script.sh will make the kernel read the first line (the shebang), and then invoke bash to interpret the script. Running it as sh script.sh uses whatever shell your system defaults sh to (on Ubuntu this is Dash, which is sh-compatible, but doesn’t support some of the extra features of Bash).

You can fix it by invoking it as bash script.sh, or if it’s your machine you can change /bin/sh to be bash and not whatever it is currently (usually just by symlinking it – rm /bin/sh && ln -s /bin/bash /bin/sh). Or you can just use ./script.sh instead if that’s already working 😉

Tagged :

Perl commandline search and replace

rajeshkumar created the topic: Perl commandline search and replace

Perl commandline search and replace

The better option is using perl command line search and replace option. The syntax is the following.

# perl -i.bak -p -e’s/old/new/g’ filename
s/old/new/g

what this does is searches for pattern “old” and replace it pattern “new”.The g option in does the serch n replace globally in the file, otherwise perl searches and replaces only the first instance of the pattern.

Lets explain the options used.

-e option allows you to define Perl code to be executed by the compiler. For example, it’s not necessary to write a “Hello World” program in Perl when you can just type this at the command line.

# perl -e ‘print “Hello World\n”‘

-p option, adds loops around your -e code.It creates code like this:

LINE:
while (<>) {
# your code goes here
} continue {
print or die “-p destination: $!\n”;
}

-i option. Actually, Perl renames the input file and reads from this renamed version while writing to a new file with the original name. If -i is given a string argument, then that string is appended to the name of the original version of the file. For example, to change all occurrences of “PHP” to “Perl” in a data file you could write something like this:

# perl -i -pe ’s/PHP/Perl/g’ file.txt

Perl reads the input file a line at a time, making the substitution, and then writing the results back to a new file that has the same name as the original file — effectively overwriting it. If you’re not so confident of your Perl abilities you might take a backup of the original file, like this:

# perl -i.bak -pe ’s/PHP/Perl/g’ file.txt

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Unix command to find memory usage

rajeshkumar created the topic: unix command to find memory usage

Monitor processes continuously. Updates every 3 seconds by default. Displays useful information such as total memory, memory in use, % of total memory used by each process, % cpu usage of each process, and process ID (PID) of each process. Type ‘h’ for help or ‘q’ to quit. To stop (‘kill’) a process, type ‘k’ and enter the PID of the process, then enter the signal for the ‘kill’ command (see below) or press Enter to use the default signal (terminate).

top
Useful options:
-d Specify time between updates
-u List processes for user
-p List process with ID
-n Specify number of iterations before quitting

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Shell script merge two list and remove duplicates

rajeshkumar created the topic: shell script merge two list and remove duplicates

You want all the records from list_A supplemented by all the records from list_B for which there is not already a matching name in list A. Mathematically this is:

A + B - {w in B | (w,value) in A }

\

There are many ways of accomplishing this, depending on access and needed efficiencies.

* If you can modify DB1 (with A), then download table B from DB2, upload it to DB1, then extract your data with the appropriate join
* If you can’t modify DB1, then download both A and B and concatenate them to the same stream, with A followed by B. Then sort by the first field. Then process the stream one record at time. Duplicate names will be side-by-side. If the same name appears more than one time, print the first and ignore subsequent records with the same name.

Here is a sample solution to your problem (starting with two lists of names/values)

#!/bin/bash

A="Smith value1
Jones value2
Wilson value3"

B="Smith value10
Wilson value11
Fox value12
Brown value13"

PrevName="Not a valid name"
echo "$A
$B" | sort -k1 |
while read Name Value
do
if [ "$Name" != "$PrevName" ]; then
echo $Name $Value
fi
PrevName="$Name"
done > outfile

You want all the records from list_A supplemented by all the records from list_B for which there is not already a matching name in list A. Mathematically this is:

A + B – {w in B | (w,value) in A }

There are many ways of accomplishing this, depending on access and needed efficiencies.

* If you can modify DB1 (with A), then download table B from DB2, upload it to DB1, then extract your data with the appropriate join
* If you can’t modify DB1, then download both A and B and concatenate them to the same stream, with A followed by B. Then sort by the first field. Then process the stream one record at time. Duplicate names will be side-by-side. If the same name appears more than one time, print the first and ignore subsequent records with the same name.

Here is a sample solution to your problem (starting with two lists of names/values):

#!/bin/bash

A=”Smith value1
Jones value2
Wilson value3″

B=”Smith value10
Wilson value11
Fox value12
Brown value13″

PrevName=”Not a valid name”
echo “$A
$B” | sort -k1 |
while read Name Value
do
if [ “$Name” != “$PrevName” ]; then
echo $Name $Value
fi
PrevName=”$Name”
done > outfile

Here is the output:
Brown value13
Fox value12
Jones value2
Smith value1
Wilson value11
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Shell script for recursive delete required.

rajeshkumar created the topic: Shell script for recursive delete required.

#!/bin/bash

if [ $# -ne 1 -o ! -d "$1" ]
then
echo "Usage $0 dirname"
exit 1
fi

find "$1" -type f -print | while read file
do
dir=$(dirname "$file")
dname=$(basename "$dir")
fname=$(basename "$file")
[ "$dname" = "$fname" ] && rm "$file"
done

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Find Application Instance Directory In Unix

scmuser created the topic: find application instance directory in unix

How to find installed application directory in unix?

rajeshkumar replied the topic: Re:find application instance directory in unix

use Which command

I mean

which java
which perl
which ant
which p4
which svn

You will get location of the apps installed in your machine.
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re:find application instance directory in unix
To search for anything with the terminal, you can either use the commands locate or which.
Locate is used more like a keyword-based search function. For example, if you type in a terminal:

locate java

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Shell Script to delete a folder that is more then

scmuser created the topic: Shell Script to delete a folder that is more then
Shell Script to delete a folder that is more then 1 week old

Hi all,
i have this scritp
set -x
cd /var/log/
direc=”bkp_`date +%F`”
mkdir $direc
cp /var/log/*.log /tmp/bkp.

Here im ok, what i need is a scritp to check if i have a folder that is more then 1 week to delete it.
Any help please?

Tagged :