Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple bash script using "set" command

Tags:

bash

I am supposed to make a script that prints all sizes and file-names in the current directory, ordered by size, using the "set" command.

#!/bin/bash

touch /tmp/unsorted

IFS='@'
export IFS

ls -l | tr -s " " "@" | sed '1d' > /tmp/tempLS

while read line
do
    ##set probably goes here##
    echo $5 $9 >> /tmp/unsorted
done < /tmp/tempLS

sort -n /tmp/unsorted
rm -rf /tmp/unsorted

By logic, this is the script that should work, but it produces only blank lines. After discussion with my classmates, we think that the "set" command must go first in the while loop. The problem is that we cant understand what the "set" command does, and how to use it. Please help. Thank you.

like image 524
alf Avatar asked Dec 28 '22 14:12

alf


1 Answers

ls -l | while read line; do
  set - $line
  echo $5 $9
done | sort -n

or simply

ls -l | awk '{print $5, $9}' | sort -n
like image 87
Karoly Horvath Avatar answered Jan 16 '23 23:01

Karoly Horvath