This forum has been archived. All content is frozen. Please use KDE Discuss instead.

How to convert KDE RC files into variables for shell script

Tags: None
(comma "," separated)
vahid
Registered Member
Posts
32
Karma
0
In this example we have 2 scripts.
I have not used this method to run the actual

1. completed-script.sh - this was the older method of notification.sh script where I held rcfile content as a shell script variable

2. pump-variables.sh - this script searchs config folder for a pattern of specific RC files (you can change this to your pattern of required RC files) and when it finds the files it types out files and takes aware lines and adds n to where lines are meant to be and generates two shell script variables.
1, filename=path to file
2. variable=content of file

This may come in hand if you are tying to automate something and generating a script that you can use on any other KDE machine
The current method of notification.sh relies on end user having those RC files , this method has all the required values automatically generated in a script ready to be used anywhere..



Pre -requists (replace.sh script)

vi /usr/bin/replace.sh
Code: Select all
#!/bin/bash
case $# in
  3)
     cat $3| sed "s/$1/$2/g" | sed "s/  / /g" > .temp.newname
     echo -e "The string has been replaced, if you have mucked up"
     echo -e "you will find the old file in /tmp/$3 n"
     mv $3 /tmp/$3
     mv .temp.newname $3
     ;;
  *)
     echo "Usage: RESTORE.SH STRING_TARGET STRING_REPLACE FILENAME"
     echo
     echo "Example: string-rename "_" " " my_file"
     echo "  will rename "my_file" to "my file""
     exit 1
esac
exit 0

chmod 755 /usr/bin/replace.sh

This is now code for pump-variabe.sh (kept in ~/scripts/)

Code: Select all
#!/bin/sh                                     

#This is looking for  a pattern of .notifyrc in the config folder -
#feel free to change pattern to other types if its variables for other types of scripts.
# for each one it generates variables for completed-script.sh script
#Please read details on completed-script.sh on the forum             
#PLEASE ENSURE PATH FOR BELOW IS ALL CORRECT

currentuser=`whoami`                                                                                       
home=`kde4-config --localprefix|sed -e "s/\.kde\///g"`
config="$home.kde/share/config/"
workdir="$home/scripts/workdir"
scripts="$home/scripts";                                                                                   
                                                                               

completedscript="completed-script.sh";

if [ $currentuser = "root" ]; then echo "error root can not run this, run as normal user"; exit; fi;

mkdir -p $workdir
filetest=`find $workdir/ -name *.notifyrc -print |head -1`
if test -e $filetest                                       
then                                                       
rm $workdir/*.notifyrc                                     
fi                                                         

find $config -name *.notifyrc -exec cp {} $workdir/ ;
cd $workdir;                                           
for filenames in `ls *.notifyrc`                       
do                                                     

cd $workdir;
#make a clean variable without dots hyphens
newvar=`echo $filenames|tr -d "."|tr -d "-"`
newvare=`echo $filenames|sed -e "s/.//g"|sed -e "s/-//g"|sed -e "s/^/$/"`
newvarcont="$newvar""c";                                                   
newvarcontc="$newvare""c";                                                 
value=`sed ':a; /$/N; s/n/ /BOH /; ta' $filenames `                     
cd $scripts;
grep "$newvar=" $completedscript $9 > /dev/null
if [ $? = 0 ]
then
echo "Found variable $newvar not adding again"
else
#add below to insert_rc of completed-script.sh script
ed -s $completedscript /dev/null
g/#insert_rc_path
a
$newvar=$config/$filenames
.
w
g/#insert_RM_here
a
rm $config/$filenames
.
w
g/#insert_variable
a
##start of $newvar
$newvarcont="$value"
echo $newvarcontc > $newvare
##END OF $newvar

.
w
q
EOF

fi

done
cd $scripts
replace.sh " /BOH " " \n " $completedscript
chmod 755 $completedscript



Here is the code for completed-script.sh
have a look out of #insert_rc #inser_RM_here and #insert_variable
this is where the pump variable after doing a search and parsing through the data will output actual variables.

Code: Select all
#!/bin/sh
home=`kde4-config --localprefix|sed -e "s/\.kde\///g"`
config="$home.kde/share/config/"

#insert_rc_path


currentuser=`whoami`
if [ $currentuser = "root" ]; then echo "error root can not run this, run as normal user"; exit; fi;

case "${1}" in
("start")
#insert_RM_here

 kill -9 `pgrep knotify4`
 echo "Ok Notifications for Powerdevil has now been re-enabled "
 ;;
("stop")
#insert_variable

kill -9 `pgrep knotify4`
echo "Ok Notifications for Powerdevil have now been stopped Danke "
 ;;
(*)
 echo "$0 {start|stop}"
 exit
 ;;
esac

Last edited by vahid on Sat Jul 04, 2009 11:49 am, edited 2 times in total.
vahid
Registered Member
Posts
32
Karma
0
Here is the result from me running it and after having one notifyrc file:
As you can see the completed-script.sh has new variables pumped in generated by pump-variable.sh


nutter@muttley:~/scripts$ find ~/.kde/share/config/ -name *.notifyrc -print
/home/nutter/.kde/share/config/powerdevil.notifyrc
nutter@muttley:~/scripts$ ./pump-variables.sh
rm: cannot remove `/home/nutter/scripts/workdir/*.notifyrc': No such file or directory
The string has been replaced, if you have mucked up
you will find the old file in /tmp/completed-script.sh

nutter@muttley:~/scripts$ cat completed-script.sh
#!/bin/sh
user=`who -s|awk '{print $1" "$2}'|grep ":0"|awk '{print $1}'`
home="/home/$user";
config="$home/.kde/share/config/"

#insert_rc_path
powerdevilnotifyrc=$config/powerdevil.notifyrc


currentuser=`whoami`
if [ $currentuser = "root" ]; then echo "error root can not run this, run as normal user"; exit; fi;

case "${1}" in
("start")
#insert_RM_here
rm /home/nutter/.kde/share/config//powerdevil.notifyrc

kill -9 `pgrep knotify4`
echo "Ok Notifications for Powerdevil has now been re-enabled "
;;
("stop")
#insert_variable
##########################start of powerdevilnotifyrc
powerdevilnotifyrcc="[Event/criticalbattery] n Action= n Execute= n KTTS= n Logfile= n n [Event/doingjob] n Action= n Execute= n KTTS= n Logfile= n n [Event/inhibition] n Action= n Execute= n KTTS= n Logfile= n n [Event/joberror] n Action= n Execute= n KTTS= n Logfile= n n [Event/lowbattery] n Action= n Execute= n KTTS= n Logfile= n n [Event/pluggedin] n Action= n Execute= n KTTS= n Logfile= n n [Event/powerdevilerror] n Action= n Execute= n KTTS= n Logfile= n n [Event/profileset] n Action= n Execute= n KTTS= n Logfile= n n [Event/unplugged] n Action= n Execute= n KTTS=n Logfile= n n [Event/warningbattery] n Action= n Execute= n KTTS= n Logfile="
echo $powerdevilnotifyrcc > $powerdevilnotifyrc
##########################END OF powerdevilnotifyrc


kill -9 `pgrep knotify4`
echo "Ok Notifications for Powerdevil have now been stopped Danke "
;;
(*)
echo "$0 {start|stop}"
exit
;;
esac


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], kesang, Sogou [Bot], Yahoo [Bot]