Could you please help me with this task?

"Write a shell script which solve the task without the whereis command. The parameter ($1) is a file name. The script tells you which directories (maybe more than one) does the file exist in. (in the all-time PATH variable)"

So far, i wrote this, but it's totally wrong. My idea was, that the script has to go to the first direvroty, then decide wheter the file is in it or not. Then change to the next directory, do this again, than go back. And do this as many times as much directories i have in the first directory.
However, i'm not allowed to use functions in this task.

#!/bin/bash
cd
for i in $(echo ls | grep -v "..*\..*." | wc -l)
do
cd $(echo ls | grep -v "..*\..*." | cut -f $i")
if [ -e $1 ];
then
echo "exists"
else
echo "does not exist"
let i++
fi
cd
done

The key you are overlooking is the variable $PATH

#! /bin/bash

# argument from command line $1 has been confirmed before attempting to use it

# parse each individual absolute path in the PATH
for i in $(echo "$PATH" | tr ":" " ")  # that's "a space between"
do
    # find the file in the directory $i
    if [ -e "$i/$1" ]; then
        echo "$1 exists in $i"
    else
        echo "$1 does not exist in $i"
    fi
done

Personally I think find would work better.

#!bin/bash
find . -name $1 -exec echo "file exits in " {} \;

Personally I think find would work better.

#!bin/bash
find . -name $1 -exec echo "file exits in " {} \;

As currently written, it doesn't work better, since it doesn't even search the PATH, but rather the current directory and under. Nevertheless, I suspected that the exercise is not about efficiency, but how to practice and work with loops and conditional statements.

Sorry I missed the $path requirement. But this would meet that:

#!bin/bash
# test for filename provided on command line or exit
if [ -z "$1" ]; then 
     echo usage: $0 filename
     exit
fi
#run a for loop using the $PATH variable and replace the : with space
for x in `echo $PATH | sed s/:/\ /g `
do
     find $x -name $1 -exec echo "file exits in " {} \;
done
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.