linux shell 截取内容,赋值给变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux shell 截取内容,赋值给变量相关的知识,希望对你有一定的参考价值。

询问个linux系统shell脚本问题,
通过grep已经截取到一行数据
a 123
怎么把这2个数据,赋值给变量 var1 var2?

参考技术A 说具体点吧。追问

具体就是:
myvar=8;#初始值,获取到的需要保存到这个变量
#从数据库获取指定配置,
mysql -h$config_host -u$user \
-p$pwd $dbname -e "select id,config from $tablename where id=1" |
grep -e "^[^i]"
#grep 去掉列名行,只留下数据行,例如"1 8096"
需要在myvar中 保存8096这个数值,,,,

参考技术B var1=`grep xxx`本回答被提问者采纳 参考技术C 关注中!

Linux Shell中的命令替换

Shell脚本中最有用的特性之1就是可以从命令输出中提取信息,并将其赋给变量。有两种方法可以将命令输出赋值给变量。

  • 反引号字符(`)

反引号字符位于键盘上ESC键的下面。赋值给变量的情势以下:

test=`date`

# demo
$ test=`date`
$ echo $test
2017年 1月11日 星期3 21时26分04秒 CST

两个反引号字符之间是shell命令,shell命令的履行结果赋值给test,注意=两边不能有空格。

  • $()情势

赋值给变量的情势以下:

test=$(date)

# demo
$ test=$(date)
$ echo $test
2017年 1月11日 星期3 21时26分35秒 CST

http://www.abc100.com/kindeditor/attached/file/20200522/20200522065527_9228.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522065644_2031.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522065758_1952.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522065608_5761.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522070024_5126.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522065721_6757.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522065833_7050.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522065520_7910.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522070018_4199.html
http://www.abc100.com/kindeditor/attached/file/20200522/20200522070011_2001.html
http://www.cere.cc/editor/attached/file/20200522/20200522065718_2698.html
http://www.cere.cc/editor/attached/file/20200522/20200522065524_4573.html
http://www.cere.cc/editor/attached/file/20200522/20200522065830_3323.html
http://www.cere.cc/editor/attached/file/20200522/20200522065516_8792.html
http://www.cere.cc/editor/attached/file/20200522/20200522065641_6917.html
http://www.cere.cc/editor/attached/file/20200522/20200522070015_8948.html
http://www.cere.cc/editor/attached/file/20200522/20200522065754_8011.html
http://www.cere.cc/editor/attached/file/20200522/20200522070008_6605.html
http://www.cere.cc/editor/attached/file/20200522/20200522065605_1917.html
http://www.cere.cc/editor/attached/file/20200522/20200522070022_0042.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522070011541154.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522065576737673.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522065663926392.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522070037553755.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522065769466946.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522065761986198.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522070045404540.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522065685148514.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522070014661466.html
http://www.chenxi120.com/uploadfile/file/20200522/20200522065560976097.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522065629_5633.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522070041_5661.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522070006_0931.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522070020_0331.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522070013_7360.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522065819_1163.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522065522_1955.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522065743_6042.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522065706_0666.html
http://www.chinagreenland.com/kindeditor/attached/file/20200522/20200522065551_9300.html

以上是关于linux shell 截取内容,赋值给变量的主要内容,如果未能解决你的问题,请参考以下文章

在一行上用一个赋值命令给多个变量赋值,各变量之间用啥符号分隔

在Shell脚本中,读取键盘输入的内容并将其赋值给Shell变量的命令是啥?

linux shell 如何把txt文本中每一行提取出来赋值给一变量,再输出这一变量

shell变量用grep怎么不能赋值

Sql中如何给变量赋值

运维常见选择题汇总