0
#!/bin/bash

if(($#!=1))
then
    echo "Please enter a file name: "
    read f1
else
    f1=$1
fi

while [ ! -f $f1 ] 
do
    echo "Please enter an existing file name: "
    read f1
done

for file in *
do
    if [ $file == $f1 ]
    then
        pwd
    fi
    echo "Check here 1"
done

for file in */*
do
    if [ $file == $f1 ]
    then    
        pwd
    fi  
    echo "Check here 2"
done

This is my script, and I have the file f3.txt in the directory having this script and a subdirectory in it.

And I want to check where this file is located. when entering the main directory, it does enter the if successfully when they find the file, but when entering the subdirectory, it won't enter the if even though f3.txt is in the subdirectory.

To clarify, the first for loop works perfectly fine, when entering the second for loop, it does echo check here 2 perfectly according to how many files there is in the subdirectory, but doesn't enter the if despite having a file f3.txt in this subdirectory

  • Put your code in https://www.shellcheck.net/ for a couple syntax fixes. – Nic3500 Mar 24 '22 at 06:08
  • `for file in *` never descends into any of the subdirectories. Use `find . -type f -name "$fi"` – David C. Rankin Mar 24 '22 at 06:38
  • @DavidC.Rankin but doesn't for file in */* descends to the subdirectories? – Anthony Ghosn Mar 24 '22 at 08:14
  • You can use backticks around the parts you want to have formatted verbatim; but probably instead [edit] your question to clarify. If you have `shopt -s globstar` enabled, `**/*` will descend into subdirectories; but the code you posted is obviously not doing that. – tripleee Mar 24 '22 at 08:26
  • As a further observation, you are basically reinventing (a small subset of) `find` so probably just read its manual page instead. – tripleee Mar 24 '22 at 08:28
  • @tripleee just edited to clarify more what's happening to me – Anthony Ghosn Mar 24 '22 at 08:45
  • @AnthonyGhosn by adding `-type f` find will only return filenames, not directories. The default output of find is the filename of each file with complete path beginning at the starting directory you provided, `'.'` in this case. – David C. Rankin Mar 24 '22 at 18:51

1 Answers1

1

The immediate problem is that the wildcard expands to the directory name and the file name, and so of course it will not be equal to just the input file name.

if [ "$(basename "$file")" = "$f1" ]

strips the directory name before comparing (and also fixes quoting and syntax; the string equality comparison operator in sh is = and although Bash also allows == as a synonym, I can see no reason to prefer that)

... though properly speaking, you probably want

find . -name "$f1" -maxdepth 2 -ls

or maybe take out -maxdepth 2 if you want to traverse subdirectories of subdirectories etc.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • alright worked with ` if [ "$(basename "$file")" = "$f1" ] ` ,but pwd displays the location of the script and not of the file, do you know how can I fix that? – Anthony Ghosn Mar 24 '22 at 09:01
  • `pwd` displays the current working directory, if you want to display the directory part of `"$file"` that's `dirname "$f1"` ... but maybe better just print the entire file name, like `find` does. – tripleee Mar 24 '22 at 09:37