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 Fri 28 Nov 2014, 10:49
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 1 of 5 [71 Posts]   Goto page: 1, 2, 3, 4, 5 Next
Author Message
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Thu 08 Dec 2011, 15:28    Post subject:  Question about Shell Script (#!/bin/sh)  

Hello,

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

Code:
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

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


Joined: 23 Oct 2006
Posts: 1287
Location: Ukraine

PostPosted: Thu 08 Dec 2011, 15:50    Post subject:  

Dear RSH,

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

Code:
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
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2386
Location: UK

PostPosted: Thu 08 Dec 2011, 15:55    Post subject: Re: Question about Shell Script (#!/bin/sh)  

IF I understand what you want correctly,
Code:
echo $scriptline >$isopname.files/build-script.sh

should simply be

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

_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Thu 08 Dec 2011, 16:27    Post subject:  

Hello!

Thank you for your fast reply.

Quote:
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? Embarassed

Thank you very much again, vovchik and sc0ttman. Nice to have you (and the forum within all its members as well). Very Happy

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


Joined: 20 Jun 2010
Posts: 3972
Location: Kiel,Germany

PostPosted: Thu 08 Dec 2011, 16:38    Post subject:  

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:
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 .
Back to top
View user's profile Send private message Visit poster's website 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Fri 09 Dec 2011, 12:29    Post subject:  

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:
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.
Back to top
View user's profile Send private message 
recobayu


Joined: 15 Sep 2010
Posts: 114
Location: indonesia

PostPosted: Fri 09 Dec 2011, 19:26    Post subject: script to repackage after download with ppm  

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:
Quote:

/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
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Fri 09 Dec 2011, 19:48    Post subject: Re: script to repackage after download with ppm  

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:
Quote:

/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 Fri 09 Dec 2011, 20:23; edited 1 time in total
Back to top
View user's profile Send private message 
puppyluvr


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

PostPosted: Fri 09 Dec 2011, 20:05    Post subject:  

Very Happy 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
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: Fri 09 Dec 2011, 20:48    Post subject:  

More sophisticated: Usage newpet [name]

Makes [name].pet in /tmp


Code:

#!/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 Fri 09 Dec 2011, 21:10; edited 3 times in total
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Fri 09 Dec 2011, 20:54    Post subject:  

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

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


Joined: 15 Sep 2010
Posts: 114
Location: indonesia

PostPosted: Fri 09 Dec 2011, 21:15    Post subject:  

thanks jpep, now i have aqualung.pet
i like coding, it makes fast. step by step i learn bash programming from this forum. Very Happy
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5042
Location: Arizona, U.S.A.

PostPosted: Fri 09 Dec 2011, 21:47    Post subject:  

Or you can gather all the lines ( the whole file ) into a variable.
Code:
echo "(Text)" > (File)

This writes the entire multi. lined file in one shot.
The double quotes " " are needed to preserve the line-feeds.
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 3972
Location: Kiel,Germany

PostPosted: Fri 09 Dec 2011, 22:32    Post subject:  

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 Very Happy Laughing Very Happy

To create a log file it is pssible to use the
Code:
logsave -a logfile.log COMMAND

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

OR
Code:
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 ) .
Back to top
View user's profile Send private message Visit poster's website 
PANZERKOPF

Joined: 16 Dec 2009
Posts: 280
Location: Earth

PostPosted: Sat 10 Dec 2011, 13:26    Post subject: Re: Question about Shell Script (#!/bin/sh)  

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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 5 [71 Posts]   Goto page: 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.1044s ][ Queries: 12 (0.0087s) ][ GZIP on ]