Tag Archives: FILE

Strange Problem in case statement while shift

By zaq1xsw2

Hi,

Here is my code as below:

test.ksh:
=======

Code:


#!/bin/ksh
option="${1}"
while [ $# -gt 0 ]
do
case $1 in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
-*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
*) ;;
esac
shift 1
done


While i am running the above script:

Code:


[kanix@voodoo mydir]$ ./test.ksh -f "abc.txt"
File name is abc.txt


The above output is as expected.. But when i am running as below:

Code:


[kanix@voodoo mydir]$ ./test.ksh -f "-abc.txt"
File name is -abc.txt
test.ksh:usage: [-f file] | [-d directory]


the usage code is getting displayed.. which is not expected..

But again when i am providing a space between the quote and hyphen it is again showing the expected result.

Code:


[kanix@voodoo mydir]$ ./test.ksh -f " -abc.txt"
File name is -abc.txt


Please advise why this is happening and how to fix this.. i need to run the code as : ./test.ksh -f “-abc.txt”

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Perl Threads Terminating Abnormally.

By Azrael

I’ve used threads before, but not with Perl. Tried looking these errors and using ‘join’ instead of ‘detach’ with no luck. Here is the code I am currently using:

Code:

#!/usr/bin/perl -w

use warnings;
use threads;
use threads::shared;

$Linux='Linux';
$Greek='Greek';

# Declare threads with their sub-routines
my $thr1 = threads->create(&work1);

sub work1 {
open(FILE,"/home/mine/text");
if (grep{/$Greek/} ){
print "/home/mine/text exists!n"
}else{
system("echo '' > /dev/null");
}
close FILE;
$thr1->detach();
}

my $thr2 = threads->create(&work2);

sub work2 {
$filename = "/home/mine/kcron";
if (-e $filename) {
print "/home/mine/kcron exists!n";
}else{
system("echo '' > /dev/null");
}
$thr2->detach();
}

my $thr3 = threads->create(&work3);

