SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2014-01-14 15:42:46

benhee
Member
From: North West UK.
Registered: 2009-10-28
Posts: 41

basic (simple?) shell script / cli question (resolved)

Hi

I've got 40 or so single line text files (notes taken from a an old Nokia phone)
I want to append these files to a single file.

cat Nokia*.txt >> notes

works fine, to give a space between lines, I use on the cli

cat Nokia*.txt >> notes |  sed 'G' notes > notes

this works fine and produces the desired result.

when I make a a two line shell script to do this, using the same lines

#!/bin/bash

cat Nokia*.txt >> notes
sed 'G' notes > notes

the result is an empty file. I would be grateful for an explanation as to the reason for this, anyone help?

Doing

#!/bin/bash

cat Nokia*.txt >> notes
sed 'G' notes >> notes

produces a file with the the list and then the list double spaced, I can understand this.
and

#!/bin/bash

cat Nokia*.txt >> notes1
sed 'G' notes1 > notes
rm notes1

gives me the file 'notes' as wanted, I can understand this, it's the different results from the apparently same lines
on the cli and then the two line shell script which puzzles me.

Last edited by benhee (2014-01-14 20:19:28)

Offline

Help fund CrunchBang, donate to the project!

#2 2014-01-14 15:45:54

brontosaurusrex
#! Red Menace
Registered: 2012-06-15
Posts: 1,643

Re: basic (simple?) shell script / cli question (resolved)

Your oneliner is a pipe and twoliner isn't. edit: ignore me.

Last edited by brontosaurusrex (2014-01-14 15:47:23)

Offline

#3 2014-01-14 15:46:34

iann
#! Die Hard
Registered: 2010-09-10
Posts: 720

Re: basic (simple?) shell script / cli question (resolved)

Your shell script is not the same as your command line.  The | operator is not the same as putting the command on two lines.  Simply use the same operator in your shell script with a single line.

The | operator is essentially what you did in your last example, which is to feed the output of the first command into the second command.

Offline

#4 2014-01-14 16:01:47

benhee
Member
From: North West UK.
Registered: 2009-10-28
Posts: 41

Re: basic (simple?) shell script / cli question (resolved)

thanks Iann and Bronto (will ignore, but why?), more study required by me to understand the differences...good fun....

Offline

#5 2014-01-14 18:11:49

aiBo
#! CrunchBanger
Registered: 2010-11-08
Posts: 243

Re: basic (simple?) shell script / cli question (resolved)

If you want to operate on one and the same file instead of reading from one file to another, you have to use the "-i" flag with sed. Like this:

sed -i 'G' notes

Offline

#6 2014-01-14 20:18:48

benhee
Member
From: North West UK.
Registered: 2009-10-28
Posts: 41

Re: basic (simple?) shell script / cli question (resolved)

thanks aiBo

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo