从文件中读取到的变量 - bash脚本 [英] Read from file into variable - Bash Script
本文介绍了从文件中读取到的变量 - bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我工作的一个bash脚本备份MySQL的。我需要从文件中读取一系列字符串,并将其传递给我的脚本变量。例如:
这样的事会在文件中(file.txt的)
数据库1表1
表4的Database1
DATABASE2
数据库3表2
我的脚本需要读取该文件,并把这些字符串变量,如:
#!斌/庆典
LIST =database1.table1 | database1.table4 | DATABASE2 | database3.table2
修改我改变主意了,现在我需要这样的输出:
database1.table1 * |。database1.table4 * | DATABASE2 * |。database3.table2 *
解决方案
您可以使用 TR
来代替换行符和空格:
LIST = $(TR的'\\ n''|'< file.txt的)
由于输入文件的最后一行可能本身包含一个换行符,你需要摆脱它:
LIST = $(TR''&LT'。'; file.txt的|贴-sd'|')
I'm working on a bash script to backup MySQL. I need to read from a file a series of strings and pass them to a variable in my script. Example:
Something like this will be in the file (file.txt)
database1 table1
database1 table4
database2
database3 table2
My script needs to read the file and put these strings in a variable like:
#! bin/bash
LIST="database1.table1|database1.table4|database2|database3.table2"
Edit. I changed my mind, now I need this output:
database1.table1.*|database1.table4.*|database2*.*|database3.table2.*
解决方案
You could use tr
to replace the newlines and spaces:
LIST=$(tr ' \n' '.|' < file.txt)
Since the last line of the input file is likely to contain a newline by itself, you'd need to get rid of it:
LIST=$(tr ' ' '.' < file.txt | paste -sd'|')
这篇关于从文件中读取到的变量 - bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文