LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-07-2023, 03:22 PM   #1
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Rep: Reputation: Disabled
BASH Issue


All,

Have a file full of scanned directories so trying to read it into an array but always get an error saying

This is a directory not a file. Thought the code for reading lines from a file was generic but guess not! So what code is needed to read each line in the file generically or as directories?

TBNK
 
Old 07-07-2023, 03:29 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Without an actual example of the file contents, the script code used to read it into an array and the exact error being produced, the question is really without meaning.

Please provide enough actual detail to allow us to understand the question and try to provide a useful answer without the need to guess about every piece of the puzzle.
 
Old 07-08-2023, 01:16 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
read and readarray process a text file.
A directory is not a text file, not even a binary file (though there are some similarities).
In bash use a for loop:
Code:
for fn in * .*
do
  [ -e "fn" ] || continue
  echo "do sth with $fn"
done
The .* lists the hidden files. (Or use "shopt -s dotglob" to include hidden files in the * )
You can prefix a dirpath: $dir/* $dir/.*
And you can filter: $dir/*.txt $dir/*.php (or $dir/*.{txt,php} )

Last edited by MadeInGermany; 07-08-2023 at 01:19 AM.
 
Old 07-08-2023, 03:40 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,044

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
also you can use:
Code:
[[ -d $fn ]] to check if $fn is a dir
[[ -f $fn ]] to check if $fn is a file
but as it was already mentioned without details hard to say more
 
Old 07-08-2023, 01:48 PM   #5
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
All,

OK! Well the file am reading is one created by KFind and dumped into a .txt file so nothing but directories in this file. So no need to even think any kind of ls cmd. My code reading this is:

Quote:
while IFS=read -r line; do
if [[ -n ”$line" && "$line" == "'/3T'" ]]; then
$oray+="$line"
fi
done < $srcfil
I'm thinking the IFS means the line must qualify as a file, which a directory will never do. Am I right?

TBNK
 
Old 07-08-2023, 04:13 PM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
so nothing but directories in this file.
What does that mean? You have directories in a file. You have file paths and directory paths listed in a file.txt?

Code:
txt="
/usr/lib/libargon2.so.1
/usr/lib/libasm-0.189.so
/usr/lib/libassuan.so.0.8.5
/usr/lib/vdpau
/usr/lib/udev
"

while read line; do 
    if [ -d "$line" ]; then
        echo ""$line" is a directory path."
    fi
    if [ -f "$line" ]; then
        echo ""$line" is a regular file path"
    fi
done <<< "$txt"

/usr/lib/libargon2.so.1 is a regular file path
/usr/lib/libasm-0.189.so is a regular file path
/usr/lib/libassuan.so.0.8.5 is a regular file path
/usr/lib/vdpau is a directory path.
/usr/lib/udev is a directory path.
 
Old 07-09-2023, 03:16 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,044

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by TBotNik View Post


I'm thinking the IFS means the line must qualify as a file, which a directory will never do. Am I right?

TBNK
No. you need to read the man page of bash and check IFS, do not assume anything [else].
https://www.baeldung.com/linux/ifs-shell-variable
 
Old 07-10-2023, 01:40 PM   #8
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
All,

So banging through this slowly. Put in the code
Code:
readarray pray < $srcfil
Which totally works! The directories I want to process are all on the 3Tb drive, so added this code:
Code:
nray()
for i in ${pray[@]}; do
if [ "$i" == "/3T/"* ]; then
$nray+=”$i"
fi
But this array stays empty so my logic is somehow wrong.

Is my error here obvious?

TBNK
 
Old 07-10-2023, 03:33 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Fixes:
Code:
nray=( )
for i in "${pray[@]}"; do
  if [[ "$i" == "/3T/"* ]]; then
    nray+=( "$i" )
  fi
done

Last edited by MadeInGermany; 07-11-2023 at 06:23 AM. Reason: Another fix
 
Old 07-11-2023, 12:09 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,044

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
would be nice to use shellcheck, that can find those kind of issues easily
 
Old 07-11-2023, 05:24 PM   #11
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
Fixes:
Code:
nray=( )
for i in "${pray[@]}"; do
  if [[ "$i" == "/3T/"* ]]; then
    nray+=( "$i" )
  fi
done
MadeInGermany,

Thanks! The right syntax always works!

Now working on the next MySQL section to put the array into the DB.

Have no book on that so trying to find online examples. The DB has to be checked for each entry to make sure to eliminate any duplicates.

Thanks again!

TBNK
 
Old 07-15-2023, 01:13 PM   #12
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
MadeInGermany,

Got a novel thought so ran a KFind on all .sh files looking for the word "mysql" and found serveral examples.

Now working on editing this, but per my PHP Config post not able to see results in PPHPmyAdmin, but have MySQL Workbench installed so will have to find results there.

Will let you know where I get with this, hopefully over the weekend.

TBNK
 
Old 07-15-2023, 02:57 PM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
You can always use the mysql command line client to insert values.
mysql_query="insert into table x (column1...) values(data1...);"
mysql -u username -e "$mysql_query"

If the table is created with an unique index on the value you will not have any duplicate rows. Instead of an array you can create a CSV file and import that into mysql.
 
Old 07-16-2023, 04:36 PM   #14
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
You can always use the mysql command line client to insert values.
mysql_query="insert into table x (column1...) values(data1...);"
mysql -u username -e "$mysql_query"

If the table is created with an unique index on the value you will not have any duplicate rows. Instead of an array you can create a CSV file and import that into mysql.
michaelk,

The insert is not the problem but getting a return or error from the select query looking for duplicates is the issue!

TBNK
 
Old 07-16-2023, 06:40 PM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Without knowing anything about the structure of the table here is one way to query for duplicate rows. Replace column with an actual column name.
Code:
select column,count(column) as duprows from table group by column having count(column)>1;

Last edited by michaelk; 07-16-2023 at 06:42 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind issue? DNS issue? Domain provider issue? Cannot reach Apache virtual hosts internally. rnturn Linux - Networking 5 01-26-2020 10:35 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
bash my little bash alaios Linux - Newbie 4 01-10-2005 11:59 PM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration