![]() ![]() While do # resolve $SOURCE until the file is no longer a symlinkĭIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 & pwd )" If you also want to resolve any links to the script itself, you need a multi-line solution: #!/usr/bin/env bash It will work as long as the last component of the path used to find the script is not a symlink (directory links are OK). Is a useful one-liner which will give you the full directory name of the script no matter where it is being called from. If the variables contain spaces or other unusual characters it will probably cause the script to fail. The reason for this is explained by 8jean in another answer. Take particular note of the double-quotes used to wrap the variables. # Symbolic link specific commands go here. ![]() So symbolic links may have to be treated differently, if subsequent commands expect directories: if then Will produce the error message: rmdir: failed to remove `symlink': Not a directory ![]() running this: ln -s "$ACTUAL_DIR" "$SYMLINK" 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.Į.g. # Control will enter here if $DIRECTORY doesn't exist. Or to check if a directory doesn't exist: if then # Control will enter here if $DIRECTORY exists. To check if a directory exists in a shell script, you can use the following: if then can go at the beginning or the end, so if you need both ] and -, you need to start with ] and end with -, leading to the regex "I know what I'm doing" emoticon: ) (That's a Posix regex rule: if you want to include ] in a character class, it needs to go at the beginning. (Aren't there always?) One is that you could not put ] into $valid, even if $valid were quoted, except at the very beginning. The combination of parameter expansion and regex operators can make bash regular expression syntax "almost readable", but there are still some gotchas. ! negates the test, turning it into a "does not match" operator, and a regex character class means "any character other than. In other words, an expression like this: valid='0-9a-zA-Z $% ' # add almost whatever else you want to allow to the list The easiest way to do this check is to make sure that it does not contain an invalid character. But beware: In this case, you cannot quote the variable expansion: # This doesn't work:įinally, I think what you are trying to do is to verify that the variable only contains valid characters. Of course, you can put the pattern into a variable: pat=""įor regexes which contain lots of characters which would need to be escaped or quoted to pass through bash's lexer, many people prefer this style. Other characters similarly need to be escaped, like #, which would start a comment if not quoted. If you wanted to match letters, digits or spaces you could use: ]]. So spaces in the regex need to be escaped or quoted. Similarly, the expression between the ] is split into words before the regex is interpreted. If you quote the right-hand side like-so " ]], then the right-hand side will be treated as an ordinary string, not a regex (and $0 will still be expanded). So if you write: ]], the $0 inside the regex on the right will be expanded before the regex is interpreted, which will probably cause the regex to fail to compile (unless the expansion of $0 ends with a digit or punctuation symbol whose ascii value is less than a digit). In other words, it's perfectly safe to leave variable expansions unquoted on the left-hand side, but you need to know that variable expansions will happen on the right-hand side. Any part of the pattern may be quoted to force it to be matched as a string.Ĭonsequently, $v on either side of the =~ will be expanded to the value of that variable, but the result will not be word-split or pathname-expanded. the string to the right of the operator is considered an extended regular expression and matched accordingly. Word splitting and pathname expansion are not performed on the words between the ] tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed.Īn additional binary operator, ‘=~’, is available. There are a couple of important things to know about bash's ] construction.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |