sed if statement to see if file exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed if statement to see if file exists
# 8  
Old 08-21-2013
Quote:
Originally Posted by rdrtx1
try something like:
Code:
if [ -n "$(ls | grep "[.]order$")" ]

A more efficient approach.
Code:
if grep -q '\.order$'; then
    echo match
else
    echo no match
fi

It's more efficient in several ways.

First, fewer subshells need to be forked.

Second, since grep's output isn't required, there's no need to pipe data from grep to a command substitution parent shell.

Third, suppose that there are very many matches in the ls output. We only need to find the first one. Once a single match is found, with -q, grep can exit immediately since the result is known to be true.

On an unrelated note, unless you intend to allow some sort of expansion between them, or unless you are trying to avoid a collision with another set of quotes, it's a good habit to use single quotes instead of double quotes. Also, while a trivial difference, it's also more efficient to parse single-quoted text.

Regards,
Alister

---------- Post updated at 03:43 PM ---------- Previous update was at 03:39 PM ----------

Quote:
Originally Posted by ahamed101
Something similar...
Code:
if [ -n "$(ls *.bin  2>/dev/null)" ];then

You can probably also redirect stdout to /dev/null and test the exit status directly.
Code:
if ls *.bin >/dev/null 2>&1; then

Regards,
Alister

---------- Post updated at 03:46 PM ---------- Previous update was at 03:43 PM ----------

Quote:
Originally Posted by neutronscott
Code:
#!/bin/bash
files=( *.order )

if (( "${#files[@]}" > 0 )); then
  ...
else
  ...
fi

With the usual shell defaults, the if-condition will always succeed, since if nothing matches the pattern the pattern remains unchanged and is assigned as the sole member of the array. For this code to work, nullglob must be enabled.

Regards,
Alister

---------- Post updated at 03:59 PM ---------- Previous update was at 03:46 PM ----------

In spirit, the following is akin to Scrutinizer's approach, but instead of using a function parameter to isolate the first of potentially many matches, it uses the for-loop's list assignment and an unconditional break.
Code:
for f in *.order; do
    break
done

if [ -e "$f" ]; then
  echo yes
fi

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed within awk statement

input | Jan 8 2018 11:28PM| 24 | 75 | 51 | 1 | 1.600| | Jan 8 2018 12:01PM| 52 | 823 | 21 | 6 | 2.675| desired output Jan-8-2018-11:28PM 24 75 51 1 1.600 Jan-8-2018-12:01PM 52 823 21 6 2.675 Dear friends, I have input file , as shown above and... (10 Replies)
Discussion started by: sagar_1986
10 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

sed to add word if pattern exists

Hi, I have a file with data listed below: clm_id double, cnl_id double, cnl_amt double I want output to be clm_id string, cnl_id string, cnl_amt double So replace double by string if the field contains the word _id I tried sed -e 's/_id/& string/' filename The above will not... (2 Replies)
Discussion started by: wahi80
2 Replies

4. Shell Programming and Scripting

If else statement in sed

Hello Guys, I am new here and this is my first post, hope someone can help me I am writing a script that is supposed to go in 9 different directories and edit a given file in each of the directories. I am using sed to edit the file as sed -i 'line# s/#to be changed/#to be replaced with/... (5 Replies)
Discussion started by: Madiouma Ndiaye
5 Replies

5. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. UNIX for Dummies Questions & Answers

Statement to find if an entry exists in a file

I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist. So I have so far... echo "Enter record:" read record //command || //command Can I use an if statement to do this? (3 Replies)
Discussion started by: itech4814
3 Replies

7. Shell Programming and Scripting

IF statement to check file exists

Hi All, If i run below copy command, it works absolutely fine, /opt/csw/bin/scp axetlxyz01:/opt/data/test/QURIES* ./input I want to make the above line better, by adding an IF statement, want to check if there is any file exists with name QURIES*.* then i need to copy that. if ... (7 Replies)
Discussion started by: rkrgarlapati
7 Replies

8. Shell Programming and Scripting

How to check if a file exists using the if statement

Hi, I'm trying to write a bit of code that will check if a file exists and then archives the file Im trying to use the following if statement without success.. if then mv filename archive/filename else echo "no filename exists" fi Should the file name be... (3 Replies)
Discussion started by: Jazmania
3 Replies

9. Shell Programming and Scripting

Variables within a sed statement

I am just wondering if it's possible to refer to variables within a sed statement as follows:- cat $file | sed -e 1's/$oldtext/$newtext/' > $file as when I run the script, the variables are not recognised and nothing happens..?? Thanks (5 Replies)
Discussion started by: sirtrancealot
5 Replies

10. Shell Programming and Scripting

if and sed statement

this is my output for my crawler. /about.html /ads/ /advanced_search?hl=en froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8 groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8 /imghp?hl=en&tab=wi&ie=UTF-8 /intl/en/options/ /language_tools?hl=en /maphp?hl=en&tab=wl&ie=UTF-8... (3 Replies)
Discussion started by: chris1234
3 Replies
Login or Register to Ask a Question