我想要一个命令(或者可能是cp的一个选项),如果它不存在,则创建目标目录.
例:
cp -? file /path/to/copy/file/to/is/very/deep/there
Run Code Online (Sandbox Code Playgroud) 我有一个包含目录名称的文件:
my_list.txt :
/tmp
/var/tmp
Run Code Online (Sandbox Code Playgroud)
我想在我添加目录名之前检查Bash,如果该名称已存在于文件中.
给定这样的文件名:
/the/path/foo.txt
bar.txt
Run Code Online (Sandbox Code Playgroud)
我希望得到:
foo
bar
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
#!/bin/bash
fullfile=$1
fname=$(basename $fullfile)
fbname=${fname%.*}
echo $fbname
Run Code Online (Sandbox Code Playgroud)
什么是正确的方法呢?
我正在尝试从基于列的"空格"调整的文本流中提取某个(第四个)字段.我正在尝试以cut下列方式使用该命令:
cat text.txt | cut -d " " -f 4
不幸的是,cut不会将多个空格视为一个分隔符.我本可以通过awk进行管道传输
awk '{ printf $4; }'
或者sed
sed -E "s/[[:space:]]+/ /g"
崩溃的空间,但我想知道是否有任何办法处理cut和本地几个分隔符?
我知道如何在Bash脚本中编写多行命令,但是如何在多行命令中为每一行添加注释?
CommandName InputFiles \ # This is the comment for the 1st line
--option1 arg1 \ # This is the comment for the 2nd line
--option2 arg2 # This is the comment for the 3nd line
Run Code Online (Sandbox Code Playgroud)
但不幸的是,继续角色之后的评论\将打破命令.
我有一个文件,foo.txt包含以下行:
a
b
c
Run Code Online (Sandbox Code Playgroud)
我想要一个简单的命令,导致内容为foo.txt:
a
b
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个用于测试的bash脚本,它接受一个参数并通过curl将其发送到网站.我需要对值进行url编码,以确保正确处理特殊字符.做这个的最好方式是什么?
这是我到目前为止的基本脚本:
#!/bin/bash
host=${1:?'bad host'}
value=$2
shift
shift
curl -v -d "param=${value}" http://${host}/somepath $@
Run Code Online (Sandbox Code Playgroud) 如何编写脚本在Ubuntu上安装MySQL服务器?
sudo apt-get install mysql 将安装,但它也会要求在控制台中输入密码.
我如何以非交互方式执行此操作?也就是说,编写一个可以提供密码的脚本?
#!/bin/bash
sudo apt-get install mysql # To install MySQL server
# How to write script for assigning password to MySQL root user
# End
Run Code Online (Sandbox Code Playgroud) 在许多SO问题和bash教程中,我看到我可以通过两种方式访问bash脚本中的命令行参数:
$ ~ >cat testargs.sh
#!/bin/bash
echo "you passed me" $*
echo "you passed me" $@
Run Code Online (Sandbox Code Playgroud)
结果如下:
$ ~> bash testargs.sh arg1 arg2
you passed me arg1 arg2
you passed me arg1 arg2
Run Code Online (Sandbox Code Playgroud)
$*和之间有什么区别$@?
应该何时使用前者,何时使用后者?