Quiero escribir el análogo de Ubuntu de un "archivo por lotes" (un script de shell). Pero no sé cómo usar el comando chmod +x nombre de archivo
para que el script se pueda ejecutar. Tampoco sé dónde utilizarlo.
chmod +x
en un archivo (su secuencia de comandos) sólo significa, que usted & #39; va a hacer que sea ejecutable. Haga clic derecho en el script y eligió Propiedades -> Permisos -> Permitir la ejecución de archivos como programa, te deja con el mismo resultado exacto como el comando en la terminal.
Si un archivo que desea cambiar los permisos se encuentra dentro del directorio del sistema, es posible que tenga que ser root
, así: (tenga cuidado, mientras usa el comando sudo
)
sudo chmod +x /usr/share/testfolder/aFile
También no está claro, lo que exactamente desea archivar aquí. Por favor, edita tu pregunta y da más detalles sobre el problema real.
También puede referirse a esta pregunta, para obtener más información: chmod u+x' versus 'chmod +x
Escriba man chmod
en una ventana de terminal (Ctrl+Alt+T) y obtendrá la siguiente salida:
NAME: chmod - cambiar los bits del modo de archivo
SINOPSIS
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
DESCRIPCIÓN
This manual page documents the GNU version of chmod. chmod changes the
file mode bits of each given file according to mode, which can be either
a symbolic representation of changes to make, or an octal number repre‐
senting the bit pattern for the new mode bits.
The format of a symbolic mode is [ugoa...][[+-=][perms...]...], where
perms is either zero or more letters from the set rwxXst, or a single
letter from the set ugo. Multiple symbolic modes can be given, sepa‐
rated by commas.
A combination of the letters ugoa controls which users' access to the
file will be changed: the user who owns it (u), other users in the
file's group (g), other users not in the file's group (o), or all users
(a). If none of these are given, the effect is as if a were given, but
bits that are set in the umask are not affected.
The operator + causes the selected file mode bits to be added to the
existing file mode bits of each file; - causes them to be removed; and =
causes them to be added and causes unmentioned bits to be removed except
that a directory's unmentioned set user and group ID bits are not
affected.
The letters rwxXst select file mode bits for the affected users: read
(r), write (w), execute (or search for directories) (x), execute/search
only if the file is a directory or already has execute permission for
some user (X), set user or group ID on execution (s), restricted dele‐
tion flag or sticky bit (t). Instead of one or more of these letters,
you can specify exactly one of the letters ugo: the permissions granted
to the user who owns the file (u), the permissions granted to other
users who are members of the file's group (g), and the permissions
granted to users that are in neither of the two preceding categories
(o).
A numeric mode is from one to four octal digits (0-7), derived by adding
up the bits with values 4, 2, and 1. Omitted digits are assumed to be
leading zeros. The first digit selects the set user ID (4) and set
group ID (2) and restricted deletion or sticky (1) attributes. The sec‐
ond digit selects permissions for the user who owns the file: read (4),
write (2), and execute (1); the third selects permissions for other
users in the file's group, with the same values; and the fourth for
other users not in the file's group, with the same values.
chmod never changes the permissions of symbolic links; the chmod system
call cannot change their permissions. This is not a problem since the
permissions of symbolic links are never used. However, for each sym‐
bolic link listed on the command line, chmod changes the permissions of
the pointed-to file. In contrast, chmod ignores symbolic links encoun‐
tered during recursive directory traversals.
BITS SETUID Y SETGID
chmod clears the set-group-ID bit of a regular file if the file's group
ID does not match the user's effective group ID or one of the user's
supplementary group IDs, unless the user has appropriate privileges.
Additional restrictions may cause the set-user-ID and set-group-ID bits
of MODE or RFILE to be ignored. This behavior depends on the policy and
functionality of the underlying chmod system call. When in doubt, check
the underlying system behavior.
OPCIONES
Change the mode of each FILE to MODE.
-c, --changes
like verbose but report only when a change is made
--no-preserve-root
do not treat `/' specially (the default)
--preserve-root
fail to operate recursively on `/'
-f, --silent, --quiet
suppress most error messages
-v, --verbose
output a diagnostic for every file processed
--reference=RFILE
use RFILE's mode instead of MODE values
-R, --recursive
change files and directories recursively
--help display this help and exit
--version
output version information and exit
Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.
En primer lugar, su script debe declarar qué intérprete utilizar. Esto se hace en la primera línea del archivo. Si se trata de un script de shell, debe ser #!/bin/sh
o #!/bin/bash
.
Así que aquí'hay un script que escribe su nombre de usuario: echo-whoami.sh
#!/bin/sh
echo $(whoami)
;
Para hacerlo ejecutable, utiliza chmod +x echo-whoami.sh
. Luego puedes ejecutarlo usando ./echo-whoami.sh
.
Un archivo batch y un script de shell son dos términos que significan efectivamente lo mismo en Linux. Sin embargo, el término script es mucho más utilizado.
El archivo de script de shell más sencillo sólo contiene comandos tal y como los escribirías en la línea de comandos (es decir, el intérprete de comandos Bash). En teoría, puedes incluso sustituir el intérprete por cualquier lenguaje que te guste (y para el que tengas un intérprete). Para ser más explícito, se sugiere que comience su primera línea con
#!/bin/sh
(si quieres la máxima portabilidad con sistemas heredados)
o
#!/bin/bash
(si quiere algunas características extra, que probablemente no le interesen hoy)
Después de esta línea introduce tus comandos, uno en cada línea. Hay muchas construcciones extra más allá del alcance de estas preguntas, vea man bash
o http://www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf (para principiantes) o http://www.tldp.org/LDP/abs/abs-guide.pdf (para preguntas más avanzadas).
Para ejecutar realmente tu script, hay dos requisitos: En primer lugar, el proceso de interpretación necesita leer el archivo, y en segundo lugar comprobar si está marcado como ejecutable. Por razones de conveniencia, también es útil poder escribir en tu script (para que puedas hacer cambios o correcciones si es necesario).
Asumiendo además que quieres que los miembros del equipo y otros puedan ejecutar (y ver) tu script, pero que no quieres que lo manipulen, la combinación de
a+x
o +x
, ya que a
es el valor por defecto)a+r
o +r
, ya que a
es de nuevo el valor por defecto),u=w
)suelen ser valores sensatos de los permisos de tus archivos. Puedes escribir las acciones individuales concatenadas, separadas con una coma.
Si bien este "lenguaje de acciones" es muy fascinante (nótese la diferencia del operador +
y el operador =
que dan lugar a diferentes resultados dependiendo de la configuración de los permisos antes de cambiarlos), son tediosos de escribir.
Como todas las acciones crean máscaras de bits que se aplican internamente, puedes escribir las máscaras de bits (ver man chmod
para más detalles) también directamente.
Para un shellscript chmod 755 myscript.sh tiene más sentido en al menos el 95% de los casos.