WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [SOLVED] echo command is not quite working the way I prefer  (Read 6381 times)

aus9

  • Guest
[SOLVED] echo command is not quite working the way I prefer
« on: April 21, 2013, 08: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


« Last Edit: April 21, 2013, 11:37:44 PM by aus9 »

aus9

  • Guest
Re: echo command is not quite working the way I prefer
« Reply #1 on: April 21, 2013, 08: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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11668
Re: echo command is not quite working the way I prefer
« Reply #2 on: April 21, 2013, 09: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.

aus9

  • Guest
Re: echo command is not quite working the way I prefer
« Reply #3 on: April 21, 2013, 10: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

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: [SOLVED] echo command is not quite working the way I prefer
« Reply #4 on: April 21, 2013, 03:52:15 PM »
Your echo command is delimited by the ' character, but you are including unquoted ' characters in the echo string.

aus9

  • Guest
Re: [SOLVED] echo command is not quite working the way I prefer
« Reply #5 on: April 21, 2013, 07: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

Offline althalus

  • Sr. Member
  • ****
  • Posts: 351
Re: echo command is not quite working the way I prefer
« Reply #6 on: April 21, 2013, 08: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


Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: echo command is not quite working the way I prefer
« Reply #7 on: April 21, 2013, 08:50:55 PM »
Also,
echo '$VAR'
is not the same as
echo "$VAR"

You should spend some time reading a shell manual.

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: echo command is not quite working the way I prefer
« Reply #8 on: April 21, 2013, 09: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?
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Offline althalus

  • Sr. Member
  • ****
  • Posts: 351
Re: echo command is not quite working the way I prefer
« Reply #9 on: April 21, 2013, 09: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.

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: echo command is not quite working the way I prefer
« Reply #10 on: April 21, 2013, 09:58:03 PM »
Oh, ok, gotcha, had thought that it was copy of script.
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

aus9

  • Guest
Re: echo command is not quite working the way I prefer
« Reply #11 on: April 21, 2013, 11: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

aus9

  • Guest
Re: echo command is not quite working the way I prefer
« Reply #12 on: April 21, 2013, 11:10:16 PM »
edit now irrelevant
« Last Edit: April 22, 2013, 09:36:52 PM by aus9 »

Offline althalus

  • Sr. Member
  • ****
  • Posts: 351
Re: echo command is not quite working the way I prefer
« Reply #13 on: April 21, 2013, 11: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.

aus9

  • Guest
Re: echo command is not quite working the way I prefer
« Reply #14 on: April 21, 2013, 11: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