Как сказал Аркадиуш, можно создать обертку:
dd() {
# Limit variables' scope
local args command output reply
# Basic arguments handling
while (( ${#} > 0 )); do
case "${1}" in
( of=* )
output="${1#*=}"
;;
( * )
args+=( "${1}" )
;;
esac
shift || break
done
# Build the actual command
command=( command -- dd "${args[@]}" "of=${output}" )
# Warn the user
printf 'Please double-check this to avoid potentially dangerous behavior.\n' >&2
printf 'Output file: %s\n' "${output}" >&2
# Ask for confirmation
IFS= read -p 'Do you want to continue? (y/n): ' -r reply
# Check user's reply
case "${reply}" in
( y | yes )
printf 'Running command...\n' >&2
;;
( * )
printf 'Aborting\n' >&2
return
;;
esac
# Run command
"${command[@]}"
}
Пример:
$ dd if=/dev/urandom of=file.txt bs=4M count=5
Please double-check this to avoid potentially dangerous behavior.
Output file: file.txt
Do you want to continue? (y/n): y
Running command...
5+0 records in
5+0 records out
20971520 bytes (21 MB, 20 MiB) copied, 0.443037 s, 47.3 MB/s
Измените его в соответствии со своими потребностями (сделайте его совместимым с POSIX -, проверьте другие условия и т. д. ).
Похоже, вы используете версию bash, которая не понимает отрицательный массив, такой как array[-1]
. Вы можете установить более новую версию bash (выше 4.3 )или попробовать этот скрипт:
fib () {
fib_array=( 0 1 ); size=2
limit=$1
while [[ size -lt limit ]]
do
fib_arr+=( "$(( ${fib_arr[size-1]} + ${fib_arr[size-2]} ))" )
size=${#fib_arr[@]}
done
echo " ${fib_arr[*]}"
}
fib "${1:-10}"
, которые используют размер (переменной массива ), чтобы избежать отрицательных индексов.
Или, если вам нужен более быстрый вариант, который работает для всех оболочек bash, используйте:
fib () {
limit=$1
set -- 0 1
printf '%s %s' "$1" "$2"; count=3
while [ "$count" -le "$limit" ]; do
t33=$(($1+$2))
set -- "$2" "$t33"
printf ' %s' "$t33"
count=$((count+1))
done
echo
}
fib ${1:-10}
Вам нужно поставить [count -1] вместо, если [ -1] и [count -2] вместо, если [ -2]