It's a big waste to call grep,awk,sed,cut,... to process a single line. These are external tools that the shell has to load, launch, read from, write to, and reap. They're also bulk tools optimized for scanning through hundreds to millions of lines. Using a shell builtin will be at least one hundred times faster.
You don't need to use echo and backticks to do simple arithmetic in ksh either. I don't think your method will work either, you'll end up with a string "0+1+1+1+1" instead of "1", then "2", then "3" etc. Using the built in (( )) math operator instead of creating a subshell with backticks will be at least a hundred times faster too.
I think it's complaining because you forgot to put "" around your backticks and it started splitting strings into multiple arguments on spaces, but this would work better:
Last edited by Corona688; 02-09-2011 at 10:41 AM..
How curious... I tested that statement with the "worst" version of KSH I had available, but even pdksh doesn't seem to use as old a syntax as yours. What system are you on?
In any case I thought of a better way. grep is capable of counting all by itself, no need to check a file line by line. This is also a typical way to use grep, on entire files, not single lines.
The following example prompts are passed into the shell script.
$1 = /tmp/dir/
$2 = varies (test.txt, test1.txt, test2.txt...)
$3 = test_YYYYMMDD.txt
---------------------------------------------------------------------------
#!/bin/sh
cd $1
if ; then
if ; then
... (3 Replies)
Hi,
I am trying to write a small script that validates if there exist files that start with a pattern in a given directory. Below is the piece of my script:
#!/usr/bin/ksh
BTFDIR=/opt/ships/temp
if
then
echo 'found'
else
echo 'not found'
fi
When I run this... (2 Replies)
check_build_info_table()
{
if
then
export build_info_table=`sqlplus -s sna/dbmanager <<!
set pagesize 0 heading off feedback off
SELECT DISTINCT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'XYZ' AND TABLE_NAME = 'MY_TABLE';
exit
!`
... (3 Replies)
Hi All,
I am running the script
VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt
result=`cat har1out.mytxt | grep $1'
echo $result
echo $1
{
if
then
echo pass
else
echo fail
fi (2 Replies)
Hello all,
I am trying to figure out why i am getting an error while executing the script...altought it seems like its work...but still get the test arguement error...any help would be appericiate...this script basically connects to any oracle db ( just have to pass db name to it)... (4 Replies)
# to search a file if it exists and whether its readable or not
# if yes print its first 5 lines
echo enter the filename to be searched
read fname
if #-d $fname
then
echo file doesn exists
elif
then
echo its a directory
elif
then
cat $fname
else
echo its not readable
fi
# end of... (9 Replies)
Hi,
No need to say I'm new to unix shell scripting.
I have a very simple script that goes this way:
for datos in `ls -rt $UNXLOG/26-Jan*`
do
export arch=`echo $datos |cut -d, -f1`
if
then
export linea1=`grep Debut ${arch}`
export horatot=`echo $linea1 |cut -d' ' -f5`
... (7 Replies)
I'm newbie to coding script so i found test: argument expected when i run it. please help me
a=`df -k |awk '{print $5 }'|egrep "(100%|%)"|cut -d"%" -f1|tail -1`
if
then
df -k|egrep "(100%|%)"|awk '{print $1,$5,$6}'
else
echo "No disk capacity more than 80%"
fi
thk in advance (7 Replies)
I am trying to compare two integer variables in the if statement, but i am getting this "test:argument expected".
What am i missing?
Why is the if loop not executing correctly?
trunkPCM="100000";
more $FILE |while read line
do
PCM=`echo $line | awk '{ print $2 }'`
... (4 Replies)
Hi All,
When i run the below code :
v_shortfield = ""
if ; then
echo "ravi"
else
echo "kumar"
fi
i am getting output as :
sam.ksh: test: argument expected
kumar
Why i am getting error test:argument expected
and why i am not getting output as "ravi" :confused: (5 Replies)