How can I check if there slash in the end of string in bash if?
Hello
I want to create a small bash script which copy file or directory to a new place. The script create the sub directories of the new place if they don't exist (mkdir -p). If there is something like that I will happy to here and to save time. How do I recognize if a given new place is a directory or file or directory with file? dirname give the directory but if its input is directory it bring only the parent directory path: Code:
$ dirname /a/b/s Nadav |
In Regular Expressions (a/k/a regexp) $ means end of line.
e.g. grep a$ myfile would find all lines that ended with "a". Since / is a special character you may need to escape it with \. e.g. grep \/$ myfile However you might want to use the "find" command: e.g. find mydir -type d Would find all subdirectories of the directory, mydir. Using this find syntax you would ONLY be working on directories and they'd all have the same format in output. P.S. Of course $ also has meaning to the shell as the start of variable names or numbers. You need to be sure you're aware of how it is being treated in the context you're using it in. e.g. grep $VAR$ myfile Would give you very odd results. Using ${VAR}$ on the other hand would clearly distinguish that you meant $VAR as the variable and were looking for whatever it was at the end of the line. |
Quote:
Code:
#!/bin/bash |
Quote:
|
Hi.
The real question appears to be: Quote:
If that is a fair assessment, then I would recommend testing the string to see if it does refer to a directory or not. Using test or [ with the predicate -d is of value here. Of course, you may wish to do more detailed testing. For example: Code:
#!/bin/sh Code:
% ./s1 |
Note that -d will not return true if a path is a symbolic link to a directory. If you want to test for this, you should recursively follow symlinks using readlink.
|
All times are GMT -5. The time now is 09:15 PM. |