0

I need to create a new file name test.sh. I need to write those lines with echo or somethings else but cant open new file manually and write it. That is, I have to write down some things in the bash such a way that the following file is created:

TEST_VALUE=$1
if [[cat data | grep $TEST_VALUE]]; then
exit 1
fi
exit 0

But, when I do that by echo the result is:

TEST_VALUE=
if [[]]; then
    exit 1
fi
exit 0

I need the file as I write it with $1 and not the argument and with the grep. I tried to grep each row but it is doing the command and not copied it as I want. How do I do it? Thank You

Try_hard
  • 5
  • 4
  • 1
    Can you post an example of the bash script that is generating this? Hard to say exactly what you're doing wrong without seeing it, but most likely you're not using quotes properly. See for example [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – tjm3772 Nov 19 '22 at 21:22
  • 2
    As an aside, the generated file won't do what you want. You're probably looking for `if grep -q "$TEST_VALUE" data; then` – Benjamin W. Nov 19 '22 at 21:24
  • Sure, this is the line that needs to go to tast.sh: git bisect run ./test.sh $TEST_VALUE – Try_hard Nov 19 '22 at 21:29
  • I need to write in bash the line that is according to the lines above and will copy it to test.sh as I wrote it – Try_hard Nov 19 '22 at 21:31
  • I need to write in bash according to the lines above and it will copy it to test.sh as I wrote it – – Try_hard Nov 19 '22 at 21:38
  • 2
    `[[ ]]` will _never_ do what you're asking it to do, because that's not what it's meant for. It's syntax that provides an extended version of the `[` command, not part of `if` syntax. – Charles Duffy Nov 19 '22 at 21:39
  • You _could_ write, say, `[[ $( – Charles Duffy Nov 19 '22 at 21:43
  • Related: [How to `cat < – Charles Duffy Nov 19 '22 at 21:44
  • (also, `[[` is syntax for extended shells like bash or ksh; if you don't put a shebang at the top of your script specifying an extended shell, it's not guaranteed it'll be available at all). – Charles Duffy Nov 19 '22 at 21:47
  • ...try running `if [[ 'false' ]]; then echo true; else echo false; fi` -- you'll see it emits `true`, because the string `false`, _like any other non-empty string_, is truthy. – Charles Duffy Nov 21 '22 at 15:44

2 Answers2

1

Ignoring that the content you're trying to copy is buggy, the best way to do this is with a quoted heredoc:

cat >file <<'EOF'
TEST_VALUE=$1
if [[cat data | grep $TEST_VALUE]]; then
exit 1
fi
exit 0
EOF

But that content is buggy! A better version would look like:

cat >file <<'EOF'
#!/bin/sh
test_value=$1
! grep -q -e "$test_value" <data
EOF
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0
echo 'TEST_VALUE=$1 
if [[cat data | grep $TEST_VALUE]]; then
exit 1
fi
exit 0' > test.sh

You need to use quotes, in any other case your command will be executed.

mbofos01
  • 51
  • 1
  • 1
  • 10
  • One can (and should) use a quoted heredoc. The sample here falls down whenever one wants to use single quotes inside the value being copied. – Charles Duffy Nov 19 '22 at 21:40
  • It also can do badly on data containing backslashes, depending on exactly which version of `echo` is in use. – Charles Duffy Nov 19 '22 at 21:40
  • 1
    ...see [Why is printf better than echo?](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo) for detailed guidance -- or just [the POSIX standard for `echo`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html), particularly the APPLICATION USAGE and RATIONALE sections. – Charles Duffy Nov 19 '22 at 21:46
  • and how do I copy the '' from the if [['cat data | grep $TEST_VALUE']]; then ? – Try_hard Nov 19 '22 at 21:58
  • @Try_hard, `if [[ 'anything' ]]` doesn't look at `anything` at a command, it just looks at it _as a string_ so it always returns true, no matter what `anything` would do if run as a shell command. This is part of why I told you that the command you're trying to run is buggy. – Charles Duffy Nov 20 '22 at 14:48
  • @Try_hard, ...that said, you can insert a literal single quote inside a single-quoted string using `'"'"'` -- the first `'` ends the prior single-quoted context, the following `"` enters a double-quoted context, the third character `'` is inside double quotes so it's literal, the fourth character `"` ends the double-quoted context, the fifth character `'` resumes a single-quoted context. – Charles Duffy Nov 20 '22 at 14:50