heredoc

Executing multi-line statements in the one-line command-line?

倖福魔咒の 提交于 2019-11-26 06:08:24
I'm using Python with -c to execute a one-liner loop, i.e.: $ python -c "for r in range(10): print 'rob'" This works fine. However, if I import a module before the for loop, I get a syntax error: $ python -c "import sys; for r in range(10): print 'rob'" File "<string>", line 1 import sys; for r in range(10): print 'rob' ^ SyntaxError: invalid syntax Any idea how this can be fixed? It's important to me to have this as a one-liner so that I can include it in a Makefile. jspcal you could do echo -e "import sys\nfor r in range(10): print 'rob'" | python or w/out pipes: python -c "exec(\"import sys

How to assign a heredoc value to a variable in Bash?

半世苍凉 提交于 2019-11-26 05:56:56
问题 I have this multi-line string (quotes included): abc\'asdf\" $(dont-execute-this) foo\"bar\"\'\' How would I assign it to a variable using a heredoc in Bash? I need to preserve newlines. I don\'t want to escape the characters in the string, that would be annoying... 回答1: You can avoid a useless use of cat and handle mismatched quotes better with this: $ read -r -d '' VAR <<'EOF' abc'asdf" $(dont-execute-this) foo"bar"'' EOF If you don't quote the variable when you echo it, newlines are lost.

In PHP, what does “<<<” represent?

蓝咒 提交于 2019-11-26 04:48:18
问题 For example: $sql = <<<MySQL_QUERY 回答1: That's heredoc syntax. You start a heredoc string by putting <<< plus a token of your choice, and terminate it by putting only the token (and nothing else!) on a new line. As a convenience, there is one exception: you are allowed to add a single semicolon after the end delimiter. Example: echo <<<HEREDOC This is a heredoc string. Newlines and everything else is preserved. HEREDOC; 回答2: It is the start of a string that uses the HEREDOC syntax. A third

How to avoid heredoc expanding variables? [duplicate]

梦想的初衷 提交于 2019-11-26 04:43:49
问题 This question already has answers here : How to cat <<EOF >> a file containing code? (3 answers) Closed last year . I\'m trying to create a script file using substitution string from ENV but want also to prevent some from escaping export PLACEHOLDER1=\"myPlaceholder1Value\" export PLACEHOLDER2=\"myPlaceholder2Value\" sudo /bin/su -c \"cat << EOF > /etc/init.d/my-script #!/bin/bash # ### BEGIN INIT INFO # Provides: my-script # Required-Start: \\$remote_fs \\$syslog # Required-Stop: \\$remote

PHP expression <<<EOB

蹲街弑〆低调 提交于 2019-11-26 03:44:54
问题 I\'ve been developing with PHP for some years now, and recently came across this code: <?php echo <<<EOB <html> <head> <title>My title</title> </head> ... EOB; ?> I\'ve never seen this approach to print HTML, which seems to be pretty useful and less prone to some weird variable or double quote syntax error. I\'ve searched for some official information about this and only found a post of Rasmus talking about this. What is a detailed explanation about this functionality and what does EOB mean?

Calling PHP functions within HEREDOC strings

房东的猫 提交于 2019-11-26 02:31:35
问题 In PHP, the HEREDOC string declarations are really useful for outputting a block of html. You can have it parse in variables just by prefixing them with $, but for more complicated syntax (like $var[2][3]), you have to put your expression inside {} braces. In PHP 5, it is possible to actually make function calls within {} braces inside a HEREDOC string, but you have to go through a bit of work. The function name itself has to be stored in a variable, and you have to call it like it is a

Using variables inside a bash heredoc

☆樱花仙子☆ 提交于 2019-11-26 01:46:47
问题 I\'m trying to interpolate variables inside of a bash heredoc: var=$1 sudo tee \"/path/to/outfile\" > /dev/null << \"EOF\" Some text that contains my $var EOF This isn\'t working as I\'d expect ( $var is treated literally, not expanded). I need to use sudo tee because creating the file requires sudo. Doing something like: sudo cat > /path/to/outfile <<EOT my text... EOT Doesn\'t work, because >outfile opens the file in the current shell, which is not using sudo. 回答1: In answer to your first

How can I write a heredoc to a file in Bash script?

霸气de小男生 提交于 2019-11-26 01:27:58
问题 How can I write a here document to a file in Bash script? 回答1: Read the Advanced Bash-Scripting Guide Chapter 19. Here Documents. Here's an example which will write the contents to a file at /tmp/yourfilehere cat << EOF > /tmp/yourfilehere These contents will be written to the file. This line is indented. EOF Note that the final 'EOF' (The LimitString ) should not have any whitespace in front of the word, because it means that the LimitString will not be recognized. In a shell script, you may

heredoc for Windows batch?

北战南征 提交于 2019-11-26 00:59:45
问题 Is there a way of specifying multiline strings in batch in a way similar to heredoc in unix shells. Something similar to: cat <<EOF > out.txt bla bla .. EOF The idea is to create a customized file from a template file.. 回答1: Not as far as I know. The closest I know of is > out.txt ( @echo.bla @echo.bla ... ) ( @ prevents the command shell itself from printing the commands it's running, and echo. allows you to start a line with a space.) 回答2: Here's another approach. @echo off :: #############

How to cat <<EOF >> a file containing code?

你。 提交于 2019-11-26 00:37:07
问题 I want to print code into a file using cat <<EOF >> : cat <<EOF >> brightup.sh !/bin/bash curr=`cat /sys/class/backlight/intel_backlight/actual_brightness` if [ $curr -lt 4477 ]; then curr=$((curr+406)); echo $curr > /sys/class/backlight/intel_backlight/brightness; fi EOF but when I check the file output, I get this: !/bin/bash curr=1634 if [ -lt 4477 ]; then curr=406; echo > /sys/class/backlight/intel_backlight/brightness; fi I tried putting single quotes but the output also carries the