如何提示用户输入sudo密码?

时间:2017-11-28 18:31:33

标签: linux bash shell fedora

我的问题是,在运行此bash脚本时,如何在选择后实现询问sudo密码。例如,如果用户选择在/目录中搜索脚本,则返回权限被拒绝。我想弄清楚如何让脚本提示输入密码,然后继续运行。

let sortedArr = arr.sorted { (id1, id2) -> Bool in
  return id1.fileId < id2.fileId // Use > for Descending order
}

1 个答案:

答案 0 :(得分:-1)

你可以做这样的事,虽然有点讨厌:

library(tidyverse)

# read in your new, two column csv
replacementcities  <- read.csv('replacement-cities.csv')

fixeddata  <- data %>%
    # join the two data frames where the city columns match
    left_join(replacementcities, by = 'city') %>%

    # replace city with whatever is not NA, using new_city first 
    mutate( city = coalesce(new_city, city) ) %>%

    # drop the, now unnecessary, new_city column
    select(-new_city)

基本上#!/bin/bash function press_enter { echo "" echo -n "Press Enter to continue" read clear } selection= where_selection= what_selection= sudo="" until [ "$selection" = "3" ]; do echo -e "Where would you like to search 1- Root 2- Home 3- Exit Enter your choice ---> \c" read selection case $selection in 1) cd / ; sudo="sudo"; press_enter ;; 2) cd /home ; sudo=""; press_enter ;; 3) echo "Have a great day!" ; exit ;; esac echo "What is the name of the file you would like to search for?" read -r a if $sudo find . -name "$a" -print -quit | grep -q . then echo "You found the file" else echo "You haven't found the file" fi done 是一个空字符串,除非用户选择1,然后它将运行&#34; sudo&#34;。更好的方法是使用第二个$sudo块,如果需要,可以使用sudo运行命令。