M
M
maniac_by2018-10-15 14:20:41
linux
maniac_by, 2018-10-15 14:20:41

I can't get the script to work with IF, what could be the reason?

Hello. I got a task, not difficult, but never dealt with programming even close. That's why help is needed.
You need to write a script that will display the top 10 files by size from the selected directory.

#!/bin/bash
dir=$1
dir2=$2

if ; then find $2 -type f -ls 2> /dev/null | sort -rnk7 | head -10 | awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'
fi

if ; then echo "help me"
fi

if ; then echo "No args"
fi

The problem is that when I write, for example, /opt , the script doesn't output anything. I understand that the point is in the declaration of a variable. But I can't figure out what exactly is wrong. Please help with a simple example. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Ratkin, 2018-10-15
@maniac_by

#!/usr/bin/env bash

if [ "$#" -gt "1" ]
then
    echo "To many arguments"
    exit 1
fi

if [ -z "$1" ]
then
    echo "Введите директорию!"
    exit 1
fi

if 
then
    find "$1" -type f -ls 2> /dev/null | sort -rnk7 | head -10 | awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'
elif 
then
    echo "Help me!"
else
    echo "$1 не директория!"
fi

S
Saboteur, 2018-10-15
@saboteur_kiev

#!/bin/bash

if ; then
  echo "provide directory name";
  exit 1
fi

if ; then
  find "$dir" -type f -ls 2>/dev/null | sort -rnk7 | head -10 | awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'
else
  echo "it is not a directory"
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question