Tiny Core Linux

General TC => General TC Talk => Topic started by: aus9 on April 21, 2013, 05:11:16 AM

Title: [SOLVED] echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 05:11:16 AM
Ok I have tried echo  as in /bin/echo and loading coreutils  for /usr/local/bin/echo

I am trying to create a script inside a build script

The script is being built but has the appearance attempting to action as well

first the working bit.......which acts correctly is
Code: [Select]
mkdir -p cups/usr/local/tce.installed
root@box:/tmp# echo '#!/bin/sh
>
> if [ ! -d /usr/local/etc/cups ]; then
>   mkdir -p /usr/local/etc/cups
> fi
>
> if [ ! -f /usr/local/etc/cups/cupsd.conf ]; then
>   cp -p /usr/local/share/cups/files/cupsd.conf /usr/local/etc/cups
> fi
> ' > $T

Now the bad , the real text file in a code box for display purposes only

Code: [Select]
mkdir -p cups/usr/local/etc/init.d
#######################
/usr/local/bin/echo '#!/bin/sh

start() {
if ps | awk '{print $3}' | grep -e "dbus-daemon" >/dev/null; then
/usr/local/etc/init.d/dbus restart
start-stop-daemon --start --exec /usr/local/sbin/cupsd 2>/dev/null
sleep 2
else
/usr/local/etc/init.d/dbus start
start-stop-daemon --start --exec /usr/local/sbin/cupsd 2>/dev/null
sleep 2
fi
}

stop() {
start-stop-daemon --stop --exec /usr/local/sbin/cupsd 2>/dev/null
sleep 2
}

status() {
if ps | awk '{print $3}' | grep -e "/usr/local/sbin/cupsd" >/dev/null; then
echo "cups is running."
exit 0
else
echo "cups is not running."
exit 1
fi
}

case $1 in
start) start
;;
stop) stop
;;
status) status
;;
restart) stop; start
;;
*) echo -e "\n$0 [start|stop|restart|status]\n"
;;
esac
' > $I

The effect of copy and paste into a terminal is horrible and I can't grab it all but a snippet is

Code: [Select]
dumpkmap                     klogd                        pg                           stdbuf                       zsync
dumpleases                   lame                         pgawk                        strings                      zsyncmake
dump.p                     last                         pgawk-4.0.0                  strip
> ;;
> *) echo -e "\n$0 [start|stop|restart|status]\n"
>
2to3                         dvipdf                       ld                           pgrep                        stty

To my untrained eyes, it looks like its trying to action my paste instead of respecting the first part of the echo command

2)  If this can not be easily resolved, feel free to suggest a better way to add scripts inside a build script.

Be aware I am a simple coder so an example may assist

thanks for reading


Title: Re: echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 05:30:40 AM
its also destroying the correct layout for my tcz.info files if I use the build script and the echo feature

I am too tired to see my mistakes
Title: Re: echo command is not quite working the way I prefer
Post by: Rich on April 21, 2013, 06:56:20 AM
Hi aus9
Quote
its also destroying the correct layout for my tcz.info files if I use the build script and the echo feature
See the attachment here:
http://forum.tinycorelinux.net/index.php/topic,15046.msg86197.html#msg86197
for another way of creating your info files.
Title: Re: echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 07:05:59 AM
Rich

thanks heaps for that, but at the moment I can't use it for cups stuff.

cups split at different dates into cups-dev libcups etc.......so the info files have different changelogs

For the moment I will just create the text files the old way using leafpad and add them to /tmp for when I run submitqc4
Title: Re: [SOLVED] echo command is not quite working the way I prefer
Post by: gerald_clark on April 21, 2013, 12:52:15 PM
Your echo command is delimited by the ' character, but you are including unquoted ' characters in the echo string.
Title: Re: [SOLVED] echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 04:17:36 PM
gerald_clark or others

I am not sure what that means.

I thought I was echoing the contents of a text file to create the contents and name of a file.

