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 :

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 :

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 :

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 :

Shell Script Run in Background

rajeshkumar created the topic: shell script run in background
I wanted to run a particular shell program in a background and it should not be dependent on terminal session of putty or telnet?

Just finding solution?
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re:shell script run in background
i found 2 following solution for this..
./myscript&

You can prevent the signal from getting to the program by starting it like this:
nohup command &
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

How to Restore The Dump File using Shell Scripting

scmuser created the topic: how to restore the dump file using shell scripting

I have created a script file to dump the application files using the following script

Code
=============
#!/bin/bash
#Full Day Backup Script
#application folders backup

now=$(date +”%d-%m-%Y”)
#use 1 instead of 0 which is incremental backup
dump -0f $now /var/www/html/*
=============

using this we can successfully dumped the application files.

Now i want to restore the dumpfile

How can i restore using shell script?

please help

Thanx in advance

Tagged :

Shell Script to Find All New Files Added

scmuser created the topic: shell script to find all new files added

Hi,

i am looking for a shell script to find all new files added or modified after a certain date & Time??

Any help?

mnanjala replied the topic: Re:shell script to find all new files added
This should solve the problem

find . -type f -mtime -7 -print

Here,
* -mmin n (modification time in minutes)
* -mtime n (modification time in days)
* -newer file (modification time newer than modification time of file)
* -daystart (adjust start time from current time to start of day)
* Plus alternatives for access time and ‘change’ or ‘create’ time.

Or thinkikng

touch can be used

Tagged :

Shell Script Program 1

scmuser created the topic: shell script program 1

Device a script that takes a filename as argument ( which must exist in the current directory) and locates from your home directory tree all path names of its links. The list should be mailed to self

Tagged :