LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-16-2011, 05:34 AM   #1
amirgol
Member
 
Registered: Apr 2011
Posts: 35

Rep: Reputation: 0
Unable to assign an array in rc.local


I have the following line in my rc.local file:
Code:
USB=( `/usr/bin/file /sys/block/sda | /bin/egrep -o 'usb.*' | /usr/bin/awk -F"/" '{print $1, $2, $3}'` )
It works just fine on any other shell script but in rc.local it stops the execution. I've no idea what happens as this is a headless server, but I know that the internal segment executes just fine: if I place the following line in my rc.local:
Code:
echo `/usr/bin/file /sys/block/sda | /bin/egrep -o 'usb.*' | /usr/bin/awk -F"/" '{print $1, $2, $3}'` >> /root/aaa
I get the expected output (usb1 1-1 1-1.1) in /root/aaa. SO it seems that the array assignment is he culprit here.

Any idea what's happening and how can I solve this? The server is running Debian Wheezy x64.
 
Old 08-16-2011, 06:31 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Well I found this with a bit of searching:
Quote:
POSIX and Bourne shells are not guaranteed to have arrays at all.
And then when tested I found the following:
Code:
$ cat test.sh
#!/bin/sh

x=($(/usr/bin/file /sys/block/sda | awk '/^[^0-9]/{c=0}/usb/{c=1}c' RS="/" ORS=" "))

echo "${x[1]}"
$ ./test.sh
./test.sh: 3: Syntax error: "(" unexpected
So I am guessing that this may be happening to you. However, this might help:
Code:
$ cat test.sh
#!/bin/sh

set - $(/usr/bin/file /sys/block/sda | awk '/^[^0-9]/{c=0}/usb/{c=1}c' RS="/" ORS=" ")

echo "$@"
$ ./test.sh
usb9 9-2 9-2:1.0
 
Old 08-16-2011, 07:20 AM   #3
amirgol
Member
 
Registered: Apr 2011
Posts: 35

Original Poster
Rep: Reputation: 0
Well, my test.sh has no errors ;-). But the same code in rc.local fails. I use the resulting array in a for loop, is there an alternative for it?
 
Old 08-16-2011, 09:15 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Quote:
Well, my test.sh has no errors ;-).
When using sh or bash?? It is an important difference as rc normally runs a POSIX shell, generally sh.
Quote:
I use the resulting array in a for loop, is there an alternative for it?
Did you look at my second example??
 
Old 08-16-2011, 10:54 AM   #5
amirgol
Member
 
Registered: Apr 2011
Posts: 35

Original Poster
Rep: Reputation: 0
Oh, my test file was using bash. Hmm.

Anyway, problem solved: I've learned that a for loop don't need an array, so now everything is working as it should've.

Thanks for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
assign value to array in function Abid Malik Programming 3 10-16-2010 02:44 PM
how to assign a u_short variable to a u_char array ? aryan1 Programming 1 07-29-2009 07:15 AM
How can I assign to a pointer array like char *args[]; ? haydari Programming 3 04-10-2007 12:48 AM
Cannot assign NULL to array? exvor Programming 7 10-31-2006 02:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration