我正在学习如何将我的 NIC 传递到我的 KVM。
这指导说将以下代码添加到文件 ~/.bashrc
#!/bin/bash
# change the 999 if needed
shopt -s nullglob
for d in /sys/kernel/iommu_groups/{0..999}/devices/*; do
n=${d#*/iommu_groups/*}; n=${n%%/*}
printf 'IOMMU Group %s ' "$n"
lspci -nns "${d##*/}"
done;
我不清楚应该将代码插入哪里,所以它在第 1-8 行。
1 #!/bin/bash
2 # change the 999 if needed
3 shopt -s nullglob
4 for d in /sys/kernel/iommu_groups/{0..999}/devices/*; do
5 n=${d#*/iommu_groups/*}; n=${n%%/*}
6 printf 'IOMMU Group %s ' "$n"
7 lspci -nns "${d##*/}"
8 done;
9
10 # ~/.bashrc: executed by bash(1) for non-login shells.
11 # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
12 # for examples
13
14 # If not running interactively, don't do anything
15 case $- in
16 *i*) ;;
17 *) return;;
18 esac
19
20 # don't put duplicate lines or lines starting with space in the history.
21 # See bash(1) for more options
22 HISTCONTROL=ignoreboth
23
24 # append to the history file, don't overwrite it
25 shopt -s histappend
26
27 # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
28 HISTSIZE=1000
29 HISTFILESIZE=2000
30
31 # check the window size after each command and, if necessary,
32 # update the values of LINES and COLUMNS.
33 shopt -s checkwinsize
34
35 # If set, the pattern "**" used in a pathname expansion context will
36 # match all files and zero or more directories and subdirectories.
37 #shopt -s globstar
38
39 # make less more friendly for non-text input files, see lesspipe(1)
40 [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
41
42 # set variable identifying the chroot you work in (used in the prompt below)
43 if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
44 debian_chroot=$(cat /etc/debian_chroot)
45 fi
46
47 # set a fancy prompt (non-color, unless we know we "want" color)
48 case "$TERM" in
49 xterm-color|*-256color) color_prompt=yes;;
50 esac
51
52 # uncomment for a colored prompt, if the terminal has the capability; turned
53 # off by default to not distract the user: the focus in a terminal window
54 # should be on the output of commands, not on the prompt
55 #force_color_prompt=yes
56
57 if [ -n "$force_color_prompt" ]; then
58 if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
59 # We have color support; assume it's compliant with Ecma-48
60 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
61 # a case would tend to support setf rather than setaf.)
62 color_prompt=yes
63 else
64 color_prompt=
65 fi
66 fi
67
68 if [ "$color_prompt" = yes ]; then
69 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
70 else
71 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
72 fi
73 unset color_prompt force_color_prompt
74
75 # If this is an xterm set the title to user@host:dir
76 case "$TERM" in
77 xterm*|rxvt*)
78 PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
79 ;;
80 *)
81 ;;
82 esac
83
84 # enable color support of ls and also add handy aliases
85 if [ -x /usr/bin/dircolors ]; then
86 test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
87 alias ls='ls --color=auto'
88 #alias dir='dir --color=auto'
89 #alias vdir='vdir --color=auto'
90
91 alias grep='grep --color=auto'
92 alias fgrep='fgrep --color=auto'
93 alias egrep='egrep --color=auto'
94 fi
95
96 # colored GCC warnings and errors
97 #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
98
99 # some more ls aliases
100 alias ll='ls -alF'
101 alias la='ls -A'
102 alias l='ls -CF'
103
104 # Add an "alert" alias for long running commands. Use like so:
105 # sleep 10; alert
106 alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1 |sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
107
108 # Alias definitions.
109 # You may want to put all your additions into a separate file like
110 # ~/.bash_aliases, instead of adding them here directly.
111 # See /usr/share/doc/bash-doc/examples in the bash-doc package.
112
113 if [ -f ~/.bash_aliases ]; then
114 . ~/.bash_aliases
115 fi
116
117 # enable programmable completion features (you don't need to enable
118 # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
119 # sources /etc/bash.bashrc).
120 if ! shopt -oq posix; then
121 if [ -f /usr/share/bash-completion/bash_completion ]; then
122 . /usr/share/bash-completion/bash_completion
123 elif [ -f /etc/bash_completion ]; then
124 . /etc/bash_completion
125 fi
126 fi
但是,当使用命令 ./.bashrc 运行 .bashrc 时会导致错误
ubuntu@ubuntu:~$ ./.bashrc
./.bashrc: line 17: return: can only `return' from a function or sourced script
答案1
您正在遵循的指南不是告诉您将此代码添加到,它会告诉您将其作为脚本运行。该页面中~/.bashrc
没有任何地方提及。这是有道理的。每次启动新的交互式非登录 shell 会话时,都会执行 中的任何命令。所以每次打开终端时。你不想每次都运行这个代码!~/.bashrc
~/.bashrc
没有必要,代码不是为此设计的,请注意它将如何为找到的每个文件打印一条消息。
最后,代码本身被写成脚本,从#!/bin/bash
舍邦线,进一步表明它与没有任何关系~/.bashrc
,并且无论如何,该代码除了打印出信息外不会执行任何操作:
shopt -s nullglob
:打开nullglob 选项。for d in /sys/kernel/iommu_groups/{0..999}/devices/*; do
:遍历中的所有文件,即名称为 0 到 999 之间的数字的/sys/kernel/iommu_groups/{0..999}/devices/
所有目录,然后是其下所有子目录中的所有文件。/sys/kernel/iommu_groups/
devices
n=${d#*/iommu_groups/*}; n=${n%%/*}
: 使用shell 字符串操作提取 IOMMU 组的名称,即当前/sys/kernel/iommu_groups/
子目录的名称(这是一种奇怪的复杂方法,但这是指南所推荐的)。lspci -nns "${d##*/}"
:使用该lspci
命令打印出这些指向的设备的文本和数字ID。
所以这只是一种提取信息供您查看和使用的简单方法。这里没有任何自动操作,它只是将内容打印到终端。
为了完整起见,这里有一个更简单的方法来做同样的事情,但不要把它放进你的~/.bashrc
,只需运行一次,看看输出,然后按照指南操作。但这次请尝试更仔细地阅读指南!
$ for d in /sys/kernel/iommu_groups/[0-9]*; do
[ -e "$d"/devices ] && printf 'IOMMU Group %s\n' "${d##*/}" ;
done
在我的系统上,返回:
IOMMU Group 0
IOMMU Group 1
IOMMU Group 10
IOMMU Group 11
IOMMU Group 12
IOMMU Group 13
IOMMU Group 14
IOMMU Group 15
IOMMU Group 16
IOMMU Group 17
IOMMU Group 18
IOMMU Group 2
IOMMU Group 3
IOMMU Group 4
IOMMU Group 5
IOMMU Group 6
IOMMU Group 7
IOMMU Group 8
IOMMU Group 9