w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Is there a command that works for command line arguments like the sort command does for files?

You need to use command substitution $(...) to capture the output of a command like that.

#!/bin/bash
if [ $# -lt 1 ] || [ $# -gt 5 ]; then
    echo "Incorrect number of arguments!"
else
    sorted=$(for var in "$@"; do echo "$var"; done | sort -rn | tr -d '
')
    echo "SORTED: $sorted"
fi

$ ./test 1 2 3 4 5
SORTED: 5 4 3 2 1
$ ./test 5 4 3 2 1
SORTED: 5 4 3 2 1




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.