0

I want to get the value and line number from the .bash_profile so that I can either delete it or update it if it doesn't match.

Current example from the file is.

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

export PS1="`whoami`@\h:\${PWD}
\t # "

So I want to know what the value is matching "export PS1...." and replace it. My problem is it is wraps and I'm not sure what regex to use to get the full value and which lines it's on. Any help is appreciated.

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
debow
  • 73
  • 7
  • not sure about the bash but maybe `".*"` ? (input as single line, "dot matches newline") – Scott Weaver Jan 22 '18 at 20:21
  • `line = File.read('/root/.bash_profile') match = line.match /^export PS1.*"/ puts "**** #{match}"` will only output `export PS1="` not the whole string – debow Jan 22 '18 at 21:57
  • I don't exactly understand what result you're looking for. If `PS1` is not present, what should the code do? If `PS1` spans one line, what should the code do? If `PS1` spans multiple lines, what should the code do? Do you really need to search the `.bash_profile` at first, or would it be more helpful to just check if `ENV["PS1"]` is a multi-line string?? – Tom Lord Jan 23 '18 at 09:41
  • For example, your code could be: `if ENV["PS1"].include?("\n")` then do [something like this](https://stackoverflow.com/a/5761614/1954610), searching for `PS1`. – Tom Lord Jan 23 '18 at 09:43
  • Sorry about that the question wasn't that clear. Basically I know there will be an existing PS1 set. I need to replace it with `export PS1='\e[1;31m\u@\h \@\n\w # \e[m'` if it doesn't match the new string. So somehow need to compare the existing string to the expected string and replace just that line. Just having trouble getting any search to work since it wraps. There could also be some nodes that don't have an existing PS1 that wraps multiple lines. – debow Jan 23 '18 at 14:20

0 Answers0