I used this method but using ' or " both seem to work ok
http://www.linuxquestions.org/questions/linux-software-2/echo-multiple-lines-of-text-312048/#post1585203
Code: [Select]
echo 'line 1
>     line 2
> ' > /tmp/zz
tc@box:~$ cat /tmp/zz
line 1
    line 2

That is, space is not truncated. So it should work for an info file, and in the recent past it has.

Questions, if anyone has time
Should I change my build script to /bin/bash and load bash?

If I define a number of variables, does that affect /bin/sh and cause it to bork a little?

###########

I can easily go back to my old method for building the info files outside the build script

I could upload any tce.installed or init script and have the script download it
----but I would prefer to have it all in the script.

Thanks for reading
Title: Re: echo command is not quite working the way I prefer
Post by: althalus on April 21, 2013, 05:24:17 PM
From what I can see, the script you are trying to create uses ' and " inside it. So, either escape these marks (ie, inside the echo, use \' and \" instead of ' or " - which is what gerald_clark was refering to) or use something more robust for quoting multi line strings. For example (ash on a raspberry pi, should work almost regardless of shell though):

Code: [Select]
tc@pibox:~$  cat > infofile <<EOF
> hello
> this is a string
> EOF
tc@pibox:~$ cat infofile
hello
this is a string

Title: Re: echo command is not quite working the way I prefer
Post by: gerald_clark on April 21, 2013, 05:50:55 PM
Also,
echo '$VAR'
is not the same as
echo "$VAR"

You should spend some time reading a shell manual.
Title: Re: echo command is not quite working the way I prefer
Post by: tinypoodle on April 21, 2013, 06:22:01 PM
Code: [Select]
tc@pibox:~$  cat > infofile <<EOF
> hello
> this is a string
> EOF
tc@pibox:~$ cat infofile
hello
this is a string

What exactly is the purpose of those ">" preceding each line?
Title: Re: echo command is not quite working the way I prefer
Post by: althalus on April 21, 2013, 06:48:39 PM
What exactly is the purpose of those ">" preceding each line?
That was a copy and paste from terminal. I've always assumed they're visual indicators to just remind you this line is continuing the previous command. The majority of shells and interpreters I've used do something similar.
Title: Re: echo command is not quite working the way I prefer
Post by: tinypoodle on April 21, 2013, 06:58:03 PM
Oh, ok, gotcha, had thought that it was copy of script.
Title: Re: echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 08:06:34 PM
althalus

I test.....eg input text = (in a code box to show space)
Code: [Select]
title: line 1
line 2
EOF

then do a command based on your method (copy and paste from terminal input and results)
Code: [Select]
cat > /tmp/test << EOF
> title: line 1
> line 2
> EOF

cat /tmp/test
title: line 1
line 2

Input is truncating space.

gerald_clark
You are right, but my time is limited by my learning skills, good looks or thereof and time issues
Title: Re: echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 08:10:16 PM
edit now irrelevant
Title: Re: echo command is not quite working the way I prefer
Post by: althalus on April 21, 2013, 08:16:27 PM
Input is truncating space.
If you pasted in those extra lines, the truncating happens in the paste. Same thing happens if you use echo "" and paste multiple lines.

also I gather, if I had no internal 'string' or "string" my original method might just work?
Yes, but that's obviously going to be problematic for cases like the init script in your initial post.
Title: Re: echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 08:26:23 PM
thanks everyone so far.

I can get around it, with my current skills

I use the echo ' string with spaces' > /pathway/file

then if I encounter " or ' internally, I do one line at time with an append

echo '  blah  "string"  blah ' >> /samefile

Title: Re: echo command is not quite working the way I prefer
Post by: aus9 on April 21, 2013, 08:36:20 PM
EDIT

thought I had it, but it borked again

Will now upload text file and use wget to get it etc

Unless there are other replies I will mark this as solved for now

thanks everyone