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
User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

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

#1 Post by RSH »

Hello,

I want to know how can i write multiple lines into a text file via "echo" (or is there another command existing)?

Code: Select all

echo $scriptline >$isopname.files/build-script.sh
writes one line into the text file '.../build-script.sh'. If i use this command again the text file '.../build-script.sh' will be deleted and the new line is written in the new text file '.../build-script.sh'.

How can i get multiple lines into the same text file?

Need it urgently!

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]

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#2 Post by vovchik »

Dear RSH,

It is simple and you almost have it. Just use the following:

Code: Select all

echo $scriptline >>$isopname.files/build-script.sh
Notice that the ">" symbol is doubled ">>", which means the information is appended without deleting the original.

With kind regards,
vovchik

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

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

#3 Post by sc0ttman »

IF I understand what you want correctly,

Code: Select all

echo $scriptline >$isopname.files/build-script.sh
should simply be

Code: Select all

echo "$scriptline" >> $isopname.files/build-script.sh
Note the >> which means 'append to the end of the file'...
Also, I added "" double quotes around $scriptline, in case in contains spaces or newlines

The double quotes mean that $scriptline can be more than one line...

Code: Select all

# VAR="hello
new line"
# echo $VAR
hello new line
# echo "$VAR"
hello
new line
#
Hope this helps... I'm sure others can help more...

EDIT: Just seen vovchiks reply, too...
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

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

#4 Post by RSH »

Hello!

Thank you for your fast reply.
It is simple and you almost have it. Just use the following.

Code:
echo $scriptline >>$isopname.files/build-script.sh
Oh yes, i know i have seen this before in /usr/sbin/pfbpanel as i modified the menu of my puppy.

Why didn't i realized this? :oops:

Thank you very much again, vovchik and sc0ttman. Nice to have you (and the forum within all its members as well). :D
[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]

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#5 Post by Karl Godt »

Another solution would be something like this using cat instead of echo :
# cat >>file.ext <<EOT
> #!/bin/sh
> echo "$0 START"
> echo "Now doing some code here"
> echo "$0" END
> EOT
# cat file.ext
#!/bin/sh
echo "/bin/bash START"
echo "Now doing some code here"
echo "/bin/bash" END
#

Code: Select all

cat >>file.ext <<EOT
#!/bin/sh
echo "$0 START"
echo "Now doing some code here"
echo "$0" END
EOT
'>' and '>>' works exactly the same as for echo .

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

#6 Post by jpeps »

Karl Godt wrote:Another solution would be something like this using cat instead of echo :
# cat >>file.ext <<EOT
> #!/bin/sh
> echo "$0 START"
> echo "Now doing some code here"
> echo "$0" END
> EOT
# cat file.ext
#!/bin/sh
echo "/bin/bash START"
echo "Now doing some code here"
echo "/bin/bash" END
#

Code: Select all

cat >>file.ext <<EOT
#!/bin/sh
echo "$0 START"
echo "Now doing some code here"
echo "$0" END
EOT
'>' and '>>' works exactly the same as for echo .
That's interesting. "file.ext" keeps writing to itself every time you run it.

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

script to repackage after download with ppm

#7 Post by recobayu »

hi everyone,
i'm sorry where to put my question, i hope in this thread is ok.
i finished download aqualung with ppm, and it was installed on my laptop.
now, i look at /root/.package/aqualung-0.9b11-Lucid.files there are many rows representating the file i download.
it's like this:
/usr/
/usr/local/
/usr/local/lib/
/usr/local/lib/X11/
/usr/local/lib/X11/mini-icons/
/usr/local/lib/X11/mini-icons/aqualung.png
/usr/lib/
/usr/lib/libmpcdec.so.3.1.1
/usr/lib/libmpcdec.so.3
...
actually, i can put them one by one. but if there are some script, it can be more fast and interesting.
how to repackage again in a pet or a folder with a script? so i don't need download that aqualung again.
thanks

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

Re: script to repackage after download with ppm

