How to find the disk usage of current folder and system up time ?
1. uptime command is used for finding the system uptime
2. du command is used to find the disk usage level.
du accepts many parameters. Please do "man du" to find different paraments accepts by du command. In the below example, we display disk usage of current folder in Mega Bytes.
[hostname:root:/tmp/spohsv] cat w.sh
#!/bin/sh
time=`uptime`
echo "The system is Up since \"$time\""
diskusage=`du -sm`
echo "Disk usage of current directory in Mega Bytes is"
echo $diskusage
echo `date`
How to append the log file name with date and timestamp ?
Please keep the below statement in the script. This will create the log file scanLog***. Example file name : "scanLog_2017-11-02-14-41-32.log"
logFilename=/tmp/scanLog_$(date "+%Y-%m-%d-%H-%M-%S").log
How to find the total lines in a file and assign it to variable ?
echo totalLines=`wc -l sample.txt | awk -F" " '{print $1}'`
echo $totalLines
How to find total lines in all files in a directory ?
Code below :
#!/bin/sh
fileList=fileList.txt
ls s*.txt > $fileList # Specify the directory in ls command.
cflag=1
while read file
do
count=`wc -l $file | awk -F" " '{print $1}'`
echo "Total lines in $file is $count "
if [ $cflag = 1 ]; then
cflag=0
totalLines=`expr $count + 0`
continue
fi
if [ $cflag = 0 ]; then
totalLines=`expr $count + $totalLines`
cflag=0
fi
done < $fileList
rm $fileList
echo "Total Lines in all files is $totalLines"
How to find the total lines in all files when the last line defines the total lines to be considered.
For example :
sample1.txt : Below file has total of 5 lines. But, file defines to use total as 3.
Hi,
Hello, we are defining an example
Above line is blank line.
total=3
sample2.txt : Below file has 8 lines. But, the file defines to use total of 5 lines.
Hello,
This text file is used for defining how to count total
when total lines are defined in end of file
Above line is blank line
Above line is blank line
total=5
In the above text files, we have blank lines. Total lines which are not blank is defined in last line of the file and it is prefixed by the string "total". In this case, below script will help is counting total lines in all files
#!/bin/sh
fileList=fileList.txt
ls s*.txt > $fileList
cflag=1
while read file
do
count=`tail -1 $file | awk -F"=" '{print $2}'`
echo "Total lines in $file is $count "
if [ $cflag = 1 ]; then
cflag=0
totalLines=`expr $count + 0`
continue
fi
if [ $cflag = 0 ]; then
totalLines=`expr $count + $totalLines`
cflag=0
fi
done < $fileList
rm $fileList
echo "Total Lines in all files is $totalLines"
How to call a sample java program from shell script ?
#!/bin/sh
### This shell script accepts one argument - JobNumber as argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <jobnumber>" >&2
echo "Example : $0 jd01"
exit 1
fi
echo "Start of the Program : `date`"
arg1=$1
/usr/java6/bin/javac -d . sample.java
/usr/java6/jre/bin/java sample $arg1
echo " End of shell program : `date"