Interactive command in command substitution bash script - bash

I'm writing a shell script that asks for a value and depending on the valued entered returns a value. Imagine that it's called "reve" and contains something like
read fname
rev << EOF
$fname
EOF
Then if I have a file that is called "file.txt" and I do
vi `./reve`
Then waits for user input. If I type "txt.elif" vi opens "file.txt". Correct until now. But the problem is when the script is something like the following
echo "Enter inverted file name"
read fname
rev << EOF
$fname
EOF
Then it tries to open a file called "Enter inverted file name".
Is it possible to ask for the value with the text and after that use the returned value only?
Thanks in advance.

If you really need it to be interactive, then you could print the message on stderr instead.
echo "Enter inverted file name" > /dev/stderr
read fname
rev << EOF
$fname
EOF

Unless you're using a really old shell, read should have a -p option to supply a prompt. It automatically sends the prompt to stderr instead of stdout, and skips the linefeed (so the response is on the same line as the prompt):
read -p "Enter inverted file name: " fname
rev << EOF
$fname
EOF

Force writing to, and reading from, the terminal using /dev/tty.
In your example this will be:
echo -e "Enter inverted file name: \c" > /dev/tty
read fname < /dev/tty
rev << EOF
$fname
EOF
I used echo -e ... \c so no newline is printed and your input is entered on the same line.

Related

PS1 Command - Bash

I am trying to get the PS1 command working with bash.
What I am trying to do is capture the user's data and input it to change the field on the command line.
I know the command is PS1="MadMike", but what I am trying to do is the captured data from the read line and insert it into the command and then run the command
#PS1 Method
1)
echo -e "\n"
#Sub Menu for Method
echo "============================================"
echo "What would you like your command line to say"
echo "============================================"
echo -e "\n"
#Waiting for user input
echo "Type below"
#Capturing User input
read input
#Setting PS1 input
PS1="input:"
You've missed the $ sign to get the content of the input variable. It should be:
PS1="$input:"

Appending Data in Bash Shell Script

In a bash shell script how does one append read data to a csv file?
For example:
#!/bin/bash
echo "Enter name:"
read name
exit 0
If I want $name put into a .csv file from this script, how can this be done?
Maybe I'm missing something, but it sounds like all you need is
echo ${name} >> file.csv
The echo statement will send the value of name to standard output, and the >> will append the standard output to the end of file.csv.

Script to read contents from the user

How to write a shell script which creates a file
echo "Enter the file you want to create"
read a
touch $a
And adds content to the created file 'a'
echo "Enter the contents to your $a file:"
cat > $a << 'EOF'
EOF
The above one is not the right way, what I am doing wrong here ?
You were almost there. This will do what you ask:
echo "Enter the file you want to create"
read a
echo "Enter the contents to your $a file (press ctrl-D when done):"
cat >"$a"
Discussion
touch $a
It is not necessary to touch the file. The cat statement will create it regardless.
Also, because file names can include whitespace, it is best to put inside double-quotes any shell variable containing a file name. This prevents word splitting.
cat > $a << 'EOF'
EOF
The construct << 'EOF' tells the shell to read from a here document. This is what you use if you have text in your shell script that you want to use as input. You don't. You want instead to read input from the user. Thus the above two lines can be simply replaced with cat >"$a" .
You can ask then proceed with a simple echo:
echo "Enter the file you want to create"
read file
echo "Enter the contents to your $a file:"
read content
echo "$content" > "$file"

How do I append text to a file?

What is the easiest way to append text to a file in Linux?
I had a look at this question, but the accepted answer uses an additional program (sed) I'm sure there should be an easier way with echo or similar.
How about:
echo "hello" >> <filename>
Using the >> operator will append data at the end of the file, while using the > will overwrite the contents of the file if already existing.
You could also use printf in the same way:
printf "hello" >> <filename>
Note that it can be dangerous to use the above. For instance if you already have a file and you need to append data to the end of the file and you forget to add the last > all data in the file will be destroyed. You can change this behavior by setting the noclobber variable in your .bashrc:
set -o noclobber
Now when you try to do echo "hello" > file.txt you will get a warning saying cannot overwrite existing file.
To force writing to the file you must now use the special syntax:
echo "hello" >| <filename>
You should also know that by default echo adds a trailing new-line character which can be suppressed by using the -n flag:
echo -n "hello" >> <filename>
References
echo(1) - Linux man page
noclobber variable
I/O Redirection
cat >> filename
This is text, perhaps pasted in from some other source.
Or else entered at the keyboard, doesn't matter.
^D
Essentially, you can dump any text you want into the file. CTRL-D sends an end-of-file signal, which terminates input and returns you to the shell.
Other possible way is:
echo "text" | tee -a filename >/dev/null
The -a will append at the end of the file.
If needing sudo, use:
echo "text" | sudo tee -a filename >/dev/null
Follow up to accepted answer.
You need something other than CTRL-D to designate the end if using this in a script. Try this instead:
cat << EOF >> filename
This is text entered via the keyboard or via a script.
EOF
This will append text to the stated file (not including "EOF").
It utilizes a here document (or heredoc).
However if you need sudo to append to the stated file, you will run into trouble utilizing a heredoc due to I/O redirection if you're typing directly on the command line.
This variation will work when you are typing directly on the command line:
sudo sh -c 'cat << EOF >> filename
This is text entered via the keyboard.
EOF'
Or you can use tee instead to avoid the command line sudo issue seen when using the heredoc with cat:
tee -a filename << EOF
This is text entered via the keyboard or via a script.
EOF

sentence as user input - multiple times from terminal - bash script

I am trying to send lines from terminal to a text file multiple times using the following script. After writing the first line and its description in 2nd line, the script asks user whether he wants to enter another line or not. If yes, then user writes the 3rd line, 4th line and so on...
my problem is that after 2nd line, i.e. starting from 3rd line, the script writes only the first word, not the full sentence. How do I solve this ?
function ml() {
echo $# >> $HOME/path/to/file/filename
echo -n "Enter description and press [ENTER]: "
read description
echo -e '\n[\t]' $description >> $HOME/path/to/file/myfile
while true
do
read -p "Add another line?y?n" -n 1 -r
echo -e "\n"
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -n "Enter another line and press [ENTER]: "
read -a meaning
echo -e "[\t]" $meaning >> $HOME/path/to/file/myfile
else
break
fi
done
echo % >> $HOME/path/to/file/myfile
}
also I would like to have another modification in the code
read -p "Add another line?y?n" -n 1 -r
instead of asking y/n input, can it be done that after inserting the first two line, every ENTER will ask for another line input and pressing ESCAPE will terminate the script?
This is because in your second call to read, you are using the -a argument which does:
The words are assigned to sequential indices of the array variable aname, starting at 0. aname is unset before any new values are assigned. Other name arguments are ignored.
That appears to be not what you want.

Resources