LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Scripting again (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-again-349122/)

yaadhav 08-02-2005 03:00 AM

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...

marghorp 08-02-2005 03:35 AM

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'

Vgui 08-02-2005 12:16 PM

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.

marghorp 08-02-2005 07:18 PM

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

sorno 08-02-2005 11:07 PM

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

hw-tph 08-03-2005 06:50 AM

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

freakyg 08-03-2005 10:28 AM

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..........

frankie_DJ 08-04-2005 08:27 AM

Lets try to kill a fly with a shotgun, ha?

alias cx='chmod u+x $@' works just fine.

Vgui 08-04-2005 10:21 AM

marghorp, did you even try the script before you (wrongly) assumed it didn't work? :rolleyes:
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.


All times are GMT -5. The time now is 07:53 AM.