sub work3 {
open(FILE,"/home/mine/geek.txt");
if (grep{/$Linux/} ){
print "/home/mine/geek.txt exists!n";
}else{
system("echo '' > /dev/null");
}
close FILE; ...read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Log file Creation

By cnrj

Hi

I have unix script which does execute some OS commands and DB Commands. I would like to create log file for each command execution, Could some one advise how do we create log file.

Example Script:


###########################################################################
ORACLE_SID=ERS11G
ORACLE_HOME=/u03/app/oracle/product/11.2.0/db_1
LD_LIBRARY_PATH=$ORACLE_HOME/lib
PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID ORACLE_HOME PATH LD_LIBRARY_PATH
###################################### Defining the Paths####################
SOURCETYPE=$1
INPUTFILESPATH=/ersdg3/ERS/TEST/RIO/files
BASEPATH=/ersdg3/ERS/TEST
ARCHIVEPATH=$BASEPATH/$SOURCETYPE/archive
CONTROLFILEPATH=$BASEPATH/$SOURCETYPE/control
AUDITFILENAME=${SOURCETYPE}_AUDIT_FILE.TXT
SOURCELOADER=${SOURCETYPE}_SOURCE_LOADER
AUDITCONTROLFILENAME=${SOURCETYPE}_REC_CNT.ctl
SOURCECONTROLFILENAME=$SOURCETYPE.ctl
SOURCECONTROLFILENAMENEW=${SOURCETYPE}_NEW.ctl
SOURCETABLENAME=ERS_${SOURCETYPE}_SRC
ARCHAUDITFILEPATH=${ARCHIVEPATH}/auditfiles
USERID=ETL_ERS
PASSWORD=ETL_ERS
SCHEMA=ERS11G
#####################################End of Paths###########################
cd $INPUTFILESPATH
################# Entry into Audit Table####################################
if [ -f *.LOG ]
then
sqlplus -s -L ETL_ERS/ETL_ERS@ERS11G<<EOF
set feedback off heading off
#exec ETL_LOG.P_LOG_START_PROCESS(trunc(sysdate),'${SOURCELOADER}','ETL_SRC');
exit;
EOF
else
echo "Files doesnt exist"
exit;
fi
####################End of Entry into Audit Table##########################
ls *.LOG >FILE.LIST
###################Check whether Audit file Exists#########################
if [ -f $AUDITFILENAME ]
then
rm $AUDITFILENAME
else
touch $AUDITFILENAME
fi
#########################################################################
while read filename
do
##############Date Format for Sql Loader Script and for Directory Creation#####
filename=$filename
fname=${filename%%.*}
date1=${fname##*_}
date=`print $date1 |cut -c1-8`
dat=`print $date |cut -c7-8`
yr=`print $date |cut -c1-4`
mon=`print $date |cut -c5-6`
var01="Jan"
var02="Feb"
var03="Mar"
var04="Apr"
var05="May"
var06="Jun"
var07="Jul"
var08="Aug"
var09="Sep"
var10="Oct"
var11="Nov"
var12="Dec"
val=$( eval eval echo $var$mon )
DATE="$dat-$val-$yr"
dir=${SOURCETYPE}_${DATE}
echo $dir
############End of Date Format################################################
done <FILE.LIST


…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Shell script not working on Solaris 10

By surender reddy

Hi

Iam running the below scripts on one file on solaris 10 but its giving error.

code

HTML Code:

sh BNGREPORT1.sh z1


code in file BNGREPORT1.sh is given below.

HTML Code:

root@blr-svr-oclan-01 # more BNGREPORT1.sh
#!/bin/sh
#cd /surender
FILE="bng_total_report_$(date +%Y%m%d_%H%M%S)"
echo HOST_NAME "|" IP "|" FREE_MEMORY "|" INT_DISK_UTILIZATION "|" SUB_ACT-SESSIONS "|" CPU_UTIL "|" ENT_DISK_UTILIZATION
echo HOST_NAME "|" IP "|" FREE_MEMORY "|" INT_DISK_UTILIZATION "|" SUB_ACT-SESSIONS "|" CPU_UTIL "|" ENT_DISK_UTILIZATION >> $FILE
.xls
cat $1 | egrep "IP IS|cont bsnl.in|PPPoE|CPU % usage|Memory:|Internal|External" > bngreport.tmp
#cat $1 | egrep "IP IS|cont bsnl.in|PPPoE|Total |Memory:|Internal|External" > bngreport.tmp
while read line
do
if [ "`echo $line | grep IP`" ]
then
IP=`echo $line | cut -d" " -f3`
continue
elif [ "`echo $line | grep "cont bsnl.in"`" ]
then
HOST_NAME=`echo $line | cut -c8-25`
continue
elif [ "`echo $line | grep PPPoE`" ]
then
sub=`echo $line | cut -c9-13`
continue
elif [ "`echo $line | grep "CPU % usage"`" ]
#elif [ "`echo $line | grep "Total"`" ]
then
CPU_UTIL=`echo $line | cut -d"," -f5 | cut -c2-6`
continue
elif [ "`echo $line | grep Memory:`" ]
then
mem=`echo $line | cut -c46-53`
continue
elif [ "`echo $line | grep Internal`" ]
then
disk_int=`echo $line | cut -c30-34`
continue
elif [ "`echo $line | grep External`" ]
then
disk_ent=`echo $line | cut -c30-33`
echo $HOST_NAME "|" $IP "|" $mem "|" $disk_int "|" $sub "|" $CPU_UTIL "|" $disk_ent
echo $HOST_NAME "|" $IP "|" $mem "|" $disk_int "|" $sub "|" $CPU_UTIL "|" $disk_ent >> $FILE.xls
continue
fi
done<bngreport.tmp
#mv /root/$FILE.xls /surender/reports


source file z1 is below

HTML Code:

IP IS 10.238.52.65
spawn telnet 10.238.52.65
Trying 10.238.52.65...
Connected to 10.238.52.65.
Escape character is '^]'.

pun-ras-bng-mhs-01

[local]pun-ras-bng-mhs-01#cont bsnl.in
[bsnl.in]pun-ras-bng-mhs-01#show sub sum all
--------------------------------------------------------------------------------
Total=12031

Type Authenticating Active Disconnecting
PPP 0 0 0
PPPoE 4 12027 4
DOT1Q 0 0 0
CLIPs 0 0 0
ATM-B1483 0 0 0
ATM-R1483 0 0 0
Mobile-IP 0 0 0
[bsnl.in]pun-ras-bng-mhs-01#show process cpu | grep CPU
Total system CPU % usage (5s, 1m, 5m): 76.90, 58.01, 54.93
[bsnl.in]pun-ras-bng-mhs-01#show process aaad detail | grep CPU
%CPU : 11.38%
[bsnl.in]pun-ras-bng-mhs-01#show memory
Memory: Total 922428k, Used 684532k, Free 195648k, Reserved 24k
[bsnl.in]pun-ras-bng-mhs-01#show disk
Location 512-blocks Used Avail Capacity Mounted on
Internal 968158 536932 382818 58% /
External 922558 235166 641264 26% /md
[bsnl.in]pun-ras-bng-mhs-01#exit
Connection to 10.238.52.65 closed by foreign host.

IP IS 10.238.54.1
spawn telnet 10.238.54.1
Trying 10.238.54.1...
Connected to 10.238.54.1.
Escape character is '^]'.

[local]enk-ras-bng-cse-01#cont bsnl.in
[bsnl.in]enk-ras-bng-cse-01#show sub sum all
--------------------------------------------------------------------------------
Total=16820

Type Authenticating Active Disconnecting
PPP 0 0 0
PPPoE 8 16812 3
DOT1Q 0 0 0
CLIPs 0 0 0
ATM-B1483 0 0 0
ATM-R1483 0 0 0
Mobile-IP 0 0 0
[bsnl.in]enk-ras-bng-cse-01#show process cpu | grep CPU
Total system CPU % usage (5s, 1m, 5m): 66.65, 70.36, 69.82
[bsnl.in]enk-ras-bng-cse-01#show process aaad detail | grep CPU
%CPU : 13.62%
[bsnl.in]enk-ras-bng-cse-01#show memory
Memory: Total 922428k, Used 685872k, Free 190908k, Reserved 24k
[bsnl.in]enk-ras-bng-cse-01#show disk
Location 512-blocks Used Avail Capacity Mounted on
Internal 968158 571876 347874 62% /
External 922558 656022 220408 74% /md
[bsnl.in]enk-ras-bng-cse-01#exit
Connection to 10.238.54.1 closed by foreign host.
Connection to 10.238.54.1 closed by foreign host.


output coming as below

HTML Code:

root@blr-svr-oclan-01 # sh BNGREPORT1.sh z1
HOST_NAME | IP | FREE_MEMORY | INT_DISK_UTILIZATION | SUB_ACT-SESSIONS | CPU_UTIL | ENT_DISK_UTILIZATION
BNGREPORT1.sh: : cannot execute
BNGREPORT1.sh: test: argument expected
root@blr-svr-oclan-01 #


can any body help.

tnx in advance.

Source: FULL ARTICLE at The UNIX and Linux Forums

Compiling C++ source file

By kristinu

I am trying to compile a c++ source file print_options.cpp and getting lot of errors

Code:

g++ -I/media/ios120/chrisd/research/tomso-branches/tomso_12_05 /media/ios120/chrisd/research/tomso-branches/tomso_12_05/libs/program_options/print_options.cpp


Code:

In file included from /media/ios120/chrisd/research/tomso-branches/tomso_12_05/libs/program_options/print_options.cpp:11:0:
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:23:3: error: variable or field ‘printopt_secn’ declared void
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:23:3: error: ‘FILE’ was not declared in this scope
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:23:10: error: ‘stream’ was not declared in this scope
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:24:3: error: expected primary-expression before ‘int’
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:25:3: error: expected primary-expression before ‘const’
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:26:3: error: expected primary-expression before ‘bool’
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:35:3: error: variable or field ‘printopt_nwln’ declared void
/media/ios120/chrisd/research/tomso-branches/tomso_12_05/tomso/program_options/print_options.hpp:35:3: error: ‘FILE’ was not declared in this scope
.......


Source: FULL ARTICLE at The UNIX and Linux Forums

How to replaces a value in a file after a particular string using perl?

By praveen265

I need to replace the value of notifications_enabled to 0 if the value already set to 1 and vice versa(not the other values and spaces remain same after the value changed). I tried the below program for that. Can any one help me out.

file:test.cfg
define host {
hostgroups linux-servers
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
notifications_enabled 1
}

script:
#!/usr/bin/perl
use strict;
use vars qw( @value $status $line);

@value = `grep -i notifications_enabled /root/test.cfg`;
open(FILE,”/root/test.cfg”) || die unable to open the config_file;
while ($line = )
{
chomp $line;
@value = `grep -i notifications_enabled /root/test.cfg`;
$status = (split(/s{2,}/,$value[0]))[1];
}
print “$statusn”;
close(FILE);

open(FILE,”>>/root/test.cfg”) || die unable to open the config_file;
while ( $line = )
{
print “$status”;
if ( $status == 1) {
$status = 0;
}
}
close(FILE);

Source: FULL ARTICLE at The UNIX and Linux Forums

Find and split the list of files with suffiz of seg**

By Satish Shettar

Hi,.

I am writing a script to get the new files and split them.

Requirement
Find the new files under the path “/wload/scmp/app/data/OAS” (There are 5 sub folders).
Gunzip the files which are having .gz suffix.
Put the list of files in the filename in the format “fiels_to_split_2013012911300.txt where as the numeric part is date and time.
Perform the split command on the files to split each file containing 50000 lines.
The output of the split filename should have the same file with suffix seg** where ** can be either numbers or alphabets.

I got the code from Pikk45 and tried to add/modify as per the need.

find /wload/scmp/app/data/OAS -name “*.gz” -size +100000000 -type f -mtime -1 -exec gunzip {} ;
FILENAME=new_fiels_to_split$(date +”%Y%m%d%H%M”).txt
find /wload/scmp/app/data/OAS -type f -mtime -1 > /wload/scmp/app/data/OAS/$FILENAME
FILES=/wload/scmp/app/data/OAS/$FILENAME
FIRST=1
FILE=1
SPL=50000
while read file; do
if [ -e “${FILE}” ]; then
while [ $SPL -le `wc -l $FILES`]
do
sed -n ‘$FIRST,$SPLp’ $FILES > $FILES.seg{$FILE}
fi
done < ${FILES}
FIRST=`expr $FIRST + 50000`
SPL=`expr $SPL + 50000`
FILE=`expr $FILE + 1`
done

for example
the source files


-rw-rw-r– 1 scmpadm scmpuser 755861762 Jan 26 20:04 activity.log.20130125.gz
-rw-rw-r– 1 scmpadm scmpuser 0 Jan 26 20:05 trigger.activity.log.20130125
-rw-rw-r– 1 scmpadm scmpuser 913911244 Jan 27 20:24 activity.log.20130126.gz
-rw-rw-r– 1 scmpadm scmpuser 0 Jan 27 20:25 trigger.activity.log.20130126
-rw-rw-r– 1 scmpadm scmpuser 329351168 Jan 28 19:10 activity.log.20130127.gz

After gunzip

-rw-rw-r– 1 scmpadm scmpuser 3012939863 Jan 28 19:10 activity.log.20130127

Files after split

-rw-rw—- 1 scmpadm scmpuser 49877675 Jan 29 07:04 activity.log.20130127.segaa
-rw-rw—- 1 scmpadm scmpuser 47552800 Jan 29 07:07 activity.log.20130127.segab
-rw-rw—- 1 scmpadm scmpuser 45867421 Jan 29 07:08 activity.log.20130127.segac
-rw-rw—- 1 scmpadm scmpuser 48985303 Jan 29 07:09 activity.log.20130127.segad
-rw-rw—- 1 scmpadm scmpuser 50846638 Jan 29 07:10 activity.log.20130127.segae
-rw-rw—- 1 scmpadm scmpuser 50946829 Jan 29 07:13 activity.log.20130127.segaf
-rw-rw—- 1 scmpadm scmpuser 50968144 Jan 29 07:14 activity.log.20130127.segag
-rw-rw—- 1 scmpadm scmpuser 51758156 Jan 29 07:18 activity.log.20130127.segah
-rw-rw—- 1 scmpadm scmpuser 52290589 Jan 29 07:23 activity.log.20130127.segai
-rw-rw—- 1 scmpadm scmpuser 19329024 Jan 29 07:26 activity.log.20130127.segaj

Source: FULL ARTICLE at The UNIX and Linux Forums

How to change time stamp with touch command?

By bioinfo

Hi,

I wish to change time stamp of a directory with all its subdirectories and files on server.
I am able to find following two ways but want to know which will be the better one. I have not tried anyone of them because I am not sure if it can effect my data:

Code:

find * -type d -exec touch '{}/.gitkeep' ;


Code:


$ bash recursive_touch.sh MyProject .gitkeep

SOURCE_DIR=$1
FILE=$2

recursive_touch() {
for dir in *
do
if [ -d $dir ]
then
cd $dir; recursive_touch
fi
touch $FILE
done
}

cd $SOURCE_DIR; recursive_touch


Thanks

Source: FULL ARTICLE at The UNIX and Linux Forums

Using sed in a for loop

By plumb_r

Hello
I have a group of files

Code:

a1.profile a2.profile a3.profile a4.profile b1.profile b2.profile b3.profile b4.profile


These files all have the same first line with a value s1 atop the columns

Code:

s1_context s1_ref s1_sample s1_% etc


I am trying to use sed in a for loop to replace the s1 in the header of each each file with the initial part of the filename with something like

Code:

for FILE in a1 a2 a3 a4 b1 b2 b3 b4
do
sed -i.bak "1s/s1/$FILE/g" $FILE.profile > $FILE.profile.header
done


But I am not getting the desired output. I just get $FILE in the header instead.
Thanks

Source: FULL ARTICLE at The UNIX and Linux Forums

Help with ahem Prime number Generating Script

By drewannCan anybody tell me why the second part of this script (Sieve of Eratosthenes) isn’t working properly. This isnt coursework or homework just private studies ( Yes Project Euler began it ) I know there are easier ways of doing this too but I want to do it this way.:p
Iam using Cygwin on Vista
Thanks

Code:

#!/bin/bash
#——PERFORM WHEEL FACTORISATION——
echo “WHEEL FACTORISATION”
let i=0
let j=0
let filenamy=1
for Input_Number in {1..1000}
do
# Write each 6 numbers of sequence to new temp txtfiles
if [ $i == 6 ]
then
filenamy=$(($filenamy+1))
let i=0
fi
# Output each number to a newline
LINE1=$Input_Number
FILE=${LINE1}
echo -e $FILE>>/home/Gio/OutputtedFiles/Filenammies/$filenamy.txt
i=$(($i+1))
done
# Count the number of txtfiles to $filecounter1
cd /home/Gio/OutputtedFiles/Filenammies
let Filecounter1=`ls *.txt | wc -l`
for (( filenamy=1; filenamy >/home/Gio/OutputtedFiles/oUtPuT.txt
echo -e $FILE2a>>/home/Gio/OutputtedFiles/oUtPuT.txt
echo -e $FILE2b>>/home/Gio/OutputtedFiles/oUtPuT.txt
done
# Count the number of txtfiles to $filecounter1
let Filecounter1=`ls *.txt | wc -l`
for (( filenamy=1; filenamy >/home/Gio/OutputtedFiles/oUtPuT.txt
fi
# Sort numbers because primes will be tagged onto end
sort -n /home/Gio/OutputtedFiles/oUtPuT.txt
# End of Loop Two
done
# End of Loop One
done
🙂
Source: The UNIX and Linux Forums