LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2009, 01:00 PM   #1
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Rep: Reputation: 33
Bash scripting with directory checking if exists


Hi!!

I have this code in bash

Quote:
array_log = ([0]="/home" [1]="/usr" [2]="/usr/bin")

for item in ${array_log[*]}
do
if [ -d "${array_log[*]}" ]; then
echo "exists"
fi

done
Somewhere i have an error .. Can you help me ?

i get this one when i m going to run it:
Quote:
./test.sh
./test.sh: line 1: syntax error near unexpected token `('
./test.sh: line 1: `array_log = ([0]="/home")'
Thanks!
 
Old 11-26-2009, 01:14 PM   #2
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Rep: Reputation: 17
Well, is array really needed?

Code:
LOG_DIRS="/home /usr /usr/bin"

for dir in $LOG_DIRS; do
        [ -d $dir ] && echo "$dir dir exists" 
done
 
Old 11-26-2009, 01:24 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Try removing the spaces either side of the first "=" in array_log = ([0]="/home" [1]="/usr" [2]="/usr/bin")
 
Old 11-26-2009, 04:48 PM   #4
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Rep: Reputation: 16
Quote:
Originally Posted by catkin View Post
Try removing the spaces either side of the first "=" in array_log = ([0]="/home" [1]="/usr" [2]="/usr/bin")
+1
 
Old 12-01-2009, 04:10 PM   #5
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
Quote:
echo "Do you want to continue deleting these files (y/n)?"
read answ

if [$answ = "y"]; then
echo ""
echo "Deletitng Files"
for file in $LOG_FILES; do
[ -f $file ] && rm $file && echo "${txtgrn}File Deleted${txtrst}"
done
fi
Why this returns me

line 45: [y: command not found

error?

Something happens in the if clause i think ..:s

Last edited by netpumber; 12-01-2009 at 04:12 PM.
 
Old 12-01-2009, 04:17 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
'[' is actually a command and has all the spacing requirements of a command. '[[' is highly preferred if this is supposed to be Bash, not POSIX sh.
 
Old 12-01-2009, 07:35 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Extending on, [[ ]] also requires spaces around the symbols.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting -- parsing a directory path int80 Programming 14 05-27-2017 11:33 PM
Bash scripting: "line [...]: [directory]: is a directory " ? serious Linux - Newbie 1 10-20-2007 12:08 PM
Bash scripting and trying to determine whether a directory exists? obelxi Programming 9 04-18-2005 11:22 PM
checking if a file exists veilig Programming 6 02-01-2004 04:16 AM
Checking if a file exists in C? tristan_vdv Programming 3 05-24-2002 10:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:35 PM.

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