mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2024 (markdown)
13
SC2024.md
13
SC2024.md
@@ -1,11 +1,16 @@
|
|||||||
## `sudo` doesn't affect redirects. Use `..| sudo tee file`
|
## `sudo` doesn't affect redirects. Use `..| sudo tee file`
|
||||||
|
|
||||||
or "Use `sudo cat file | ..`" for input files.
|
or "Use `..| sudo tee -a file`" instead of `>>` to append.
|
||||||
|
|
||||||
|
or "Use `sudo cat file | ..`" instead of `<` to read.
|
||||||
|
|
||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
```
|
```
|
||||||
# Write to a file
|
# Write to a file
|
||||||
|
sudo echo 3 > /proc/sys/vm/drop_caches
|
||||||
|
|
||||||
|
# Append to a file
|
||||||
sudo echo 'export FOO=bar' >> /etc/profile
|
sudo echo 'export FOO=bar' >> /etc/profile
|
||||||
|
|
||||||
# Read from a file
|
# Read from a file
|
||||||
@@ -16,6 +21,9 @@ sudo wc -l < /etc/shadow
|
|||||||
|
|
||||||
```
|
```
|
||||||
# Write to a file
|
# Write to a file
|
||||||
|
echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null
|
||||||
|
|
||||||
|
# Append to a file
|
||||||
echo 'export FOO=bar' | sudo tee -a /etc/profile > /dev/null
|
echo 'export FOO=bar' | sudo tee -a /etc/profile > /dev/null
|
||||||
|
|
||||||
# Read from a file
|
# Read from a file
|
||||||
@@ -28,9 +36,10 @@ Redirections are performed by the current shell before `sudo` is started. This m
|
|||||||
|
|
||||||
* To *read* from a file that requires additional privileges, you can replace `sudo command < file` with `sudo cat file | command`.
|
* To *read* from a file that requires additional privileges, you can replace `sudo command < file` with `sudo cat file | command`.
|
||||||
* To *write* to a file that requires additional privileges, you can replace `sudo command > file` with `command | sudo tee file > /dev/null`
|
* To *write* to a file that requires additional privileges, you can replace `sudo command > file` with `command | sudo tee file > /dev/null`
|
||||||
|
* To *append* to a file, use the above with `tee -a`.
|
||||||
* If the file does *not* require special privileges but the command *does*, then you are already doing the right thing: please [[ignore]] the message.
|
* If the file does *not* require special privileges but the command *does*, then you are already doing the right thing: please [[ignore]] the message.
|
||||||
|
|
||||||
Both substitutions work by having a command open the file for reading or writing, instead of relying on the current shell. Since the command is run with elevated privileges, it will have access to files that the current user does not.
|
The substitutions work by having a command open the file for reading or writing, instead of relying on the current shell. Since the command is run with elevated privileges, it will have access to files that the current user does not.
|
||||||
|
|
||||||
Note: there is nothing special about `tee`. It's just the simplest command that can both truncate and append to files without help from the shell. Here are equivalent alternatives:
|
Note: there is nothing special about `tee`. It's just the simplest command that can both truncate and append to files without help from the shell. Here are equivalent alternatives:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user