开发者

Simple bash script using "set" command

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开发者_Go百科 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.


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

or simply

ls -l | awk '{print $5, $9}' | sort -n


Set manipulates shell variables. This allows you to adjust your current environment for specific situations, for example, to adjust current globbing rules.

Sometimes it is necessary to adjust the environment in a script, so that it will have an option set correctly later on. Since the script runs in a subshell, the options you adjust will have no effect outside of the script.

This link has a vast amount of info on the various commands and options available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