Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
[已解决]请教 shell script 变量中空格的问题
Experiences and Insight
1360
views ·
4
replies ·
To
floor
Go
admin
deepin
2012-08-10 16:52
Author
那个农历软件 lunar 的参数是要带空格的,比如:
lunar 2012 08 10 09:00
Copy the Code
而每次这么输一遍太麻烦,试过
lunar $date
Copy the Code
后面加参数是不行的。
于是写了这个小脚本:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Yoftd=$(date +%Y) #Year of today
moftd=$(date +%m)
doftd=$(date +%d)
Hoftd=$(date +%H:%M)
thedate=("$Yoftd $moftd $doftd $Hoftd")
echo -e $thedate #test var
lunartd=lunar $thedate
Copy the Code
变量 thedate 中已经有空格,但是 lunar 在读取的时候只读了第一个空格前的那个 2012,无法执行。
在 cal 中用这个方法加入参数也出现同样问题,而直接在终端输
cal 8 2012
Copy the Code
也没问题。
请问该如何解决空格的问题?
或者有更好的写参数方法?求教。
Reply
Like 0
Favorite
View the author
All Replies
cxbii
deepin
2012-08-11 03:32
#1
不懂。。。。
Reply
Like 0
View the author
admin
deepin
2012-08-17 22:47
#2
我自己顶上去
Reply
Like 0
View the author
admin
deepin
2012-09-06 05:37
#3
shell 解决方法:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
lunar -h --utf8 $(date +"%Y %m %d %H:%M")
Copy the Code
Reply
Like 0
View the author
admin
deepin
2012-09-06 05:38
#4
Perl 解决方法:
#!/usr/bin/perl
use 5.010;
#use strict;
$date=`date +"%Y %m %d %H:%M"`;
say `lunar -h --utf8 $date`
Copy the Code
Reply
Like 0
View the author
Please
sign
in first
New Thread
Rankings
7 days
30 days
90 days
1
xclimbing
126 posts
2
Oli
109 posts
3
聪明蛋
59 posts
4
麻烦告诉我小浣熊哪里有
55 posts
5
柠萌~
54 posts
6
新手来啦~
53 posts
Popular Events
More
于是写了这个小脚本:
在 cal 中用这个方法加入参数也出现同样问题,而直接在终端输
请问该如何解决空格的问题?
或者有更好的写参数方法?求教。