将bash变量设置为等于字符串的结果,其中换行符由空格替换

时间:2017-01-10 17:24:17

标签: bash sed newline tr

我有一个等于字符串的变量,它是由换行符分隔的一系列键/值对。

我想用空格替换这些换行符,并设置一个等于结果的新变量

从互联网上的各种答案中我得出以下结论:

#test.txt has the content:
#test=example
#what=s0omething

vars="$(cat ./test.txt)"

formattedVars= $("$vars" | tr '\n' ' ')
echo "$taliskerEnvVars"

问题是,当我尝试设置formattedVars时,它会尝试执行第二行:

script.sh: line 7: test=example
what=s0omething: command not found

我只希望formattedVars等于test=example what=s0omething

我错过了哪些技巧?

2 个答案:

答案 0 :(得分:1)

将您的行更改为:

=

请注意代码中secretsContent的空格,这在赋值语句中是不允许的。

我发现您没有在代码中设置vars,而是设置了private Date FechaInicio, FechaFin; private Calendar myCalendar = Calendar.getInstance(); private Calendar myCalendarEnd = Calendar.getInstance(); private long epochTimeStart, epochTimeEnd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_nuevo_xxxxx_step3); //First Date picjer that have the listener dateStart new DatePickerDialog(xxxxx.this, dateStart, myCalendar .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DATE)).show(); } //Start date listener DatePickerDialog.OnDateSetListener dateStart = new DatePickerDialog.OnDateSetListener(){ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub myCalendar.set(Calendar.YEAR, year); myCalendar.set(Calendar.MONTH, monthOfYear); myCalendar.set(Calendar.DATE, dayOfMonth); //Start de time picker for startdate new TimePickerDialog(xxxxx.this,timeStart,myCalendar.get(Calendar.HOUR),myCalendar.get(Calendar.MINUTE),false).show(); } }; //Time picker dialog for starTime TimePickerDialog.OnTimeSetListener timeStart = new TimePickerDialog.OnTimeSetListener(){ @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { myCalendar.set(Calendar.HOUR, hourOfDay); myCalendar.set(Calendar.MINUTE,minute); epochTimeStart = myCalendar.getTimeInMillis(); //Start de DatePicker for End Date with new DatePickerDialog(xxxxxx.this, dateEnd, myCalendarEnd .get(Calendar.YEAR), myCalendarEnd.get(Calendar.MONTH), myCalendarEnd.get(Calendar.DATE)).show(); } }; //Listener for end date DatePickerDialog.OnDateSetListener dateEnd = new DatePickerDialog.OnDateSetListener(){ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub //Save the date in other calendar instance myCalendarEnd.set(Calendar.YEAR, year); myCalendarEnd.set(Calendar.MONTH, monthOfYear); myCalendarEnd.set(Calendar.DATE, dayOfMonth); //Time picker for end date new TimePickerDialog(xxx.this,timeEnd,myCalendarEnd.get(Calendar.HOUR),myCalendarEnd.get(Calendar.MINUTE),false).show(); } }; //Listener for time of End Date TimePickerDialog.OnTimeSetListener timeEnd = new TimePickerDialog.OnTimeSetListener(){ @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { myCalendarEnd.set(Calendar.HOUR, hourOfDay); myCalendarEnd.set(Calendar.MINUTE,minute); epochTimeEnd = myCalendarEnd.getTimeInMillis(); } }

答案 1 :(得分:1)

如果可能,请使用数组来保存文件的内容:

class Calculadora:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.somar = Button(root,text = 'SOMAR',command = self.soma)
        self.somar.pack(side=TOP)
        self.entry1 = Entry(frame) # 1
        self.entry1.pack(side=BOTTOM) 
        self.entry2 = Entry(frame) # 1
        self.entry2.pack(side=BOTTOM)
        self.labelR = Label(frame,text='[Aqui]') # 2
        self.labelR.pack(side=TOP) # 2

    def soma(self): # 3
        a = int(self.entry1.get())
        b = int(self.entry2.get())
        self.labelR['text'] = a + b

readarray -t vars < ./test.txt   # bash 4

然后你可以用数组做你需要的。您可以使用

创建以空格分隔的列表
# bash 3.x
declare -a vars
while IFS= read -r line; do
  vars+=( "$line" )
done < ./test.txt

,但请考虑是否需要。如果目标是将它们用作预命令修饰符,请使用,例如

formattedVars="${vars[*]}"