script does not write to file

Asked

Viewed 78 times

1

I’m putting together a bash script as follows:

#!/bin/bash

destfile=/home/user/teste.txt
array=($(ls 20151* |awk '{ print $9 }'))
n=${array[@]}

echo "$n" > "$destfile"

but when running the script the test.txt file is created, but empty. But if I run the commands one by one in the terminal, the file is created with the content, exactly the way I need it. What I’m doing wrong?

  • your problem certainly lies in this line: ($(ls 20151* |awk '{ print $9 }')) Is there any way you can post some examples of file names starting with 20151*? Post the name of the whole file, including the extension, if any.

  • this is not the issue as all filenames are basically in this format: "20151223152832_alexsandro_felix.txt"

  • Check it out, I don’t know how this can be working running manual like you said, because when doing awk '{ print $9 }' in the file name 20151223152832_alexsandro_felix.txt really can’t return anything. It would only work this way if, for example, your string had 9 blank spaces in the file name. Then the awk would print the 9th position of that "vector".

  • Post what you want to return which then becomes easier to help. What should be the contents of the test.txt file based on this filename pattern you have?

2 answers

2

I discovered my mistake, actually as I have in my user an alias for ls I ended up getting used to it, so really the error was in the line that @Cantoni quoted. To correct I did as follows:

#!/bin/bash

destfile=/home/user/file.txt;
array=($(ls -lh 20151* |awk '{ print $9 }'));
n=${#array[@]};

echo "$n" > "$destfile";
  • 1

    So at the end you just wanted the file name. :-)

  • in this part of the script that was giving the problem yes, but it is a little more extensive. (at the end, for what I needed I ended up using awk. I set up the array with a find filtering the files by date). Anyway, thank you all very much.

  • 1

    deep down what you want is ls -1 > ~/file.txt ?

2


By default, the awk split into a string using the white space character as a separator.

So if it’s done:

echo "20151223152832_alexsandro_felix.txt" | awk '{print $9}'

nothing will be returned, because there is no white space in the string and even if it had, it would take 8 spaces for the print at position 9 to return something.

Try changing the string now, including a blank space:

echo "20151223152832 _alexsandro_felix.txt" | awk '{print $1}'

Will be returned: 20151223152832

echo "20151223152832 _alexsandro_felix.txt" | awk '{print $2}'

Will be returned: _alexsandro_felix.txt

The command ls -lh returns a string in this format:

-rw-rw-r-- 1 cantoni cantoni 0 Jan 12 10:39 20151223152832_alexsandro_felix.txt

So by making a awk '{print $9}' the file name is printed. The danger of this approach is if the file name contains spaces. If this happens, then the awk '{print $9}' will not return the whole file name.

Explained this, a way to solve this problem without using the awk would be using the following command:

ls -A 20151*

In the context of the question problem would be this:

array=($(ls -A 20151*));

It is possible to change the separator by which the awk split into a string, see example below:

echo "20151223152832:_alexsandro_felix.txt" | awk -F':' '{print $2}'

Will be returned: _alexsandro_felix.txt

In this case, the separator was the character ':'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.