Vreau să scriu analogul Ubuntu al unui "batch file" (un script shell). Dar nu știu cum să folosesc comanda chmod +x filename
pentru a face în așa fel încât scriptul să poată fi rulat. Și nici nu știu unde să o folosesc.
chmod +x
pe un fișier (scriptul dumneavoastră) înseamnă doar că îl veți face executabil. Faceți clic dreapta pe scriptul dvs. și alegeți Properties -> Permissions -> Allow executing file as program, vă lasă cu exact același rezultat ca și comanda din terminal.
Dacă un fișier pentru care doriți să modificați permisiunile se află în directorul de sistem, este posibil să fie nevoie să fiți root
, astfel: (aveți grijă, în timp ce utilizați comanda sudo
)
sudo chmod +x /usr/share/testfolder/aFile
De asemenea, nu este clar ce anume doriți să arhivați aici. Vă rugăm să vă editați întrebarea și să dați mai multe detalii despre problema reală!
Pentru mai multe informații, puteți consulta și această întrebare: chmod u+x' versus 'chmod +x1
Tastați man chmod
într-o fereastră de terminal (Ctrl+Alt+T) și veți obține următoarea ieșire:
Nume: chmod - modifică biți de mod de fișier
SYNOPSIS
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
DESCRIERE
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.
BIȚI DE SETUID ȘI 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.
OPȚIUNI
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]))+'.
În primul rând, scriptul dvs. trebuie să declare ce interpretor să folosească. Acest lucru se face în prima linie a fișierului. Dacă este un script shell, ar trebui să fie #!/bin/sh
sau #!/bin/bash
.
Iată deci un script care scrie numele de utilizator: echo-whoami.sh
#!/bin/sh
echo $(whoami)
Pentru a-l face executabil, folosiți chmod +x echo-whoami.sh
. Apoi îl puteți rula folosind ./echo-whoami.sh
.
Un fișier de lot și un script de shell sunt doi termeni care au aceeași semnificație în Linux. Totuși, termenul script este mult mai des folosit.
Cel mai simplu fișier de script shell conține doar comenzi așa cum le-ați tasta pe linia de comandă (adică în interpretorul de comenzi Bash). Teoretic, puteți chiar să înlocuiți interpretorul cu orice limbaj pe care îl doriți (și pentru care aveți un interpretor). Pentru a fi mai explicit, se sugerează să începeți prima linie cu
#!/bin/sh
(dacă vreți portabilitate maximă cu sistemele vechi)
sau
#!/bin/bash
(dacă doriți unele caracteristici suplimentare, de care probabil nu vă interesează astăzi)
După această linie introduceți comenzile dumneavoastră, câte una pe fiecare linie. Există o mulțime de construcții suplimentare care depășesc scopul acestor întrebări, consultați man bash
sau http://www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf (pentru începători) sau http://www.tldp.org/LDP/abs/abs-guide.pdf (pentru întrebări mai avansate).
Pentru a rula efectiv scriptul dumneavoastră, există două cerințe: În primul rând, procesul de interpretare trebuie să citească fișierul și, în al doilea rând, să verifice dacă acesta este marcat ca executabil. Din motive de comoditate, este de asemenea util să puteți scrie în scriptul dvs. (astfel încât să puteți face modificări sau corecturi dacă este necesar).
Presupunând, de asemenea, că doriți ca membrii echipei și alte persoane să poată rula (și vedea) scriptul dumneavoastră, dar că nu doriți ca aceștia să îl manipuleze, combinația de
a+x
sau +x
, deoarece a
este cea implicită),a+r
sau +r
, deoarece a
este din nou valoarea implicită),u=w
)sunt, de obicei, valori sensibile ale permisiunilor pentru fișierele dumneavoastră. Puteți tasta acțiunile individuale concatenate, separate prin virgulă.
Deși acest "limbaj al acțiunilor" este foarte fascinant (observați diferența dintre operatorii +
și =
, care au rezultate diferite în funcție de setarea permisiunilor înainte de a le modifica), acestea sunt plictisitoare de tastat.
Deoarece toate acțiunile creează măști de biți care sunt aplicate intern, puteți tasta și direct măștile de biți (a se vedea man chmod
pentru detalii).
Pentru un shellscript chmod 755 myscript.sh are cel mai mult sens în cel puțin 95% din toate cazurile.