M.S M.S - 1 year ago 59
Linux Question

if-elif inside script not working

Create a script named dirstat.What i should do is to find if is a
file,directory or something else.For example to appear this messages :
file1 is file .Desktop is directory. Public is something else.dirstat
is file.I dont know what i did is wrong or right.What should i write more and where ?

$vi dirstat
for i in *; do
if [ -f file1]
echo -n "file1 is file"
elif [ -d desktop)
echo -n "desktop is directory"
elif [ -p public ]
echo -n "Public is something else"

Answer Source

Not tested, but similar idea to what you had:

file_type() {
    if [ -f "$1" ]; then
        echo "file"
    elif [ -d "$1" ]; then
        echo "directory"
        echo "something else"

for i in *; do
    printf "%s is %s\n" "$i" "$(file_type "$i")"

This will display all non-dot files in your current directory. Alternatively, use for i (without the in *) to display all the files listed on the command-line arguments.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download