#8 Post by jpeps »

recobayu wrote:hi everyone,
i'm sorry where to put my question, i hope in this thread is ok.
i finished download aqualung with ppm, and it was installed on my laptop.
now, i look at /root/.package/aqualung-0.9b11-Lucid.files there are many rows representating the file i download.
it's like this:
/usr/
/usr/local/
/usr/local/lib/
/usr/local/lib/X11/
/usr/local/lib/X11/mini-icons/
/usr/local/lib/X11/mini-icons/aqualung.png
/usr/lib/
/usr/lib/libmpcdec.so.3.1.1
/usr/lib/libmpcdec.so.3
...
actually, i can put them one by one. but if there are some script, it can be more fast and interesting.
how to repackage again in a pet or a folder with a script? so i don't need download that aqualung again.
thanks
mkdir /tmp/aqualung
cd /tmp/aqualung
cp -p /usr/local/lib/X11/mini-icons/aqualung.png .
cp -p /usr/lib/libmpcdec.so.3* .
...
Last edited by jpeps on Sat 10 Dec 2011, 00:23, edited 1 time in total.

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

#9 Post by puppyluvr »

:D Hello,
I dont believe that is a complete list, as the binaries etc are not listed..
Add them all before making a pet..

Anyone want to help with a script to read the final /* lines in any /root/.packages file, then copy the actual files to a dir...
RePet-1.0
I cant remember how to get cat to ignore things...
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

#10 Post by jpeps »

More sophisticated: Usage newpet [name]

Makes [name].pet in /tmp

Code: Select all

#!/bin/sh

if [ ! -f /root/.packages/${1}.files ]; then
      echo "package doesn't exist"
     exit
 fi

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

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

mkdir /tmp/"$1"

for i in `cat /root/.packages/${1}.files`; do
echo "$i" >> /tmp/list
done

while read line; do 

  if [ -f $line ]; then

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

cd /tmp
dir2pet "$1"

Last edited by jpeps on Sat 10 Dec 2011, 01:10, edited 3 times in total.

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

#11 Post by RSH »

jpeps wrote:That's interesting. "file.ext" keeps writing to itself every time you run it.
Does it mean, this could be used to create a log file or is it an unwanted 'loop'?

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]

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

#12 Post by recobayu »

thanks jpep, now i have aqualung.pet
i like coding, it makes fast. step by step i learn bash programming from this forum. :D

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#13 Post by sunburnt »

Or you can gather all the lines ( the whole file ) into a variable.

Code: Select all

echo "(Text)" > (File)
This writes the entire multi. lined file in one shot.
The double quotes " " are needed to preserve the line-feeds.

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#14 Post by Karl Godt »

RSH wrote:
jpeps wrote:That's interesting. "file.ext" keeps writing to itself every time you run it.
Does it mean, this could be used to create a log file or is it an unwanted 'loop'?
RSH
I always copy the code into the console first .

This was not intended to create any loops at all , but i will play around with it :D :lol: :D

To create a log file it is pssible to use the

Code: Select all

logsave -a logfile.log COMMAND
command
OR

Code: Select all

echo 'DBG : what i wanna say here' >> logfile.log
OR

Code: Select all

cat>logtest.sh <<EndOfText
exec 1>>logfile.log 2>>error.log
echo 'DBG : what i wanna say here'
EndOfText
source logtest.sh &
sleep 1
cat logfile.log
cat error.log
which is the simpliest solution as far as i can tell (look into /etc/rc.d/rc.sysinit for an example [ "$LOGLEVEL" ] || exec 1>/tmp/bootsysinit.log 2>&1 ) .

PANZERKOPF
Posts: 282
Joined: Wed 16 Dec 2009, 21:38
Location: Earth

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

#15 Post by PANZERKOPF »

RSH wrote:I want to know how can i write multiple lines into a text file via "echo" (or is there another command existing)?
Yet another solution:
echo -e "line1\nline2\nline3\nlineN"
SUUM CUIQUE.

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.

Post Reply