Home > other >  file -i command don't identify some of the files that been passed to a function by the ls comma
file -i command don't identify some of the files that been passed to a function by the ls comma

Time:12-09

I'm trying to use a function to print the type of files and if the file is a directory it will call the function recursively with the list of files from the directory.

however when sending via ls command the list of the files inside the directory $(file -i filename) gives me "cannot open `a1' (no such file or directory)" even though the files are there and he got them from ls.

my testing directory is:

a1 - a directory file, gives (no such file or directory)
bunzip2test.txt - a text file, gives (no such file or directory for some reason)
dir1.tar.gz - a compressed file, gives the correct info
t.txt.gz - a compressed file, gives the correct info
zipping.zip - a zip, gives the correct info

my code is:

#! /bin/bash

function main()
{
    f=0
    printing $@
}

function printing()
{
    
    cFile=($@)
    echo "printing all files:"
    for i in ${cFile[@]}
    do
        echo $i "and it's type is: $(file -i $i)"
    done
    if ((f == 0))
    then
        f=1

        printing $(cd testing && ls)
    fi
}

main $*

output:

testing and it's type is: testing: inode/directory; charset=binary
printing all files:
a1 and it's type is: a1: cannot open `a1' (No such file or directory)
bunzip2test.txt and it's type is: bunzip2test.txt: cannot open `bunzip2test.txt' (No such file or directory)
dir1.tar.gz and it's type is: dir1.tar.gz: application/gzip; charset=binary
t.txt.gz and it's type is: t.txt.gz: application/gzip; charset=binary
zipping.zip and it's type is: zipping.zip: application/zip; charset=binary

running cd testing && file -i $(ls) from the terminal it does work as predicted and identify all of the files correctly

CodePudding user response:

every time you call printing(), you call/execute cd testing... that is why only the first time the command works, and then it does not.. pull the cd testing before/outside the function OR add the folder name within $(file -i $i) as $(file -i testing/$i)

CodePudding user response:

solved it now by calling cd directoryname before calling the function again

here is the code:

#! /bin/bash

function main()
{
    f=0
    printing $@
}

function printing()
{
    
    cFile=($@)
    echo "printing all files:"
    for i in ${cFile[@]}
    do
        echo $i "type is: $(file -i $i)"
    done
    if ((f == 0))
    then
        f=1
        cd testing
        printing $(ls)
    fi
}

main $*
  • Related