I have a file named "open". I open up a terminal, read it's first line into a variable text
and tried to put all the contents of file except for text
in a new file called "open.temp" using grep's pattern inversion. Here's the code:
papagolf@Sierra ~/My Files/My Programs % cat open
# PPPP
QQQQ
RRRR
SSSS
papagolf@Sierra ~/My Files/My Programs % text=`head -n 1 open`
papagolf@Sierra ~/My Files/My Programs % cat open | grep -v $text > open.temp
papagolf@Sierra ~/My Files/My Programs % cat open.temp
QQQQ
RRRR
SSSS
But when I put the same commands in a file called "test.sh", give it executable permission and execute it, it pops an error.
papagolf@Sierra ~/My Files/My Programs % rm open.temp
papagolf@Sierra ~/My Files/My Programs % touch test.sh
papagolf@Sierra ~/My Files/My Programs % nano test.sh
papagolf@Sierra ~/My Files/My Programs % chmod +x test.sh
papagolf@Sierra ~/My Files/My Programs % ./test.sh
grep: PPPP: No such file or directory
papagolf@Sierra ~/My Files/My Programs % cat test.sh
text=`head -n 1 open`
cat open | grep -v $text > open.temp
sed 1d open >open.temp
? – Kusalananda Aug 10 '17 at 12:15grep -v # PPPP
: search for "#" in the file "PPPP". Use quotation marks on"$text"
so that you are grepping for# PPPP
instead of#
inPPPP
. – user4556274 Aug 10 '17 at 12:18sed
actually. :P But what's wrong with my method? It seems to work in the terminal, just not in the file. – 7_R3X Aug 10 '17 at 12:19tail -n +2 open > open.temp
– Stéphane Chazelas Aug 10 '17 at 12:22