LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-02-2005, 03:00 AM   #1
yaadhav
LQ Newbie
 
Registered: Aug 2005
Location: I live in a world full of anger and hate
Posts: 3

Rep: Reputation: 0
Shell Scripting again


Script for chmod (Use exec)

Typing “cmod +x file” every time you want to add execute permission to a shell script is a bit of a pain. Write a shell script called cx, which will run chmod for you. Make sure your cx script will change the modes of all files if used as: cx fiel1 file2 file3 file4

Can someone also help me answer the above...
 
Old 08-02-2005, 03:35 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
for one file it's simple.

#!/bin/sh

chmod +x $1

Save the file as cx

And this would be run like cx filename

But the even easier way is to create an alias. I don't know if chmod can take multiple files, never tried it, but I guess it can. No way of verifying now as I am nowhere near a linux box.

You could create an alias like this:

alias cx='chmod +x'

Now everytime you run cx actually the chmod 'x gets run. But be carefull, check that cx doesn't already exist as you might do some weird stuff. If cx would be a name of a system file used frequently, then making an alias with the same name would cause problems, because instead of the cx from the system, chmod +x would be called. And you don't want some program randomly changing file permissions to x, now do you?

Anyway alias is the way to go if you ask me.

Just put a line like this in your .bashrc file:

alias cx='chmod +x'
 
Old 08-02-2005, 12:16 PM   #3
Vgui
Member
 
Registered: Apr 2005
Location: Canada
Distribution: Slackware
Posts: 496

Rep: Reputation: 31
One minor note about the post above. Instead of using $1, use $@
Using $@ means that _all_ files you pass to the script are used, as compared to just the first one.
And I would suggest an alias, basically:
Code:
alias cx='chmod +x $@'
Then you can go:
Code:
cx one two three
And it will change all three files to executable.
 
Old 08-02-2005, 07:18 PM   #4
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Don't want to be rude ut just a correction of the above script. I think the alias cx='chmod +x $@" wouldn't work, because cx has no arguments to go with.

this way cx would be substituted by chmod +x $@ and $@ is not yet set in cx itself.

Well if you need a bash script you could do this:

for i in $@
do
chmod +x $i
done

Last edited by marghorp; 08-02-2005 at 07:19 PM.
 
Old 08-02-2005, 11:07 PM   #5
sorno
LQ Newbie
 
Registered: Aug 2004
Posts: 21

Rep: Reputation: 15
how about this
#!/bin/sh
chmod +x "$@"



this will allow:cx 'a p p l e' apple 'a pp l e'
and add exe permission to three above files
 
Old 08-03-2005, 06:50 AM   #6
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Quote from the LQ rules:
Quote:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
Håkan
 
Old 08-03-2005, 10:28 AM   #7
freakyg
Member
 
Registered: Apr 2005
Distribution: LFS 5.0 and 6.1
Posts: 705

Rep: Reputation: 30
instead of making a $Script...........one can add this to a right click context menu...........this is from a book
Quote:
LINUX DESKTOP HACKS
100 Industrial-Strength Tips & Tools by...
Nicholas Petreley & Jono Bacon
Quote:
All you have to do to create the context menu that will make a file executable
is create a text file formatted very much like an application link, and
then drop it in a special directory. We’ll start by creating a file called make_
executable.desktop. Start up your favorite editor, and enter the following
text:
[Desktop Entry]
Encoding=UTF-8
ServiceTypes=application/x-shellscript
Actions=MakeExe
[Desktop Action MakeExe]
Name=Make file executable
Exec=chmod +x %f
Icon=kfm
Save your work. The action this file takes is defined by the entry Exec=chmod
+x %f. KDE substitutes the name of the selected file for %f. If you want to
make this feature available to everyone who uses KDE on this computer,
place the file here:
# cp make_executable.desktop <path to kde>/share/apps/konqueror/servicemenus

Create Your Own KDE Right-Click Menu Actions
Depending on your Linux distribution, the path might not be tied to your
KDE directory. It might be /usr/share/apps/konqueror/servicemenus.
If you simply want to make this feature available to yourself, place the file
here (assuming your KDE settings are kept in ~/.kde; your distribution might
use ~/.kde3.3 or something similar):
$ cp make_executable.desktop ~/.kde/share/apps/konqueror/servicemenus
The ServiceTypes field in the file is set to the mime-type
application/x-shellscript, which means the menu option will
be available only when you right-click a shell script file.

Symbols Available for Menu Actions
Konqueror passes the names of selected URLs, selected files, and other
selected elements to your custom menu actions through the use of symbols.
Symbols exist for single files, multiple files, single URLs, and more. The
right-click action that allows you to change a file to be executable works,
because it substitutes the name of the selected file for the symbol %f. Here
are some other symbols available to you, and how they work.

%f
A single filename, even if you select multiple files. In general, you use
this only when you intend to select a single file. In some cases, you can
use this to select several files for use by a program that knows how to
launch a separate instance for each filename.
%F
Multiple selected files. Use this for programs that can act upon several
files at once.
%u
A single URL.
%U
Multiple URLs.
%d
The single directory that contains the selected file represented by %f.
%D
A list of the same directory containing an entry for every selected file in
that directory (%F).
%n
A filename without a path.
%N
Multiple filenames without paths.
%k
A file represented as a URI.
easy peasy...........now have at it..........
 
Old 08-04-2005, 08:27 AM   #8
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 31
Lets try to kill a fly with a shotgun, ha?

alias cx='chmod u+x $@' works just fine.
 
Old 08-04-2005, 10:21 AM   #9
Vgui
Member
 
Registered: Apr 2005
Location: Canada
Distribution: Slackware
Posts: 496

Rep: Reputation: 31
marghorp, did you even try the script before you (wrongly) assumed it didn't work?
As I said (and frankie_DJ), using:
Code:
alias cx='chmod +x $@'
Works perfectly fine, I tested it before posting...so that solves that problem.
 
  


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
shell scripting pcdebb Linux - Newbie 17 11-20-2003 03:49 PM
shell scripting markus1982 Linux - General 2 05-20-2003 07:42 AM
Help with Shell Scripting. eggoz Linux - General 2 04-21-2003 01:15 PM


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