Question about Shell Script (#!/bin/sh)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#16 Post by jpeps »

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: Select all

#!/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, 05:50, edited 2 times in total.

User avatar
puppyluvr
Posts: 3470
Joined: Sun 06 Jan 2008, 23:14
Location: Chickasha Oklahoma
Contact:

#17 Post by puppyluvr »

:D 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... 8)
Close the Windows, and open your eyes, to a whole new world
I am Lead Dog of the
Puppy Linux Users Group on Facebook
Join us!

Puppy since 2.15CE...

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#18 Post by jpeps »

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

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#19 Post by RSH »

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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#20 Post by jpeps »

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
Attachments
gnewpet.png
(13.62 KiB) Downloaded 998 times
gnewpet-1.1.pet
(1.72 KiB) Downloaded 492 times
Last edited by jpeps on Wed 14 Dec 2011, 09:50, edited 2 times in total.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#21 Post by jpeps »

Here's the code for the frontend:

Code: Select all


#!/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

User avatar
recobayu
Posts: 387
Joined: Wed 15 Sep 2010, 22:48
Location: indonesia

#22 Post by recobayu »

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: Select all

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: Select all

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: Select all

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.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#23 Post by seaside »

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: Select all

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: Select all

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: Select all

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: Select all

    sed -i '/^second/ r text2.txt' text1.txt
Cheers,
s

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#24 Post by jpeps »

seaside wrote: Try this -

Code: Select all

    sed -i '/^second/ r text2.txt' text1.txt
Another addition to my sed notes! :)

User avatar
harii4
Posts: 448
Joined: Fri 30 Jan 2009, 04:08
Location: La Porte City, IA , U.S.A.
Contact:

#25 Post by harii4 »

SOLVED - used an bad pet package - sorry :oops:
found out by more testing.
works great :D
----------------------------------------------------------
gnewpet not working in TXZ_pup? :?

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: Select all

# 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.

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#26 Post by RSH »

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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#27 Post by jpeps »

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.
Attachments
gnewpet.png
(12.75 KiB) Downloaded 836 times
gnewpet-2.1.pet
(1.93 KiB) Downloaded 507 times

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#28 Post by RSH »

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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#29 Post by jpeps »

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.

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#30 Post by RSH »

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) :(

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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#31 Post by jpeps »

RSH..I'm in a rush to get out here, so probably aren't addressing what you're asking. A gui frontend would just create to list from the checkbox. Here's the batch script, which makes multiple pets in /tmp

Code: Select all

#!/bin/sh
 
while read line; do
  newpet "$line"
 done </tmp/newpet-list

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#32 Post by RSH »

This is what i need.

Instead of COMBO using LIST :lol:

I've wrote it all the time --> List :lol:

I thought just about an option to extend those listed entries (add checkbox to each one as attribut or similar?)

Is that possible?
Attachments
solved-this-is-what-ive-searched.jpg
(13.31 KiB) Downloaded 757 times
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#33 Post by jpeps »

RSH wrote:
I thought just about an option to extend those listed entries (add checkbox to each one as attribut or similar?)

Is that possible?
A combobox-checkbox widget. I don't know of any. A workaround would be an editable list where you add marks to entries that you want included. I did something like that in PetCheck.

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#34 Post by RSH »

jpeps wrote:I did something like that in PetCheck.

http://murga-linux.com/puppy/viewtopic. ... 75&t=71341
Downloaded.

I will have look into it.

Thank you.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#35 Post by jpeps »

Here's gnewpet with batch mode include. In batch mode, the file list button brings up an editable pet list. Remove the initial "?" for pets that you want included.

menu fix: Thanks pemasu
Attachments
gnewpet-3.1.pet
(2.11 KiB) Downloaded 539 times
gnewpet.png
(77.23 KiB) Downloaded 681 times
Last edited by jpeps on Sun 15 Jan 2012, 00:40, edited 2 times in total.

Post Reply