How do I iterate over a range of numbers defined by variables in Bash?
edit: I prefer seq over the other methods because I can actually remember it 😉
edit: I prefer seq over the other methods because I can actually remember it 😉
To check if a directory exists in a shell script, you can use the following: Or to check if a directory doesn’t exist: However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check. E.g. running … Read more
When all else fails in Cygwin… Try running the dos2unix command on the file in question. It might help when you see error messages like this: -bash: ‘\r’: command not found Windows style newline characters can cause issues in Cygwin. The dos2unix command modifies newline characters so they are Unix / Cygwin compatible. CAUTION: the dos2unix command modifies files in place, … Read more
You can use it like this: Also works for multi-line array declaration
Working with xenserver, and I want to perform a command on each file that is in a directory, grepping some stuff out of the output of the command and appending it in a file. I’m clear on the command I want to use and how to grep out string(s) as needed. But what I’m not … Read more
Use find for that: find needs a starting point, and the . (dot) points to the current directory.
Try mkdir -p: Note that this will also create any intermediate directories that don’t exist; for instance, will create directories foo, foo/bar, and foo/bar/baz if they don’t exist. Some implementation like GNU mkdir include mkdir –parents as a more readable alias, but this is not specified in POSIX/Single Unix Specification and not available on many common platforms like macOS, various BSDs, and various commercial … Read more
When all else fails in Cygwin… Try running the dos2unix command on the file in question. It might help when you see error messages like this: -bash: ‘\r’: command not found Windows style newline characters can cause issues in Cygwin. The dos2unix command modifies newline characters so they are Unix / Cygwin compatible. CAUTION: the dos2unix command modifies files in place, … Read more
Heredoc sounds more convenient for this purpose. It is used to send multiple commands to a command interpreter program like ex or cat The string after << indicates where to stop. To send these lines to a file, use: You could also store these lines to a variable: This stores the lines to the variable named VAR. When printing, remember the … Read more
To remove the line and print the output to standard out: To directly modify the file – does not work with BSD sed: Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed: To directly modify the file (and create a backup) – works with BSD and GNU … Read more