Bash
!bash
help-circle
rss
Check for sudo access in bash script
Is this the correct way to check for sudo access? ``` has_sudo() { if [[ "$EUID" = 0 ]]; then exit 0 else sudo -k # make sure to ask for password on next sudo if sudo true; then exit 0 else exit 1 fi fi } load_zshenv_on_startup() { if has_sudo; then append_zshenv_to_global_files else link_zshenv_to_home_directory fi } ```
fedilink

How can I evaluate a variable before appending with tee?
I need to evaluate this variables before appending this lines to a few files: ``` sudo tee -a /etc/profile /etc/zshenv /etc/zsh/zshenv &>/dev/null <<EOF export XDG_CONFIG_HOME="$SYNC"/config export XDG_CACHE_HOME="$HOME"/.cache export XDG_DATA_HOME="$HOME"/.local/share export XDG_STATE_HOME="$HOME"/.local/state export ZDOTDIR="$XDG_CONFIG_HOME"/zsh EOF ```
fedilink

How to avoid part of a bash script from executing twice when re-running the script?
For example I have a function that appends to a file a few lines. But if something else fails in the script I wouldn't want to fix it and execute it again, and then have those lines duplicated.
fedilink


How to improve this fix permissions function efficiency?
How do I improve this function efficiency so that it only searches once instead of three times: ``` # Fixes permissions for files and directories in the given folder or the current folder fixperms() { find "${1:-.}" -type f -executable -exec chmod 744 {} \; find "${1:-.}" -type f ! -executable -exec chmod 644 {} \; find "${1:-.}" -type d -exec chmod 755 {} \; } ```
fedilink

How to clear zsh configuration?
I think it was `zsh -z` but I'm not sure. It's for doing this: ``` reload_zsh() { echo "Reload zsh" zsh -z [ -f $ZSHRC ] && source $ZSHRC } ```
fedilink

How to let every file know the absolute path of the root of a project and be able to change a folder path easily?
I want to let every file know the path of the root of a project. For example let's consider this the root: `/home/username/.config/project`. If I have a folder in the project and every file in the folder has a line like `ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"` (gets the absolute path of the parent directory) whenever I move the folder to another location I may have to change that line for every file in the folder. Is there a better option to let every file know the path of the project root?
fedilink

Ask specific questions about how to code something in sh, bash, zsh, etc

General bash discussion on lemmy.ml

Create Post From:
lemmy.ml

  • 0 users online
  • 2 users / day
  • 2 users / week
  • 2 users / month
  • 5 users / 6 months
  • 17 subscribers
  • 9 Posts
  • 17 Comments
  • Modlog