Arthas a dit:Salut Flakk,
Merci pour ton aide, mais effectivement, les commandes Unix ne sont pas mon fort (il va falloir que je m'y mette un jour ) et celle que tu me présentes est déjà un peu trop complexe à mon goût.
J'ai en revanche essayer en PHP et j'arrive à changer en lowercase la totalité du contenu de la racine d'un répertoire donné mais pas des sous-répertoires ?
Connais-tu PHP ? Si oui, peut-être peux-tu m'aider ?
Merci
#!/bin/sh
#Tatouille example (c)
for libs in `ls -R /usr/lib | grep dylib`;do
if ! test -L /usr/lib/$libs ;then
unameLib=`(echo $libs | sed -n -e "s/^lib//g" -n -e "s/\..*//p")`
versionLib=`(echo $libs | sed -n -e "s/^lib$unameLib\.//g" -n -e "s/\.dylib//p")`
nameLib=$(echo $unameLib | cut -c 1 | tr a-z A-Z)$(echo $unameLib | sed 's/^.//')_Lib
echo $nameLib "------" $versionLib "------" $unameLib
fi
done
flakk a dit:huhu tartouille
je suis pas sur que ton script en exemple réussisse à le convaincre si déja la ligne que j'avais mis lui fait peur.
mais ceci dit, entièrement d'accord... faire ca autrement qu'en shell... c'est c'est presque un non-sens... (en php, on peut même enlever le "presque" de la phrase précédente )
et au risque de me répéter, si ca peu paraitre compliqué de prime abord.. ca ne l'est pas... et l'investissement en temps pour s'y mettre n'est rien comparé au gain de temps que ca apporte quotidienneement.
on a un OS digne de ce nom avec les outils adéquats.. profitons en !!
#!/bin/bash
# {{{ Config
# Bundle
AppPath="/home/elm/www"
workspace="$AppPath/resources/workspace/$(date +%Y%m%d)"
BundlePath="$AppPath/bundles/PopulateDB.bundle"
RegateFile="$AppPath/resources/receipt/eqnaaa30.txt"
mysqlbindir="/usr/local/mysql/bin"
SplitNumRow="40000"
# MySQL
DB="lpmf_import"
DB_USER="root"
DB_PASS=""
db_B_tbl=$DB".import_B"
db_C_tbl=$DB".import_C"
db_P_tbl=$DB".import_P"
db_Entity_tbl=$DB".import_entity"
# Files
BFile="B.txt"
CFile="C.txt"
PFile="P.txt"
RFile="R.txt"
PXFile="PX.txt"
pXrFile="Pfinal.txt"
# }}}
# {{{ Function
isRoot ()
{
#
# Root privileges
#
ROOT_UID=0
ROOTUSER_NAME=root
E_WRONG_USER=403
username=`id -nu`
if [ "$UID" -ne "$ROOT_UID" ]
then
consolMessage "You must be root to run this script."
exit $E_WRONG_USER
fi
if [ "$username" != "$ROOTUSER_NAME" ]
then
consolMessage "You must be root to run \"`basename $0`\"."
exit $E_WRONG_USER
fi
}
consolMessage ()
{
#
# Print a Message
#
echo "$1"
echo "..............................................................................."
}
initWorkspace ()
{
#
# init workspace folders
#
consolMessage ""
mkdir -p $1
rm -fR "$1"
mkdir -p $1/data
rm -fR "$1/data"
mkdir -p $1/data
mkdir -p $1/pivot
rm -fR "$1/pivot"
mkdir -p $1/pivot
chmod -R 777 $1
}
extractB ()
{
#
# Reading the regate file and
# extract the index of Properties
#
consolMessage " Reading the regate file, creating the File $1"
grep -e '^B;' $3 | cut -d ';' -f2,3,4,5,1,6 | sort -k3 -t';' -T /tmp > $2
consolMessage " >>> Done"
}
extractC ()
{
#
# Reading the regate file and
# extract the index of Headings
#
consolMessage " Reading the regate file, creating the File $1"
grep -e '^C;' $3 | cut -d ';' -f2,3,4,5,1,6 | sort -k3 -t';' -T /tmp > $2
consolMessage " >>> Done"
}
extractP ()
{
#
# Reading the regate file and
# extract the content of Properties and Headings
#
consolMessage " Reading the regate file, creating (sorting by Rcode,Pcode) the File $1"
grep -e '^P;' $3 | cut -d ';' -f1,2,3,4,5 | sort -k3,4 -t';' -T /tmp > $2
consolMessage " >>> Done"
}
splitP ()
{
#
# Splitting the previous content file
#
consolMessage " Reading the regate file in memory, splitting (every $3 rows) the file $1"
(cd $2 && split -l$3 ../$1)
CountItem=`ls $2 |wc -l`
consolMessage " >>> Done"
}
Btable ()
{
#
# Populating DB creating, populating, Properties table import_B
#
consolMessage " Populating DB : $DB, creating, populating $db_B_tbl with $BFile"
sql="use $DB; \
drop table if exists $db_B_tbl; \
create table if not exists $db_B_tbl ( \
B1 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
B2 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
B3 varchar(5) character set utf8 collate utf8_unicode_ci not null , \
B4 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
B5 varchar(4) character set utf8 collate utf8_unicode_ci not null, \
B6 varchar(54) character set utf8 collate utf8_unicode_ci not null, \
PRIMARY KEY (B3), \
KEY B1 (B1,B2,B3,B4,B5,B6) \
)engine=MyISAM default charset=utf8 collate=utf8_unicode_ci;"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
FileName=$workspace/$BFile;
sql="use $DB;load data infile '$FileName' into table $db_B_tbl fields terminated by ';' lines terminated by '\n'"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
consolMessage " >>> Done"
}
Ctable ()
{
#
# Populating DB creating, populating, Headings table import_C
#
consolMessage " Populating DB : $DB, creating, populating $db_C_tbl with $CFile"
sql="use $DB; \
drop table if exists $db_C_tbl; \
create table if not exists $db_C_tbl ( \
C1 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
C2 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
C3 varchar(5) character set utf8 collate utf8_unicode_ci not null , \
C4 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
C5 varchar(4) character set utf8 collate utf8_unicode_ci not null, \
C6 varchar(54) character set utf8 collate utf8_unicode_ci not null, \
PRIMARY KEY (C3), \
KEY C1 (C1,C2,C3,C4,C5,C6) \
)engine=MyISAM default charset=utf8 collate=utf8_unicode_ci;"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
FileName=$workspace/$CFile;
sql="use $DB;load data infile '$FileName' into table $db_C_tbl fields terminated by ';' lines terminated by '\n'"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
consolMessage " >>> Done"
}
Ptable ()
{
#
# Populating DB creating, populating, Content table import_p
#
consolMessage " Populating DB : $DB, creating, populating $db_P_tbl with the split of $PFile"
sql="use $DB; \
drop table if exists $db_P_tbl; \
create table if not exists $db_P_tbl ( \
P1 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
P2 varchar(1) character set utf8 collate utf8_unicode_ci not null , \
P3 varchar(6) character set utf8 collate utf8_unicode_ci not null , \
P4 varchar(5) character set utf8 collate utf8_unicode_ci not null , \
P5 varchar(255) character set utf8 collate utf8_unicode_ci not null \
)engine=MyISAM default charset=utf8 collate=utf8_unicode_ci;"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
sql="use $DB;"
for P_file in `ls $workspace/data | grep -e '[a-z]'`
do
FileName=$workspace/data/$P_file
sql="$sql load data infile '$FileName' into table $db_P_tbl fields terminated by ';' lines terminated by '\n';"
done
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
consolMessage " >>> Done"
}
extractRID ()
{
#
# Extracting the REGATEID from import_P
#
consolMessage " Transpose task, getting Regate ID from $db_P_tbl"
pRIDFile="$RFile"
if [ -f $workspace/$pRIDFile ]
then
rm -f $workspace/$pRIDFile
fi
sql="use $DB;
SELECT P3
INTO OUTFILE '$workspace/$pRIDFile'
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
FROM import_P
GROUP BY P3;"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
pRIDNum=`cat $workspace/$pRIDFile |wc -l`
consolMessage " >>> Done"
}
extractPref ()
{
#
# Extracting the references to catch from P_ref table
#
consolMessage " Transpose task, extract pivot"
if [ -f $workspace/$PXFile ]
then
rm -f $workspace/$PXFile
fi
sql="use $DB; \
SELECT \
p_ref.code, \
p_ref.col,p_ref.colfieldtype,p_ref.coldfielddefault,p_ref.coldfieldlength \
INTO OUTFILE '$workspace/$PXFile' \
FIELDS TERMINATED BY ';' \
LINES TERMINATED BY '\n' \
FROM p_ref \
WHERE p_ref.state =1 \
ORDER BY p_ref.colpos;"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
consolMessage " >>> Done"
}
storePref ()
{
#
# Storing the results of extractPref () into arrays
#
consolMessage " Transpose task, storing $PXFile into arrays"
i=1
#Parr Properties Array
while read -r
do
fieldName[$i]=$(echo $REPLY | awk -F ";" '{print $2}')
fieldType[$i]=$(echo $REPLY | awk -F ";" '{print $3}')
fieldDefault[$i]=$(echo $REPLY | awk -F ";" '{print $4}')
fieldLength[$i]=$(echo $REPLY | awk -F ";" '{print $5}')
Parr[$i]=$(echo $REPLY | awk -F ";" '{print $1}')
let i=$i+1
let j=$i-1
done <$workspace/$PXFile
consolMessage " >>> Done"
}
explodeRID ()
{
#
# Splitting REGATE File every regateid
#
consolMessage " Transpose task, splitting $RegateFile in folder $workspace/pivot (foreach regateid stored in $pRIDFile)"
if [ -f $workspace/$pXrFile ]
then
rm -f $workspace/$pXrFile
fi
wf=1
while read -r
do
grep -e "^P\;[A-Z]\{1\}\;${REPLY}\;" $workspace/$PFile > $workspace/pivot/${REPLY}.txt
echo " --- writing file Entity : ${REPLY}.txt --- $wf/$pRIDNum --- "
let wf=$wf+1
done <$workspace/$pRIDFile
consolMessage " >>> Done"
}
transEntity ()
{
#
# Creating the transposed file
#
consolMessage " Transpose task, creating the transposed file"
rw=1
while read -r
do
RID="$REPLY"
rowVal=""
for (( m=1 ; m <= $j ; m++ ))
do
row=$(grep -e "^P\;[A-Z]\{1\}\;${RID}\;${Parr[$m]}\;" $workspace/pivot/${RID}.txt)
if [ "${row}" = "" ]
then
line="P;M;${RID};${Parr[$m]};${fieldDefault[$m]}"
else
if [ "$(echo ${row} | awk -F ";" '{print $5}')" = "" ]
then
line="P;M;${RID};${Parr[$m]};${fieldDefault[$m]}"
else
line="${row}"
fi
fi
if [ $m = 1 ]
then
rowVal="${RID};$(echo ${line} | awk -F ";" '{print $5}')"
else
rowVal="${rowVal};$(echo ${line} | awk -F ";" '{print $5}')"
fi
done
echo " --- writing row ${rw} Entity : ${RID} - Items : ${m} --- "
echo $rowVal >>$workspace/$pXrFile
let rw=$rw+1
done <$workspace/$pRIDFile
consolMessage " >>> Done"
}
EntityTable ()
{
#
# Creating, populating import entity table
#
consolMessage " Transpose task, creating, populating $db_Entity_tbl"
sql="use $DB; \
DROP TABLE IF EXISTS $db_Entity_tbl; \
CREATE TABLE $db_Entity_tbl (
REGATEID varchar(6) collate utf8_unicode_ci NOT NULL default ''"
for (( m=1 ; m <= $j ; m++ ))
do
if [ "${fieldType[$m]}" = "varchar" ]
then
sql="$sql,${fieldName[$m]} ${fieldType[$m]}(${fieldLength[$m]}) collate utf8_unicode_ci NOT NULL default '${fieldDefault[$m]}'"
elif [ "${fieldType[$m]}" = "date" ]
then
sql="$sql,${fieldName[$m]} ${fieldType[$m]} NOT NULL default '${fieldDefault[$m]}'"
fi
done
sql="$sql \
,PRIMARY KEY(REGATEID) \
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
FileName=$workspace/$pXrFile
sql="use $DB;"
sql="$sql load data infile '$FileName' into table $db_Entity_tbl fields terminated by ';' lines terminated by '\n';"
$mysqlbindir/mysql --user=$DB_USER --password=$DB_PASS --batch -N -e "$sql"
consolMessage " >>> Done"
}
# }}}
#----------------------------------------------------------------------------------------------------------------------
# Root privileges
isRoot
clear
#
#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
# First step
#----------------------------------------------------------------------------------------------------------------------
# Init workspace
initWorkspace "$workspace"
# Reading the regate file and extract the index of Properties
extractB "$BFile" "$workspace/$BFile" "$RegateFile"
# Reading the regate file and extract the index of Headings
extractC "$CFile" "$workspace/$CFile" "$RegateFile"
# Reading the regate file and extract the content of Properties and Headings
extractP "$PFile" "$workspace/$PFile" "$RegateFile"
# Spliting the previous content file
splitP "$PFile" "$workspace/data" "$SplitNumRow"
#----------------------------------------------------------------------------------------------------------------------
# End of first step
#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
clear
consolMessage "Script Execution time : $SECONDS sec, $CountItem items"
#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
# Second step
#----------------------------------------------------------------------------------------------------------------------
# Creating, populating, Properties table
Btable
# Creating, populating, Headings table
Ctable
# creating, populating, Content table
Ptable
#----------------------------------------------------------------------------------------------------------------------
# End of second step
#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
clear
consolMessage " Script Execution time : $SECONDS sec"
#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
# Third step
#----------------------------------------------------------------------------------------------------------------------
# Extracting all REGATEID
extractRID
# Extracting properties to catch
extractPref
# Storing the results of extractPref () into arrays
declare -a fieldName #Columns
declare -a fieldType #Types (varchar,int,date ... )
declare -a fieldDefault #Default Values
declare -a fieldLength #Lengths
declare -a Parr #Codes
j=0 #Number of selected properties
storePref
# Splitting REGATE File ( every regate id stored into RID.txt )
explodeRID
# Creating the transposed file
transEntity
# Creating, populating import_entity table
EntityTable
#----------------------------------------------------------------------------------------------------------------------
# End third step
#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
clear
consolMessage " Script Execution time : $SECONDS sec"
consolMessage " Import finished"
#----------------------------------------------------------------------------------------------------------------------
exit 0