Adding text to the end of a line in bash with sed - linux

I have variable in my script called:
VAR=/opt/sbin/test
and I want to append a text for example 'text' at the end of the line with a space and then run the line as
/opt/sbin/test text

I think the command you are looking for is:
VAR=/opt/sbin/test
VAR="$VAR text"
BLAH=$(${VAR})
The above will set $VAR to /opt/sbin/test text and then BLAH=$(${VAR}) will execute it.

Related

How do I specify the range for the ed function in a Bourne Script?

I am currently trying to code a function that fixes the spelling of a specific word in a Bash Script. What I have so far is:
ed -s $argument <<END
s/Old word/New word/g
w $argument
q
END
However this obviously just fixes the last line of the file that I am feeding it. My question is how do I change it from END so that it goes through the entire file instead of just the last line?
The range is specified before the command. In your case, you can use , to apply it to the whole file instead of the current line:
ed -s "$argument" <<END
,s/Old word/New word/g
w
q
END
(The word END is an arbitrary here-document delimiter and unrelated to which lines are modified)

Linux Bash Trying to write checklist progress "bar" of sorts. Replace the same line

Sorry if I am not giving you enough info, this is my first time posting here.
I am trying to make this in a bash script.
Downloading...............
"run bash commands and when they are done, replace the "Downloading..." text with the text bellow in the same line aka space."
Downloading............... DONE!
go to next line and show
Installing................
"run bash commands again and when they are done, replace the "Installing..." text with the text bellow in the same line aka space."
Installing................ DONE!
I hope you get what I mean. Thanks in advance.
I've tried:
#/bin/bash
tput sc # save cursor
printf "Something that I made up for this string"
sleep 1
tput rc;tput el # rc = restore cursor, el = erase to end of line
printf "Another message for testing"
sleep 1
tput rc;tput el
printf "Yet another one"
sleep 1
tput rc;tput el
But it doesn't make new lines, it just uses one line to show all text.
I'm assuming you pulled the tput code from somewhere, and I'm guessing that 'somewhere' also explained that tput is being used to overwrite the same line multiple times (as your script actually does).
From your description it doesn't sound like you need to overwrite any lines so using tput is the wrong solution.
If I understand your description correctly you should be able to do everything you want with some (relatively) simple printf commands, eg:
printf "Downloading .... " # no '\n' in the output so cursor remains at end of current line
# run your bash commands here
printf "DONE!\n" # append to end of current line and then add a new line (\n)
printf "Installing .... " # no '\n' in the output so cursor remains at end of current line
# run more bash commands here
printf "DONE!\n" # append to end of the current line and then add a new line (\n)
Keep in mind that if any of your 'bash commands' generate any output then the cursor will be moved (probably to a new line) thus messing up your output. Net result is that you'll need to make sure your 'bash commands' do not generate any output to stdout/stderr (alternatively, make sure all output - stdout/stderr - is redirected to files).
If your requirement is to have the 'bash commands' send output to the terminal then you may need to go back to using tput ... but that's going to depend on exactly how you want the output to appear.
NOTE: If this (above) does not meet your requirement then please update the question with more details.

VI read line not giving desired output

I have a file called test, I open it using vi as such:
vi test
Now I want to insert a line through a shell command, for simplicity I use a printf:
:r! printf %s hello
However the line that is entered is
tests
i.e. the name of the file with a s appended.
If I enter the same command in terminal directly, it works fine.
What I want to do is ultimately be able to encode a string in base64 and enter it on the same line as where my cursor is in vi, so that I won't have to copy the string in a separate terminal, encode it, and copy it back into vi. How can I do this? What am I doing wrong?
The first stage of processing a command line in vim is expanding it. % is expanded to the name of the current file — test in your case. %s is expanded to tests.
To avoid expanding protect the special character with a backslash:
:r! printf \%s hello

Tmux doesn't show line breaks when calling `shell` to display output from Bash command

Given the following text file "HelloWorld.txt"
Hello World
~~~line break~~~
This is a text file
In .tmux.conf, I config the following setup:
bind F1 shell "cat HelloWorld.txt"
When I use this shortcut, Tmux prints the following:
Hello World
This is a text file
That line break just disappears mysteriously.
How can I preserve line breaks?
I couldn't find a bug report but this seems to be how tmux' run-shell command behaves. The workaround I found is to pipe the output via sed to replace each blank line with a space.
Your example would then become like this:
bind F1 run-shell "cat HelloWorld.txt | sed 's/^$/ /'"

insert line into a new file in linux using shell script

I need to write shell script to create a new file and insert some stuffs into it. The problem is, using sed 'i/blablabla' command, it only works when there is at least one line existed in the file. What is the command to insert into a new file?
Thanks
echo 'new line' >file_name
Also, you can append to the end without using sed using the >> operator:
echo 'additional line' >>file_name
More variants:
cat >>file_being_appended_to <<some_random_string
This is the first line
This is the second line and it has a variable in it; it's right here: $some_variable
some_random_string
cat >>file_being_appended_to <<'some_random_string'
This is the first line
This is the second line and it has a variable reference in it; it's right here: $some_variable,
but this variable reference is not expanded because the beginning delimiter string has single quotes around it.
some_random_string
Other variants:
echo 'This is the first line
This is the second line.' >file
some_variable="some value"
echo "This is the first line
This is the second line and it has a variable in it; it's right here: $some_variable" >file

Resources