-3

when i execute this script that i had made

#!/bin/bash
for object in $(ls -l)
do
echo $object
done

it's displayed as below
ls -l script KO

when i execute my script i want to have a result like this
ls -l script OK

i was trying a lot of things but its not working please i need your help thank you in advance

Armali
  • 18,255
  • 14
  • 57
  • 171
rim
  • 1
  • 3
  • 3
    [Why *not* parse `ls`?](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – Cyrus Jan 04 '18 at 11:04
  • [How do I get the find command to print out the file size with the file name?](https://stackoverflow.com/q/64649/608639), [Sort the output of find -exec ls](https://unix.stackexchange.com/q/140561/56041), [How to use the exec option in find with examples](https://linuxaria.com/howto/linux-shell-how-to-use-the-exec-option-in-find-with-examples), etc. – jww Jan 04 '18 at 14:30

1 Answers1

0

You want to read the output of ls -l line by line; to do this, replace the line

for object in $(ls -l)

by

ls -l|while read object

and change

echo $object

to

echo "$object"

(to preserve spaces).


If you insist on using for, you can do:

readarray -t < <(ls -l)
for object in "${MAPFILE[@]}"; do echo "$object"; done
Armali
  • 18,255
  • 14
  • 57
  • 171
  • do you have any suggestions please? – rim Jan 04 '18 at 13:38
  • @rim the thing is that you don't want to parse `ls` at all. See [this](https://stackoverflow.com/questions/48093846/use-ls-l-in-script-shell-linux-and-separate-between-results/48093940#comment83162204_48093846). – PesaThe Jan 04 '18 at 14:18
  • @PesaThe i want that ls -a list all the objects (file,folder..) of my current directory and seperate between file.... like in the picture. – rim Jan 04 '18 at 14:40
  • what @Armali write is correct and working as i want , but with "while boucle ", and me i want " For boucle".thank you for your suggestions – rim Jan 04 '18 at 14:41
  • @rim - For me to be able to suggest anything further, you'd have to clarify: Why do you want `for` instead of `while`? Is there anything you can't accomplish with `while`? What is `boucle`? If you need anything else than the result shown in your question, you should edit the question. – Armali Jan 09 '18 at 10:57