LinuxQuestions.org
Help answer threads with 0 replies.
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 02-16-2012, 01:27 PM   #1
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Rep: Reputation: 15
Bash scripting help..permission denied on a specific line?


I tried to write a script to link every image file in my web images directory. I wanted to have the script use the full file path of the image, but I wanted the link itself to only reference the file name. I tried using the "basename" program but I can't get it to work in the script. Can you help me with a way to do this?

Code:
#!/bin/bash

#this is a script to create a simple index.html file with links to every 
#file in the current directory

mv index.html index.html.old
echo "moved old index.html to index.html.old..."
echo "building new index.html file..."

echo "<html>" >> index.html

for blah in `ls /web/dwinfo/htdocs/repo/*.jpg`
do
   echo `basename $blah`       # this works 
   # $blah =  `basename $blah` #this line doesn't work:Permission denied line 14
    $blah = `echo $blah | basename' #this dosen't work: Too few arguments line 15
    $blar = `basename $blah`        #this doesn't work: command not found line 16
    echo "<a href=\"$blah\">$blah</a><br>" >> index.html
done

echo "</html>" >> index.html
echo "done."
 
Old 02-16-2012, 01:47 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
$blah is interpreted as a command, do not put spaces in left and right of "=" char

And yes, you get permission denied when it tries to execute a non executable file

Last edited by Cedrik; 02-16-2012 at 01:55 PM.
 
Old 02-16-2012, 03:10 PM   #3
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Original Poster
Rep: Reputation: 15
So I guess the first word of every line has to be an executable, just like when you type normal commands. That makes sense.
 
Old 02-16-2012, 03:36 PM   #4
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 582

Rep: Reputation: 59
Daravon:


Just understand this simple concept to make your life easy:

In Shell Script we use "$" before a variable name when we want to extract its value or refer to the actual contents it's holding in the memory. We use the bare variable name when we want to assign a value to it. This value can be a literal or an output of some other command- you already know this part.

So, let's correct some of your statements above:

This line needs to be chaged

Code:
$blah =  `basename $blah`
to
Code:
blah=`basename $blah`
Note: There MUST NOT be any space before and after the assignment operator.

You do not need this line:

Code:
$blah = `echo $blah | basename'
even if you correct it to:


Code:
blah=`echo $blah | basename`
since the fist one is sufficient.

Note:

Your line:

Code:
$blah = `echo $blah | basename'

has an apostrophe / single quote (') at the end of the line instead of a back-tick (`). This is a common mistake. Avoid it.

Now change the other statements accordingly and you will (most likely) succeed this time.

Note: "Permission Denied" error (in your presented case) means the file is not having the execution bit set on it. Of course, you would not run/execute a JPEG file!
 
Old 02-16-2012, 03:56 PM   #5
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Original Poster
Rep: Reputation: 15
Thanks all. I learned that each line must begin with an executable, and the $vardollarsign only is needed when I want to "dereference" the variable. I don't use the $vardollarsign when I want to assign a value to a variable. And I can't put spaces around the '='.
 
Old 02-16-2012, 03:57 PM   #6
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 582

Rep: Reputation: 59
Yes, you got it. Do not forget that you would not leave any spaces in "assignment" operations as shown above.
 
Old 02-16-2012, 05:19 PM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
also
Quote:
$blah = `basename $blah`
cant work..You'd have to have a null container to accept the command.
 
Old 02-16-2012, 08:22 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,326

Rep: Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364Reputation: 2364
You may want to bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 02-16-2012, 09:45 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,459

Rep: Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852
Just thought I would point out that the following is incorrect:
Quote:
I learned that each line must begin with an executable
The following is an example of a line that does not use this rule:
Code:
x=5
 
1 members found this post helpful.
  


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
Python: rather than permission denied, can you get an OS specific password challenge? BrianK Programming 9 10-04-2011 01:33 PM
Permission Denied Error on command line? zbryant4210 Linux - Software 3 10-06-2009 08:08 AM
permission denied at command line noelc Linux - Software 1 09-27-2009 01:58 AM
need to delete a line if a field of that line matches using awf in bash scripting accesskarthi Linux - Newbie 8 06-29-2009 03:15 AM
bash: Permission Denied Panagiotis_IOA Slackware 5 11-07-2006 10:25 PM


All times are GMT -5. The time now is 02:33 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