Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 03 Sep 2014, 00:58
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Question about Shell Script (#!/bin/sh)
Post new topic   Reply to topic View previous topic :: View next topic
Page 2 of 5 [71 Posts]   Goto page: Previous 1, 2, 3, 4, 5 Next
Author Message
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Sun 11 Dec 2011, 15:36    Post subject:
Subject description: building a new pet from /root/.packages file
 

I refined newpet a bit to read the original pet.specs and make the duplicate without having to repeat the gui building process; also added a few features like searching for the correct name so it's not necessary to remember the exact title; also added cleanup, help menu, and Xdialog notice that the pet is in /tmp .

edit. Added option to update version, "newpet [package] [newname]"
edit2: Removed a tgz2pet related bug.

Code:

#!/bin/sh


## creates a new pet in /tmp of files listed in /root/.package

if [ ! "$1" -o "$1" == "-h" ]; then
  echo 'USEAGE: newpet [package] [newname]
        creates new pet in /tmp
        add [newname] for updating version'
  exit
fi

NAME="$(ls /root/.packages | grep -m1 -i  "$1" | sed 's/.files//')"
if [ ! -f /root/.packages/${NAME}.files ]; then 
   echo "package doesn't exist"
   exit
fi


[ -d /tmp/"$NAME" ] &&  rm -r /tmp/"$NAME"

mkdir /tmp/"$NAME"

for i in `cat /root/.packages/${NAME}.files`; do

echo "$i" >> /tmp/list

done


while read line; do

  if [ -f $line ]; then

  DIR="$(dirname ${line})"
     [ -d /tmp/${NAME}${DIR} ] || mkdir -p /tmp/${NAME}${DIR}
 
     cp -p  "$line" /tmp/${NAME}${DIR}
fi
done </tmp/list

cat /root/.packages/user-installed-packages | grep -m1 "$NAME" >>/tmp/${NAME}/pet.specs 

## Make pet

cd /tmp

## create new name  (if $2 )
if [ "$2" ]; then

   ## edit pet.specs
   SPECS="${NAME}/pet.specs"

  sed -i "s/${NAME}.pet/${2}.pet/" ./"$SPECS"
  newver="$(echo "$2" | tr -cd '0-9.')"
  oldver="$(cat ${SPECS} | cut -d\| -f3)"
  sed -i "s/|${oldver}|/|${newver}|/" ./"$SPECS"
  field1="$(cat ${SPECS} | cut -d\| -f1)"
  sed -i "s/${field1}/${2}/" ./"$SPECS"
   
  NEWNAME="$2"
   
  mv ./$NAME ./$NEWNAME
     NAME="$NEWNAME"
fi

tar -czf ./${NAME}.tgz  ./"$NAME"

tgz2pet ./${NAME}.tgz

if [ -f ${NAME}.pet ]; then
  Xdialog --msgbox "${NAME}.pet created in /tmp" 5 35
fi
## Cleanup

[ -d /tmp/${NAME} ] && rm -r /tmp/${NAME}
[ -f /tmp/list ] && rm -r /tmp/list


Last edited by jpeps on Mon 12 Dec 2011, 01:50; edited 2 times in total
Back to top
View user's profile Send private message 
puppyluvr


Joined: 06 Jan 2008
Posts: 3197
Location: Chickasha Oklahoma

PostPosted: Sun 11 Dec 2011, 19:48    Post subject:  

Very Happy Hello,
@jpeps,
Yea, like that.. Wink
Your script is a bit above me, but if I omit the .files when I call it it works great.
As a test I pulled xfce4 back outta Lupen... Cool

_________________
Close the Windows, and open your eyes, to a whole new world
http://puppylinuxstuff.meownplanet.net/puppyluvr/
Puppy Linux Users Group on Facebook

Puppy since 2.15CE...
Back to top
View user's profile Send private message Visit poster's website 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Sun 11 Dec 2011, 23:39    Post subject:  

puppyluvr wrote:

Your script is a bit above me, but if I omit the .files when I call it it works great.


Yeah..just put in the package name without any".files" extension. The first arg is just a search word anyway, so if you get close it should find the correct package.

example..to update:

"newpet traymanager TrayManager-4.6"

or just:

"newpet traymanager"

for a copy


I just edited it a bit to make the searches in pet.specs a little more specific; also, tgz2pet related bug for updating pets
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Mon 12 Dec 2011, 21:09    Post subject:  

I really don't like using the terminal very much (except in programming processes) and so i did a little work on the first script of jpeps to use this one by a GUI or similar with right-click-action.

I have made a .pet for both of use. Both have language support for english and german.

http://murga-linux.com/puppy/viewtopic.php?t=74164

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Tue 13 Dec 2011, 05:46    Post subject:  

Here's one with a gtkdialog front-end. Select a package from the drop down menu, and then push the "make pet" button for a copy. If you enter a new name, it will make a new version. There's also a button for viewing the files for a selected package.

version 1.1 show only files in package
gnewpet-1.1.pet
Description 
pet

 Download 
Filename  gnewpet-1.1.pet 
Filesize  1.72 KB 
Downloaded  256 Time(s) 
gnewpet.png
 Description   
 Filesize   13.62 KB
 Viewed   875 Time(s)

gnewpet.png


Last edited by jpeps on Wed 14 Dec 2011, 05:50; edited 2 times in total
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Tue 13 Dec 2011, 05:55    Post subject:  

Here's the code for the frontend:


Code:


#!/bin/sh

## gnewpet:   frontend for newpet script.  jpeters (jpeps)  12/13/11
#ver 1.1  12/14/11   show only files

n="0"
cd /root/.packages
for i in `ls | grep ".files" | sed 's/.files//'`; do
n="$((n+1))"
file[$n]="$i"
done
export NUM="$((n+1))"


function funcItemCreate() {
for ((i=1;i<"$NUM";i++)); do
echo '<item>'"${file[$i]}"'</item>'
done
}

Copy() {

newpet ${COMBOBOX} ${NewVersion}

}
export -f Copy

report() {

[ -f /tmp/list ] && rm /tmp/list

VAR=`cat /root/.packages/${COMBOBOX}.files`

for i in $VAR; do
 [ -f "$i" ] && echo "$i" >>/tmp/list
done

Xdialog --stdout --no-cancel --editbox /tmp/list 18 70
rm /tmp/list
}
export -f report

GTKDIALOG=gtkdialog

export MAIN_DIALOG='
<window title="newpet">
<vbox>
  <hbox>
    <text>
      <label>File List:</label>
    </text>
    <combobox>
      <variable>COMBOBOX</variable>
       '"$(funcItemCreate)"'
     
    </combobox>
  </hbox>
  <hbox>
     <text><label>New Version</label></text>
     <entry tooltip-text="Leave blank for same version">
          <variable>NewVersion</variable>
     </entry>
  </hbox>
  <hbox>
    <button ok></button>
    <button cancel></button>
    <button>
         <label>Show Files</label>
         <action>report</action>
    </button>
    <button>
         <label>Make Pet</label>
         <action>Copy</action>
    </button>
  </hbox>
</vbox>
</window>
'
$GTKDIALOG --program=MAIN_DIALOG
Back to top
View user's profile Send private message 
recobayu


Joined: 15 Sep 2010
Posts: 112
Location: indonesia

PostPosted: Thu 15 Dec 2011, 04:57    Post subject:  

jpeps, thanks a lot. that is very interesting for me.
now, i have a question again.
if i have a text1 file that contain
Code:

first line of text1
second line of text1
thirth line of text1
fourth line of text1
fifth line of text1


and i have a text2 file that contain
Code:

line 1 of text2
line 2 of text2
line 3 of text2
line 4 of text2
line 5 of text2


how can the text1 file to be like this?
Code:

first line of text1
second line of text1
line 1 of text2
line 2 of text2
line 3 of text2
line 4 of text2
line 5 of text2
thirth line of text1
fourth line of text1
fifth line of text1


can i use sed or echo? or grep?
thanks.
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Thu 15 Dec 2011, 11:58    Post subject:  

recobayu wrote:
jpeps, thanks a lot. that is very interesting for me.
now, i have a question again.
if i have a text1 file that contain
Code:

first line of text1
second line of text1
thirth line of text1
fourth line of text1
fifth line of text1


and i have a text2 file that contain
Code:

line 1 of text2
line 2 of text2
line 3 of text2
line 4 of text2
line 5 of text2


how can the text1 file to be like this?
Code:

first line of text1
second line of text1
line 1 of text2
line 2 of text2
line 3 of text2
line 4 of text2
line 5 of text2
thirth line of text1
fourth line of text1
fifth line of text1


can i use sed or echo? or grep?
thanks.

recobayu,

Try this -
Code:
    sed -i '/^second/ r text2.txt' text1.txt


Cheers,
s
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Fri 16 Dec 2011, 00:27    Post subject:  

seaside wrote:

Try this -
Code:
    sed -i '/^second/ r text2.txt' text1.txt



Another addition to my sed notes! Smile
Back to top
View user's profile Send private message 
harii4


Joined: 30 Jan 2009
Posts: 444
Location: La Porte City, IA , U.S.A.

PostPosted: Sun 01 Jan 2012, 11:37    Post subject:  

SOLVED - used an bad pet package - sorry Embarassed
found out by more testing.
works great Very Happy
----------------------------------------------------------
gnewpet not working in TXZ_pup? Confused

i'm getting no files in the new pet - empty
The gnewpet gui - "show files" is empty as well.
might be an TXZ_pup thing?
Using straight DotPet - did not convert using pet2slack.

console:
Code:
# gnewpet
cat: /root/.packages/user-installed-packages: No such file or directory

_________________
3.01 Fat Free / Fire Hydrant featherweight/ TXZ_pup / 431JP2012
----------------------------------------------------------------------------------------
Peace and Justice are two sides of the same coin.
Back to top
View user's profile Send private message Visit poster's website 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Wed 11 Jan 2012, 23:25    Post subject:  

Hi,

i have found out, that the scripts here posted don't bring you the installed .pet back as it has been installed.

Every symbolic link comes back as a real file. Though the created .pet may mostly work - they are just a little bigger as the installed one (the more links the more real files will be written instead of the links)

BUT, no problem after all: it is easy to fix!

Just change "cp -p "$line" /tmp/${NAME}${DIR}" to this: "cp -p -P "$line" /tmp/${NAME}${DIR}".

-P says "don't follow symbolic links". So symbolic links will be re-copied as symbolic links inside the .pet.

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Fri 13 Jan 2012, 02:30    Post subject:  

Thanks RSH. Bug corrected. I also have a checkbox to leave the copied directory in /tmp for other possible file additions/deletions. As before, for a straight copy leave the new version entry blank.
gnewpet.png
 Description   
 Filesize   12.75 KB
 Viewed   677 Time(s)

gnewpet.png

gnewpet-2.1.pet
Description 
pet

 Download 
Filename  gnewpet-2.1.pet 
Filesize  1.93 KB 
Downloaded  279 Time(s) 
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Fri 13 Jan 2012, 09:29    Post subject:  

jpeps wrote:
Here's the code for the frontend:


Code:


#!/bin/sh

## gnewpet: frontend for newpet script. jpeters (jpeps) 12/13/11
#ver 1.1 12/14/11 show only files

n="0"
cd /root/.packages
for i in `ls | grep ".files" | sed 's/.files//'`; do
n="$((n+1))"
file[$n]="$i"
done
export NUM="$((n+1))"


function funcItemCreate() {
for ((i=1;i<"$NUM";i++)); do
echo '<item>'"${file[$i]}"'</item>'
done
}

Copy() {

newpet ${COMBOBOX} ${NewVersion}

}
export -f Copy

report() {

[ -f /tmp/list ] && rm /tmp/list

VAR=`cat /root/.packages/${COMBOBOX}.files`

for i in $VAR; do
[ -f "$i" ] && echo "$i" >>/tmp/list
done

Xdialog --stdout --no-cancel --editbox /tmp/list 18 70
rm /tmp/list
}
export -f report

GTKDIALOG=gtkdialog

export MAIN_DIALOG='
<window title="newpet">
<vbox>
<hbox>
<text>
<label>File List:</label>
</text>
<combobox>
<variable>COMBOBOX</variable>
'"$(funcItemCreate)"'

</combobox>
</hbox>
<hbox>
<text><label>New Version</label></text>
<entry tooltip-text="Leave blank for same version">
<variable>NewVersion</variable>
</entry>
</hbox>
<hbox>
<button ok></button>
<button cancel></button>
<button>
<label>Show Files</label>
<action>report</action>
</button>
<button>
<label>Make Pet</label>
<action>Copy</action>
</button>
</hbox>
</vbox>
</window>
'
$GTKDIALOG --program=MAIN_DIALOG


Hi jpeps.

This gui is more comfortable as the one i did made using your first script. My suggestion is:

what must changed or added in the gui to get the same list but with checkboxes to select several (is this right for 'more than 1?) files to rebuild?

I think than you would have one of the most useful and ultimate tool - not only to "cannibalize" the work of others but also to get pets, that somehow get lost, out of iso files.

And at least: it would be exactly (just modified, that's actually mostly all what i am able to do) what i need on a work not only useful for my LazY Puppy but also useful to promote and support puppy in principle.

I know it has to be done some work on the backend, but therefor (my work) i do need only the gui code.

Thank you in advance for your excellent code.

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Fri 13 Jan 2012, 13:13    Post subject:  

RSH wrote:


what must changed or added in the gui to get the same list but with checkboxes to select several (is this right for 'more than 1?) files to rebuild?


You could of course do that, but why not build them one at a time? It seems much simpler and easier to control, especially when you want to rename versions.

To do a list, it would be trivial to just add a loop.
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Fri 13 Jan 2012, 14:22    Post subject:  

RSH wrote:
...i do need only the gui code.


I meant: i am not interested in the backend and/or to build tons of files out of an iso at a time.

The issue is following:

1. Reading a directory
- while reading creating list (.txt file) and writing names of found files into this list file

2. Read the list file and show completely in a GUI to select or deselect items of list (incl. all/none - button)

3. Save list.

4. Read list and create symbolic links of only the enabled listed files.

I can do 1., 3. and 4.

I can do 2. only using geany, want to do it in a GUI - more comfortable when using all/none button/s and then select or deselect some. To use the list in your gui without changes would mean to click to open list, scroll to the item and click it. Makes two clicks at one item. If there are 200 files listed and i want to use only 3 of them i would have to click 2x197 clicks to get all edited - so, no GUI comfort. (similar is geany using keyboard) Sad

How can i create an gtkdialog item, that is a scroll-able list like yours, but already opened and just adding a checkbox to every item?

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 2 of 5 [71 Posts]   Goto page: Previous 1, 2, 3, 4, 5 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1148s ][ Queries: 12 (0.0069s) ][ GZIP on ]