在bash中,我需要检查字符串是否以"#"符号开头.我怎么做?
这是我的 -
if [[ $line =~ '#*' ]]; then
echo "$line starts with #" ;
fi
Run Code Online (Sandbox Code Playgroud)
我想在一个文件上运行这个脚本,文件看起来像这样 -
03930
#90329
43929
#39839
Run Code Online (Sandbox Code Playgroud)
这是我的剧本 -
while read line ; do
if [[ $line =~ '#*' ]]; then
echo "$line starts with #" ;
fi
done < data.in
Run Code Online (Sandbox Code Playgroud)
这是我的预期输出 -
#90329 starts with #
#39839 starts with #
Run Code Online (Sandbox Code Playgroud)
但我无法让它发挥作用,任何想法?
我有这个脚本 -
nmapout=`sudo nmap -sP 10.0.0.0/24`
names=`echo "$nmapout" | grep "MAC" | grep -o '(.\+)'`
echo "$names"
Run Code Online (Sandbox Code Playgroud)
现在$names
变量包含用换行符分隔的字符串 -
>_
(Netgear)
(Hon Hai Precision Ind. Co.)
(Apple)
Run Code Online (Sandbox Code Playgroud)
我尝试用子串方法进行数组转换 -
names=(${names//\\n/ })
echo "${names[@]}"
Run Code Online (Sandbox Code Playgroud)
但问题是${names[$i]
,如果我运行此循环,我无法通过索引(即等)访问它们-
for (( i=0; i<${#names[@]}; i++ ))
do
echo "$i: ${names[$i]"
# do some processing with ${names[$i]}
done
Run Code Online (Sandbox Code Playgroud)
我得到这个输出 -
>_
0: (Netgear)
1: (Hon
2: Hai
Run Code Online (Sandbox Code Playgroud)
但我想要的是 -
>_
0: (Netgear)
1: (Hon Hai Precision Ind. Co.)
2: (Apple)
Run Code Online (Sandbox Code Playgroud)
我无法找到一个好方法,请注意第二个字符串中有空格.
任何的想法 ?
我必须计算这样的多项式 -
f(x)= x ^ 4 - 2.274x ^ 3 + 1.8x ^ 2 - 0.576x + 1.0
这个lisp函数 -
(defun polynomial (x)
(+ (+ (+ (+ (expt x 4) (* -2.274 * (expt x 3)))
(* 1.8 (* x x))) (* -0.576 x)) 0.1))
Run Code Online (Sandbox Code Playgroud)
当我调用(多项式0.5)时,结果在不同的评估中是不同的,如下所示 -
CL-USER> (polynomial 0.5)
-1.9495
CL-USER> (polynomial 0.5)
0.8786454
CL-USER> (polynomial 0.5)
0.07474504
CL-USER> (polynomial 0.5)
0.3032537
CL-USER> (polynomial 0.5)
0.23830011
CL-USER>
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事 ?我正在使用最新的sbcl.
我是一个LISP新手.
为了获得列表的运行总和,我写的是 -
(setf sum 0.0)
(mapcar #'(lambda(x)
(setf sum (+ sum x)) sum) values))
Run Code Online (Sandbox Code Playgroud)
例如,如果您'(1 2 3 4)
输入,则上述代码将'(1 3 6 10)
作为输出返回,依此类推.
是否可以在不使用全局变量的情况下(以更优雅的方式)执行相同的操作sum
?
我试图在需要可点击的终端上转储 url,并且该 url 带有一个查询参数。例如 -
google='https://www.google.com/search?q='
orgname='foo bar'
gsearch=$google\'$orgname\'
echo "details: $orgname ($gsearch)"
Run Code Online (Sandbox Code Playgroud)
但问题是,可点击的链接完全忽略了 ie 之后的所有内容q=
,不包含该字符串'foo bar'
,请参见下图 -
如何制作包含查询的可点击链接(即上面大括号中的整个网址)?
另请注意,我在搜索参数中添加引号,因为它可能包含空格。
我有一个假设将2D数组作为参数的函数,我的代码看起来像这样 -
#include <stdio.h>
#include <stdlib.h>
void func(double**, int);
int main()
{
double m[3][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
func(m, 3);
}
void func(double **m, int dim)
{
int i, j ;
for(i = 0 ; i < dim ; i++)
{
for(j = 0 ; j < dim ; j++)
printf("%0.2f ", m[i][j]);
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud)
然后编译器说 -
test.c: In function ‘main’:
test.c:9:2: warning: passing argument 1 of ‘func’ from incompatible pointer type [enabled …
Run Code Online (Sandbox Code Playgroud) 请看下面的代码 -
public interface TestInterface {
public static String NON_CONST_B = "" ;
}
public class Implemented implements TestInterface {
public static String NON_CONST_C = "" ;
}
public class AutoFinal {
public static String NON_CONST_A = "" ;
public static void main(String args[]) {
TestInterface.NON_CONST_B = "hello-b" ;
Implemented.NON_CONST_C = "hello-c";
AutoFinal.NON_CONST_A = "hello-a" ;
Implemented obj = new Implemented();
}
}
Run Code Online (Sandbox Code Playgroud)
然而,编译器抱怨这TestInterface.NON_CONST_B
是最终的 -
AutoFinal.java:6: error: cannot assign a value to final variable NON_CONST_B
TestInterface.NON_CONST_B = "hello-b" …
Run Code Online (Sandbox Code Playgroud) 假设我正在使用 matplotlib 绘制 3D 散点图。我正在使用此处给出的代码:https ://matplotlib.org/2.1.1/gallery/mplot3d/scatter3d.html
出于我的目的,我需要删除轴、刻度线等。所以我这样做:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
ax.set_axis_off()
Run Code Online (Sandbox Code Playgroud)
我还删除了所有轴标签。要删除白色填充,我将像这样保存图形:
plt.savefig("test.png", bbox_inches = 'tight', pad_inches = 0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但我想要的是一个仅限制所有数据点所在的部分的图形,如下所示:
我需要一个incf
在增量期间执行某些边界检查的函数:
val := val + delta
if val >= 1.0
then return 1.0
else return val
Run Code Online (Sandbox Code Playgroud)
我可以这样写incf
:
(defun incf-bounded(val delta)
(incf val delta)
(if (>= val 1.0) 1.0 val))
Run Code Online (Sandbox Code Playgroud)
在这种情况下我需要使用它(setf x (incf-bounded x delta))
.但是我该怎么写一个我可以使用的(incf-bounded x delta)
,比如,哪里x
会被修改?
我的操作系统(Ubuntu 12.04)附带预先打包的Qt4库和其他相关内容.但我想使用最新的香草Qt 5.2.1,我已经安装它并且它工作正常,假设我编译了这个依赖于Qt的框架.
我要做的是将Qt 5.2.1库添加到$LD_LIBRARY_PATH
.我的Qt 5.2.1主路径是/opt/Qt5.2.1/5.2.1/gcc_64
,因此我的$LD_LIBRARY_PATH
包含/opt/Qt5.2.1/5.2.1/gcc_64/lib
(和/opt/Qt5.2.1/Tools/QtCreator/lib
qtcreator相关的东西)
但是,最近我正在尝试编译最新的gnu 八度音阶.它的configure
脚本通过调用来查找Qt cflags
(即-l
/ -I
/ -L
flags)pkg-config
.
我知道pkg-config查找相应的.pc
文件以获取与所有编译/链接标志相关的确切信息.我的系统(Ubuntu)维护一个包含所有必需.pc
文件位置的列表/var/lib/dpkg/info/pkg-config.list
.但我$PKG_CONFIG_PATH
在Ubuntu 12.04上没有任何环境变量(我不知道为什么).
我新安装的Qt5有一组.pc
文件/opt/Qt5.2.1/5.2.1/gcc_64/lib/pkgconfig/
.
那么,如何让pkg-config识别新安装的Qt 5.2.1中的开发库?
我知道这可以通过-dev
从Ubuntu repo 安装Qt4 软件包来解决,但我不想那样做.我需要香草Qt 5.2.1用于其他目的.
注意:
/opt/Qt5.2.1/5.2.1/gcc_64/lib/pkgconfig/
pkg-config.list 的路径,但是pkg-config没有相应地更新编译标志./opt/Qt5.2.1/5.2.1/gcc_64/lib/pkgconfig/
)$PKG_CONFIG_PATH
也不起作用.这是八度配置脚本输出.