- Efficient use of line break in shell script
- 2 Answers 2
- Echo newline in Bash prints literal \n
- 23 Answers 23
- How to Replace Line Breaks , New Lines From String in Java — Windows, Mac or Linux Example
- How to break a long line in .bash_profile
- 3 Answers 3
- How to break a long string into multiple lines assigned to a variable in linux bash script
- 4 Answers 4
Efficient use of line break in shell script
Context: I have a task to run a few script on a remote machine, and exit
I have a script, but I am confused how to use the line break inside it,
In step two, where I am running the JMeter script, it has a lot of properties. Is there a way I can use a line break here, so it will consider all the lines to be in one step. Like mentioned below
2 Answers 2
Inside here documents where the delimiter after (here EOF ) is not quoted, the sequence is removed, that’s a line continuation.
Actually, the only cases where is not removed is:
- inside single quotes
- inside here documents where the delimiter is quoted
- where the backslash itself is quoted ( )
So, here you can do:
And ssh will end up being fed:
Even if you used: ssh . so as to avoid parameter expansion, command substitution and arithmetic expansion being performed inside the here-document, ssh would be fed:
But the remote shell would interpret that as a line continuation, so it would have the same effect.
Note that when you do:
sshd on the remote host runs the user’s login shell to interpret that code. Since it could be anything, not necessarily a Bourne-like shell, it may be better to run:
Where sshd runs user-s-login-shell -c sh , so you know a Bourne-like shell is interpreting your code.
As an example that JVM_ARGS=»-Xms512m -Xmx25000m» ./jmeter.sh. is Bourne-shell or compatible syntax. It would work in csh , tcsh , rc , es , fish shells, so wouldn’t work with ssh user@server sh if the login shell of user on server was one of those shells.
A significant difference though is that in that case, user-s-login-shell is not started as a login shell so won’t read /etc/profile or
/.profile (or the equivalent for the user’s login shell) to set the login session up.
Alternatively, you could convert that code to a syntax compatible to all those shells: env JVM_ARGS=’-Xms512m -Xmx25000m’ ./jmeter.sh. (use single quotes instead of double quotes and use env to pass an env var instead of the Bourne/rc specific envvar=value cmd syntax).
The backslashes can be avoided by using xargs :
Or by using a shell like rc , ksh93 , zsh , bash , yash and an array:
With rc / zsh syntax:
(here quoting EOF so that $args is not expanded by the local shell).
Or with the ksh93 / bash / yash syntax (also works with zsh ):
Источник
Echo newline in Bash prints literal \n
In Bash, tried this:
But it doesn’t print a newline, only \n . How can I make it print the newline?
I’m using Ubuntu 11.04 (Natty Narwhal).
23 Answers 23
You could use printf instead:
printf has more consistent behavior than echo . The behavior of echo varies greatly between different versions.
Make sure you are in Bash. All these four ways work for me:
Words of the form $’string‘ are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
You could always do echo «» .
It worked for me in the nano editor.
From the man page:
-e enable interpretation of backslash escapes
In the off chance that someone finds themselves beating their head against the wall trying to figure out why a coworker’s script won’t print newlines, look out for this:
As in the above, the actual running of the method may itself be wrapped in an echo which supersedes any echos that may be in the method itself. Obviously I watered this down for brevity. It was not so easy to spot!
You can then inform your comrades that a better way to execute functions would be like so:
Источник
How to Replace Line Breaks , New Lines From String in Java — Windows, Mac or Linux Example
We often need to replace line terminator characters, also known as line breaks e.g. new line \n and carriage return \r with different characters. One of the common case is to replace all line breaks with empty space in order to concatenate multiple lines into one long line of String. In order to replace line breaks, you must know line terminator or new line characters for that platform. For example, In Windows operating system e.g. Windows 7 and 8, lines are terminated by \n\r also known as CR+LF , while in Unix environment e.g. Linux or Solaris line terminator is \n , known as line feed LF . You can also get line terminator pragmatically by using Java system property line.terminator.
Now the question is, How can you replace all line breaks from a string in Java in such a way that will work on both Windows and Linux (i.e. no OS specific problems of carriage return/line feed/new line etc.)?
Well, You can use System.getProperty(«line.terminator») to get the line break in any OS and by using String replace() method, you can replace all line breaks with any character e.g. white space. replace() method from java.lang.String class also supports regular expressions, which makes it even more powerful.
In next section we will learn how to replace all line breaks from Java String by following a simple code example. By the way, if you are removing line breaks from String then don’t forget to store result of replace() method into same variable.
Since String is immutable in Java, any change on it will always produce a new String, if you assume that call to replace() will change the original string then it’s not going to happen. For example, in below code original String text will remain unchanged, even after calling replace() method.
In order to see the result, you must store String returned by replace() method as shown below :
Now let’s see how to remove new line characters in Java.
Источник
How to break a long line in .bash_profile
I have a very long line in my .bash_profile, as below:
How do I break this line into multiple lines?
Also, is .bash_profile considered a shell script? If not, what exactly is it categorized as? If I know this, I will be able to look up general formatting guide for this file.
3 Answers 3
Or, with an array and string concatenation:
That last item in the array will expand to the value of the DYLD_FALLBACK_LIBRARY_PATH variable, or to nothing if it’s not set or is empty.
The expansion of «$
You can do this using the line break character: \ . Also you should declare and export on two different lines so:
Your bash profile is a config file and a script of sorts.
/.bash_profile a script, as it certainly executes commands.
Since it’s Bash, you can append to a string ( var+=value ). Though you pretty much need a temporary variable here, since you’re prefixing the paths to the original value.
Note that like your original snippet, this assumes that DYLD_FALLBACK_LIBRARY_PATH is not empty in the beginning. If it is, this’d leave a trailing : in it.
Alternatively, if your paths don’t contain whitespace just put the string in quotes, and remove the whitespace afterwards:
Источник
How to break a long string into multiple lines assigned to a variable in linux bash script
I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple lines and assigned to a variable?
4 Answers 4
Assigning long strings as multiple sub-string in an array could make the code more aesthetically appealing:
Which results in the expected:
Assuming you want to assign the string some long piece of text to assign to the variable str . This won’t work:
It’ll try to run the lines after the first as commands, you’ll probably get «command not found» errors.
You can do this, but the newlines will be embedded in the variable, so it won’t be a single line:
Though you can use the substring replacement expansion (in Bash, ksh, zsh) to replace them with spaces, e.g. str=»$
Another option is to use += to append to the value of the variable (also Bash, ksh, zsh only):
Here, any whitespace will need to be manually typed within the quotes.
Or, similarly in a standard shell:
Then there’s the way with line continuation (that Jeff already mentioned in their answer):
Here, too, trailing whitespace is important, the line continuation only works if the backslash is immediately followed by a newline, not if there are spaces in between.
Источник