官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 596字
  • 2021-07-23 19:17:40

How it works...

First, before digging into the script itself or even the attributes/properties of files, we need to know a few things about Linux and its sibling operating systems:

  • Files and directories can be owned. This means that they can have an owner (user) and groups associated with their ownership. For this, we can use the chown and chgrp commands.
  • Files and directories can have different permissions applied to them. This means that they may be executable, readable, writable, and/or everything. For this, we can use the chmod command and the appropriate permission setting.
  • Files and directories can also be empty.

Great! Furthermore, there are two more concepts that need to be introduced:

  • The read command, which is used to wait for user input and read it into a variable. It is also useful for "pause" functionality in scripts.
  • Recursive functions. Notice that inside of the script unless it exits or the user presses ctl + C, the script keeps calling a particular function. This is recursion and it will continue unless stopped or a limit is applied.

At this point, we also know functions, parameters, input/output, return codes, subshells, and conditional logic. You may not have noticed the ! character, and this is used to negate a statement. For example, if we test for the existence of fileops/bobs.txt using the -e test operator, it will return true. Instead, we can test for the opposite, which is that fileops/bobs.txt is non-existent.

The same logic as inverting or negating statements can also be achieved using if/else functionality, but it can sometimes improve script "readability" and "flow". Ultimately, the choice to use inversion is up to the script writer.
  1. Great! We have created our script and are ready to execute it.
  2. Upon executing the script, we are greeted with:
$ ./files-extended.sh 
Welcome to the file attributes tester

To exit, press CTRL + C

What is the complete path of the file you want to inspect?
#

If we look back at the setup for this recipe, we know that we created several files inside of the directory fileops/ and that a few of them have different permissions, one of which is owned by a user named Bob

Let's try a few executions (in order) :

  • fileops/bobs.txt
  • fileops/write.txt
  • fileops/exec.txt
  • fileops/all.txt
  • thisDoesNotExist.txt:
# fileops/bobs.txt

"fileops/bobs.txt" file size is: 11"
rbrash is not the owner of "fileops/bobs.txt"

rbrash is not among the owning group(s) for "fileops/bobs.txt"

What are our permissions on this file?

[R] Read

What is the complete path of the file you want to inspect?
# fileops/write.txt

"fileops/write.txt" is empty

What are our permissions on this file?

[W] Write

What is the complete path of the file you want to inspect?
# fileops/exec.txt

"fileops/exec.txt" is empty

What are our permissions on this file?

{X] Exec

What is the complete path of the file you want to inspect?
# fileops/all.txt

"fileops/all.txt" is empty

What are our permissions on this file?

[R] Read
[W] Write
{X] Exec

What is the complete path of the file you want to inspect?
# fileops

Directory "fileops" has children:

all.txt
bobs.txt
empty.txt
exec.txt
read.txt
string.txt
swordinthestone.txt
write.txt

What are our permissions on this directory?

[R] Read
[W] Write
{X] Exec

What is the complete path of the file you want to inspect?
# thisDoesNotExist.txt

Error: "thisDoesNotExist.txt" does not exist!
$

As thisDoesNotExist.txt does not exist, the script abruptly exits and places you back at the console prompt. We tested with the various flags, negation, ownership, and even our ever useful utility xargs.

主站蜘蛛池模板: 胶州市| 长汀县| 香河县| 五河县| 鄂伦春自治旗| 丰城市| 西藏| 广宁县| 南宁市| 陆良县| 依兰县| 泗水县| 衡水市| 乌拉特中旗| 永平县| 邯郸市| 汝南县| 班玛县| 长治县| 营口市| 安图县| 江阴市| 玉田县| 盘锦市| 墨江| 克拉玛依市| 柏乡县| 英吉沙县| 嘉义县| 石首市| 区。| 永善县| 吴江市| 永昌县| 绥阳县| 兰溪市| 鄂伦春自治旗| 阳信县| 大荔县| 华池县| 东明县|