该脚本是:
#!/bin/bash
# Dynamic Menu Function
createmenu () {
select selected_option; do # in "$@" is the default
if [ 1 -le "$REPLY" ] && [ "$REPLY" -le $(($#)) ]; then
break;
else
echo "Please make a vaild selection (1-$#)."
fi
done
}
declare -a drives=();
# Load Menu by Line of Returned Command
mapfile -t drives < <(lsblk --nodeps -o name,serial,size | grep "sd");
# Display Menu and Prompt for Input
echo "Available Drives (Please select one):";
createmenu "${drives[@]}" …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种使用标准shell命令确定正在运行的进程的位数的简单而可靠的方法.
虽然我已经看到了Solaris和Linux的解决方案,但我希望找到一种适用于更加简单,可靠和可移植性的解决方案.
在Linux中这可以使用来完成/proc/$PID/exe,/proc/$PID/maps,/proc/$PID/auxv例如但这些方法或者不存在在Solaris或不能容易地从调用sh.
在Solaris中这样做的方式pflags $PID却pflags装不上大多数股票的Linux发行版.
我需要在我的shell脚本中使用花括号对命令进行分组,以便我可以将它们的输出定向到单独的日志文件,如此...
>cat how-to-exit-script-from-within-curly-braces.sh
{
printf "%d\n" 1
printf "%d\n" 2
} | tee a.log
{
printf "%d\n" 3
printf "%d\n" 4
} | tee b.log
>./how-to-exit-script-from-within-curly-braces.sh
1
2
3
4
>cat a.log
1
2
>cat b.log
3
4
>
Run Code Online (Sandbox Code Playgroud)
虽然我添加了花括号以方便日志记录,但我仍然希望在大括号内调用exit命令时退出脚本.
它当然不会这样做.它只退出花括号,然后继续执行脚本的其余部分,如此...
>cat how-to-exit-script-from-within-curly-braces.sh
{
printf "%d\n" 1
exit
printf "%d\n" 2
} | tee a.log
{
printf "%d\n" 3
printf "%d\n" 4
} | tee b.log
>./how-to-exit-script-from-within-curly-braces.sh
1
3
4
>cat a.log
1
>cat b.log
3
4
>
Run Code Online (Sandbox Code Playgroud)
使退出代码非零并将"set -e"添加到脚本似乎不起作用... …
作为形象。所有命令都是相似的。我知道如何使用它,但是我真的不知道细节。有人知道吗?非常感谢你。
# does `cat` read fd and print?
$ cat file
# does `cat` read from stdin and print?
$ cat < file
$ cat - < file
# with heredoc or herestring, what methods `cat` command use to read from heredoc?stdin?
$ cat << EOF
heredoc> test
heredoc> EOF
test
$ cat <<< "test"
$ cat - << EOF
heredoc> test
heredoc> EOF
test
$ cat - <<< "test"
# and I dont why these commands works?
$ cat …Run Code Online (Sandbox Code Playgroud) 我写了一个小的NEOS备份脚本来转储我的mysql数据库并压缩它。
在我的笔记本电脑(Deepin OS)上,脚本运行良好,但在服务器上却抛出错误...
https://gist.github.com/breadlesscode/2f2e274a3c9314b99ac71fc6b2afb07a
空行会引发错误?我不知道为什么。我尝试过不同的行进,但是确实行得通。
bash -x ./crons/BackupNeos.sh输出:
+ BACKUP_FOLDER=%s_NEOS_Backup
+ NEOS_ROOT=$'/html/neos\r'
+ BACKUP_ROOT=$'/html/backups\r'
+ NEOS_PACKAGE=$'My.Package\r'
+ COMPRESS_BACKUP=$'true\r'
+ NEOS_FLOW_EXPORT=$'true\r'
+ MYSQL_FULL_BACKUP=$'true\r'
+ MYSQL_HOST=$'server\r'
+ MYSQL_USER=$'user\r'
+ MYSQL_PW=$'password\r'
+ MYSQL_DB=$'database\r'
+++ date +%Y-%m-%d_%H-%M-%S
++ printf %s_NEOS_Backup 2017-01-15_18-53-30
+ BACKUP_FOLDER=$'2017-01-15_18-53-30_NEOS_Backup\r'
++ printf %s/%s $'/html/backups\r' $'2017-01-15_18-53-30_NEOS_Backup\r'
+ BACKUP_FOLDER_PATH=$'/html/backups\r/2017-01-15_18-53-30_NEOS_Backup\r\r'
+ $'\r'
: Command not found.le 18:
' printf 'Backup script start:\n\n
Backup script start:
+ cd $'/html/backups\r\r'
: File or directory not foundtml/backups
' printf 'Create backup folder...
+ mkdir $'2017-01-15_18-53-30_NEOS_Backup\r\r'
' printf …Run Code Online (Sandbox Code Playgroud) 我有一个以下格式的文本文件:
Name Place Mobile
Jon Sam India 1234567890
Barack Obama USA 0987654321
Run Code Online (Sandbox Code Playgroud)
每个字段由制表符空格分隔.我想通过shell脚本提取每个字段.我使用以下代码:
while IFS= read -r var; do
echo $var | awk -F"\t" '{print $1,"->"$2,"->"$3}'
done < myfile
Run Code Online (Sandbox Code Playgroud)
我期待以下风格的输出:
Jon Sam->India->1234567890
Run Code Online (Sandbox Code Playgroud)
但它打印如下:
Jon Sam India 1234567890-> ->
Run Code Online (Sandbox Code Playgroud)
这意味着,不会发生分裂.我的计划有什么问题?
我有这个shell脚本变量var.它保留3个以新行分隔的条目.从这个变量var,我想提取2和0.078688.只是这两个数字.
var="USER_ID=2
# 0.078688
Suhas"
Run Code Online (Sandbox Code Playgroud)
这些是我试过的代码:
echo "$var" | grep -o -P '(?<=\=).*(?=\n)' # For extracting 2
echo "$var" | awk -v FS="(# |\n)" '{print $2}' # For extracting 0.078688
Run Code Online (Sandbox Code Playgroud)
以上都没有工作.这里有什么问题?如何解决这个问题?
尝试使用shell命令替换大量文件中的值(在脚本中使用以迭代目录中的文件).
Input file examples:
file1:
019=False
051=Limited
051>Limited Lease Time=2419200
file2:
019=False
051=Limited
051>Limited Lease Time=14400
Run Code Online (Sandbox Code Playgroud)
这工作没问题:
sed -e '/Limited Lease Time=/s/[0-9]\+$/86400/g' <infile >outfile
# cat outfile
019=False
051=Limited
051>Limited Lease Time=86400
Run Code Online (Sandbox Code Playgroud)
但是......然后意识到我正在替换任何值,包括低于86400的值,当时我只想更换大于86400的任何值.
我以为我可以使用awk,这种'有点'工作,因为在输出中只有'限制租用时间'行,其值> 86400:
awk -F= '/Limited Lease Time/ && $2 > 86400 { print $2 }'
Run Code Online (Sandbox Code Playgroud)
但是......它只输出匹配的行,而我需要原始输出(示例中为3行),包括修改,如果匹配值> 86400.
感谢一些处理这个问题的建议!
如何在当前工作目录中递归删除所有不带.txt和.exe扩展名的文件?我需要一线。
我试过了:
find . ! -name "*.txt" "*.exe" -exec rm -r {} \
find -type f -regextype posix-extended -iregex '.*\.(txt|exe)$'
Run Code Online (Sandbox Code Playgroud)