Luca Borrione Luca Borrione Plus 1 for the fast solution and a more generalizable one — Wassadamo. Christian Christian 2 2 silver badges 4 4 bronze badges. If I understood your question correctly, this should do what you need. You don't need to test the output of grep, you can just use grep -q and call grep directly from if as Thomas does in his answer.
In addition, the question didn't include checking whether the directory exists before adding it to the list it could be a list of deleted directories, after all. I removed the example script, it didn't add anything to the answer given by Thomas. David Okwii David Okwii 6, 2 2 gold badges 31 31 silver badges 28 28 bronze badges. B8ightY B8ightY 3 3 silver badges 4 4 bronze badges.
GTodorov GTodorov 1, 20 20 silver badges 20 20 bronze badges. I would recommend adding -q so that you don't get the found strings in the stdout — Edenshaw. If you just want to check the existence of one line, you do not need to create a file.
Rudy Rudy 6, 10 10 gold badges 45 45 silver badges 80 80 bronze badges. I don't see -c option in fgrep --help — Nam G VU. AndrewD AndrewD 4, 2 2 gold badges 27 27 silver badges 30 30 bronze badges. Uses too much memory in case the file is large.
Shinoy Shaji Shinoy Shaji 11 11 silver badges 25 25 bronze badges. ThomWiggers I tried the same and it worked for me. Triangle Triangle 1, 3 3 gold badges 21 21 silver badges 34 34 bronze badges. The Overflow Blog. Typically, testing for a file returns 0 true if the file exists, and 1 false if the file does not exist. For some operations, you may want to reverse the logic.
The exclamation point! This command makes sure there is not a file named test. You should see test. You can use a similar command for a directory — replace the —f option with —d :.
As usual, changing the —f option to —d lets you run the same test for multiple directories. There are many other options available. Please consult the main page test ——help for additional options. The previous commands work well for a simple two-line command at a command prompt. You can also use bash with multiple commands. When several commands are strung together, they are called a script. The first one will check whether a file exists regardless of the type, while the second one will return true only if the FILE is a regular file not a directory or a device.
The most readable option when checking whether a file exists or not is to use the test command in combination with the if statement. Always use double quotes to avoid issues when dealing with files containing whitespace in their names.
You can also use the test command without the if statement. We can use both double or single brackets for such one liner but as I said earlier I would recommend using double brackets. I hope you know that in Linux everything is a file. So in that way, a directory is also considered to be a file. We can use " -d " attribute to check if a directory exists in shell programming.
We can use -d attribute within single [.. Similarly we use single brackets in this example to check if the directory is preset within shell script. In this example we will use test command to make sure if directory is present or not before we perform certain task. In this example we will use single and double brackets in single line form to check for the directory. I would recommend using [[.. There are no shell attributes as I have used in all other examples and scenarios in this tutorial.
0コメント