First, I am not a Unix scripter, but have been tasked to have the step below to check for the file size. If it is greater than 0, then it will continue to process that file.
However, if its 0, it will echo "Skip it - NO DATA IN THE dnt_pln_inconj.dat FILE"
Problem is when I run this step, it only gets as far as "Received file with data - Continue"
How can I make it continue processing if the file size is greater than 0? What am I doing wrong?
Any help would be awesome.
#Step 12: Verify pln proc inconjunction
step_num=$((step_num+1))
echo "stepnum $step_num: Verify pln proc inconjunction"
if [[ $last_step -eq $step_num ]]
then
if [[ -s ${source_dir}/${file} ]]
then
echo "Received file with data - Continue"
$scriptdir/dental_script/dnt_verify_counts.sh dnt_pln_inconj.dat PLN-130
ret_val=$?
if [[ $ret_val -ne 0 ]]
then
next_step_num=$((step_num+1))
echo $next_step_num > $logdir/$scriptname"_"step_num.log
exit $step_num
fi
else
echo "Skip it - NO DATA IN THE dnt_pln_inconj.dat FILE"
fi
last_step=$((last_step+1))
else
echo "Skip it"
fi
It all depends on what dnt_verify_counts.sh script is doing.
In your code line:
ret_val=$?
is checking for exit code of previous command - which is dnt_verify_counts.sh with parameters. I think it doesn't return file size..
If you want to check file size, play with 'du' utility for example, which gives you size of given file, e.g.:
du /path/to/dnt_pln_inconj.dat
and then, cut given data with cut, to return only size part; line:
$(du /path/to/dnt_pln_inconj.dat | cut -f 1 -)
will return given file size which you can use in your if - then - else block.conditions checking.
Related
I am trying to learn unix. I wanted to set up a sort of file watcher that looks for files and returns the file names so i can move them from source to processing folder to process. It echos File Found I just cannot figure out how to capture the file name.
#determines if file exists
if [ -f * ]; then
echo "File found"
else
echo "File not Found"
fi
# returns file to array
#Needs name still
NewFiles[0] =
#output what what found in 0 index
echo "Found File"
echo NewFiles[0]
Assuming the files have no fancy names (embedded spaces or similar), you might use that approach:
set -- *
[ $# -gt 0 ] && {
echo Found file
echo $1
}
I want to log data of asterisk command line. But the criteria is I want log data for calls separately, i.e. I want to log data for each call in separate file.
Is there a way to do that?
In case there is no inbuild feature in asterisk to do this, here is a bash solution:
#!/bin/bash
echo "0" >/tmp/numberoflines
IFS=''
pathToLogFile = /path/to/log/file
while [ 1 ]
do
NUMBER=$(cat /tmp/numberoflines)
LINECOUNT=$(wc -l < $pathToLogFile)
DIFFERENCE=$(($LINECOUNT-$NUMBER))
if [ $DIFFERENCE != 0 ]; then
lines=($(tail -n $DIFFERENCE $pathToLogFile))
for line in $lines; do
callID = `expr "$line" : 'CALLID_REGEX (see below)'`
$(echo "$line" >> /path/to/log/directory/$callID)
done
fi
sleep 5;
echo "$LINECOUNT" >/tmp/numberoflines
done
untested
it should be used to get ab idea to solve this problem.
the regular expression: normaly: /\[(C\d{8})\]/. sadly I don't know the syntax in bash. I'm sorry. you have to convert it by yourself into bash-syntax.
The idea is: remember the last line in the logfile that was processed by the bash script. check the line count of the log file. if there are more lines then the remembered line: walk through the new lines and extract the call id at the beginning of each line (format: C******** (* are numbers). in words: a C followed by a number with 8 digits). now append the whole line at the end of a log file. the name of the file is the extracted callid.
EDIT Information about the call id (don't mistake it with the caller id): https://wiki.asterisk.org/wiki/display/AST/Unique+Call-ID+Logging
I am attempting to make a script that will check to see if there is any tyext within a file. I have developed the following script. I have made it check to see if there is exactly 2 arguments, see if the file exists, but I am having trouble checking the file for text within it. The code is as follows:
#!/bin/ksh
#check if number of arguments are 2
if [ $# -ne 2 ]; then
echo "Does not equal two arguments"
echo "Usage $0 inputfile outputfile"
exit 1
fi
#check if input file exists
if [ ! -f $1 ]; then
echo "$1 not found!"
exit 1
fi
#Check if input file is null
#This next block of code is where the issue is
if [ grep -q $1 -eq 0 ]; then
echo "$1 must have text within the file"
exit 1
fi
Any help would be appreciated
test's "-s" option checks if the file is empty -- see manual. So your last chunk would become
#Check if input file is null
#This next block of code is where the issue is
if [ ! -s $1 ]; then
echo "$1 must have text within the file"
exit 1
fi
Try using stat
stat -c %s filename
#!/bin/bash
LOCATION=$1
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ]
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
I don't actually get what the If statement is saying can anyone help me to explain this?Thank you
$1 refers to the first argument of the bash file. In this case, you can pass your directory path by issuing the following command:
# ./test2.sh /path/of/your/directory
#!/bin/bash
LOCATION=$1 #first argument of the script
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ] #if the number of argument(s) ($#) is less than 1
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
You can read this article for more information about parameter passing.
Hope it helps.
$1 is the first argument that is passed to the bash script. If you start the script like ./test2.sh argument1 argument2 the $1 will refer argument1.
The if-statement checks, if the count of arguments (that's the $#) is smaller than 1, then it will output the usage statement (as it seems you can't run the script without any argument).
How to write a shell script named "backup.sh" which accepts one parameter, which would be a filename/directory.
Create a backup copy of that with the .bak appended to its name.Show message on success.
If the file/directory does not exist, show a proper message.
i did up to this point.please help me to figure this out
#!/bin/sh
#create_backup.sh
And add a .bak
bak="${backup.sh}.bak"
if [ "$#" -eq 0 ]
then
exit 1;
echo "File Succesfully backuped"
fi
cp ${1} "${1}.back"
echo "File is not found "
exit 0
#!/bin/bash -e
directory=$1
cp -r $directory $directory.bak
echo "Success"
obvious caveats with pathing/error codes/etc