scmuser created the topic: Find absolute path name of a file
Use expr to extract the parent directory from the absolute path name of a file
Category: Shell Script
Find Last Modification Time of File
scmuser created the topic: find last modification time of file
Write a script that accepts a filename as argument and displays the last modification time if the file exist and a suitable message if it does not?
Make sure script is running using korn shell
scmuser created the topic: Make sure script is running using korn shell
If you have developed a script using the Korn shell, how will you make sure the script will use that shell even if login shell is different?
renjith replied the topic: Re: Make sure script is running using korn shell
begin shell script with Shabang/interpreter line
#!/bin/ksh
rajeshkumar replied the topic: Re: Make sure script is running using korn shell
There are 2 ways to run a script with specific shell no matter if login shell is different..
Method 1:
Use following before any shell script…
#!/bin/ksh
Method 2:
Execute shell script with ksh
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Identical file validation using shell script?
scmuser created the topic: identical file validation using shell script?
Device a script that accepts two directory names, bar1 and bar2, and deletes those files in bar2 which are identical to their namesakes in bar1.
rajeshkumar replied the topic: Re: identical file validation using shell script?
Solution:
#!/bin/bash
cd $1
for file in * ; do
if [ -f ../$2/$file ] ; then
cmp $file ../$2/$file >/dev/null 2>/dev/null && rm ../$2/$file
fi
done
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Useful Command for Process and Jobs in Unix/Linux
rajeshkumar created the topic: Useful Command for Process and Jobs in Unix/Linux
List out all background Job running in machine i.e. list current jobs
> jobs
How to sent Job into Background ie background the suspended job
> bg
How to run background jobs on foreground
1. Find out the job number using “jobs” command
2. Type follwoing fg %jobnumber
Whats the differnce between control c and control z in Linux
^C – kill the job running in the foreground
^Z – suspend the job running in the foreground
To kill a suspended or background process
> kill %jobnumber
To kill a job running in the foreground
> sleep 100
^C
List out all background job running by each user
ps -u
To see every process on the system using standard syntax:
> ps -ef
To see every process on the system using BSD syntax:
> ps ax
To print a process tree:
> ps -ejH
To get info about threads:
> ps -eLf
To see every process running as root (real & effective ID) in user format:
> ps -U root -u root u
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Unix command to Know some useful system Information
rajeshkumar created the topic: Unix command to Know some useful system Information
Unix command to Know some useful system Information
top show system stats and top CPU using processes
uptime show one line summary of system status
who who is on the system and what they are doing
more show text file on display terminal with paging control
uname Knowing machine characteristics such as OS, version etc..
Please add your collection…
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Find c file Recursivly and Copy to Directory
scmuser created the topic: find c file recursivly and copy to directory
Write a shell script to find c file recursivly and copy to one common directory?
rajeshkumar replied the topic: Re: find c file recursivly and copy to directory
following program will serve your purpose…
#!/bin/sh
counter=0
if [ $# -eq 0 ]
then
for file in `find ./os -name “*.c”`
do
cp $file /root/Release/Rajesh
echo $file
counter=`expr $counter + 1`
done
else
echo “No Argument passed”
fi
echo “Number of files found=$counter”
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Test your Shell Scripting Skills: – Program 5
scmuser created the topic: Test your Shell Scripting Skills: – Program 5
Write a script that behaves both in interactive and noninteractive mode. When no arguments are supplied, it picks up each C program from the current directory and lists the first 10 lines. it then prompts for deletion of the file. if the user supplies arguments with the script, then it works on those files only.
rajeshkumar replied the topic: Re: Test your Shell Scripting Skills: – Program 5
Following script should work….
#!/bin/sh
counter=0
if [ $# -eq 0 ]
then
for file in `find . -name "*.c"`
do
head -10 $file
counter=`expr $counter + 1`
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $file;;
n|N) exit ;;
*) echo "Invalid response"
esac
done
else
head -10 $1
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $1;;
n|N) exit ;;
*) echo "Invalid response"
esac
fi
echo "Number of files found=$counter"
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
rajeshkumar replied the topic: Re: Test your Shell Scripting Skills: – Program 5
#!/bin/sh
counter=0
if [ $# -eq 0 ]
then
for file in `find . -name "*.c"`
do
head -10 $file
counter=`expr $counter + 1`
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $file;;
n|N) exit ;;
*) echo "Invalid response"
esac
done
else
head -10 $1
echo "Do you want to delete this file=(Please type Y or N)"
read vote
case $vote in
Y|y) rm -f $1;;
n|N) exit ;;
*) echo "Invalid response"
esac
fi
echo "Number of files found=$counter"
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Test your Shell Scripting Skills: – Program 6
scmuser created the topic: Test your Shell Scripting Skills: – Program 6
Display a process in the system every 30 seconds five times using a(i) while loop (ii) for loop. What is the unusual feature of the for loop?
rajeshkumar replied the topic: Re: Test your Shell Scripting Skills: – Program 6
This should work….
#!/bin/bash
counter=0
while [ $counter -lt 5 ]
do
echo "Displaying running System process"
ps
echo "Displaying System Process and Top CPU using processes"
top
echo "Displaying One line Summary of System Status"
uptime
sleep 30
done
for counter1 in 1 2 3 4 5
do
echo "Displaying running System process"
ps
echo "Displaying System Process and Top CPU using processes"
top
echo "Displaying One line Summary of System Status"
uptime
sleep 30
done
The unusual feature of for loop is – There is no three part structure as used in C, Awk and perl. Unlike while and until, for does not test a condition, but uses a list instead.
for example…
for varibale in list
do
commands
done
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Wget with proxy
rajeshkumar created the topic: wget with proxy
Hi,
Please find a following commmand which can used to wget with proxy….
export http_proxy=
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
scmuser replied the topic: Re: wget with proxy
GNU Wget is a free software package for retrieving files using HTTP, HTTPS and FTP, the most widely-used Internet protocols. It is a non-interactive commandline tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.
GNU Wget has many features to make retrieving large files or mirroring entire web or FTP sites easy, including:
* Can resume aborted downloads, using REST and RANGE
* Can use filename wild cards and recursively mirror directories
* NLS-based message files for many different languages
* Optionally converts absolute links in downloaded documents to relative, so that downloaded documents may link to each other locally
* Runs on most UNIX-like operating systems as well as Microsoft Windows
* Supports HTTP proxies
* Supports HTTP cookies
* Supports persistent HTTP connections
* Unattended / background operation
* Uses local file timestamps to determine whether documents need to be re-downloaded when mirroring
* GNU Wget is distributed under the GNU General Public License.