1 Replies - 532 Views - Last Post: 11 May 2017 - 05:05 PM Rate Topic: -----

#1 tmdwuming  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-May 17

[UNIX and Linux]Shell Script help

Posted 11 May 2017 - 03:09 PM

Write a bash script to sort the lines in a text file by the number of characters on each line.
after write and quit I run my .sh file then enter some random string, then I press ctrl+d result is nothing showed up

so somebody pleases help me :bigsmile:/>
#!/bin/bash

declare -a input
    while read str
    do
	if [ -z "${input[${#str}]}" ]
    then   
	input[${#str}]="$str"
    else
	input[${#str}]="${input[${#str}]}\n$str"
    fi
    done
#print
    for line in "${!input[@]}"
	do
    	echo "${input[$line]}"
    done



Is This A Good Question/Topic? 0
  • +

Replies To: [UNIX and Linux]Shell Script help

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6547
  • View blog
  • Posts: 30,673
  • Joined: 10-May 07

Re: [UNIX and Linux]Shell Script help

Posted 11 May 2017 - 05:05 PM

What is with the exclamation point on line 14?

I would set it like this :
for line in $(echo ${input})


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1