How to add code to .bashrc without causing errors?

Asked by Ken

I'm learning how to passthrough my NIC to my KVM.

This guide https://mathiashueber.com/passthrough-windows-11-vm-ubuntu-22-04/ says to add the following code to the file ~/.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;

I'm unclear where the code should be inserted, so it's in lines 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

However this causes an error when running .bashrc with the command ./.bashrc

ubuntu@ubuntu:~$ ./.bashrc
./.bashrc: line 17: return: can only `return' from a function or sourced script

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
Last query:
Last reply:
Revision history for this message
Best Manfred Hampl (m-hampl) said :
#1

Where do you see an instruction to add this to the ~/.bashrc script? I do not see bashrc being mentioned anywhere.
My interpretation of the text is that you should execute this just once to identify the IOMMU groups.

Revision history for this message
Ken (kotgc) said :
#2

As a learner the confusion was the link to 'one can use a bashscript' askubuntu.com/questions/471285/…. As a learner looking for the location, this link's guide shows ~/bin, but I found .bashrc in ~/, so I assumed yet another guide with typos and worked in the ~/.bashrc location and file.

Revision history for this message
Ken (kotgc) said :
#3

Thanks Manfred Hampl, that solved my question.