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:"
Related
Can someone help me to store multiline output in variable in bash. I have the following code:
# FILES[1] contents:
# age people.csv
# ...data...
FILE="${FILES[1]}"
cmd=$(head -n 1 "$FILE")
cmd="./corona $cmd"
echo "Command to run: ${cmd[*]}"
output=$(eval "$cmd")
echo "$output"
I'm trying to store the output of corona script in output variable. But it doesn't seem to work. The output stucks at
Command to run: ./corona age people.csv
And on the second line I can see only the blinking cursor. But when I press Ctrl+D it suddenly prints all the output from corona script and stops. So, probably, the echo command works just after pressing the shortcut.
Also, I'd like to mention, that variable FILES is an array of filenames. So the FILE variable is a name of the file. It has command arguments to run on the first line and other data starting from the second line.
Here is a sample script I developed to read the output of ls into a variable. You could use the same technique.
#!/bin/bash
my_array=()
while IFS= read -r line; do
my_array+=( "$line" )
done < <( ls )
echo ${#my_array[#]}
printf '%s\n' "${my_array[#]}"
How to make all user input on command line as stdin for a program?
In my case, I want to replace certain words inputed by user. For example, every time user uses the word animal1, I want it received as goldfish. So it would look like this:
$ animal1
goldfish: command not found
I tried the following bash command
while read input
do
sed "s/animal2/zebra/g;s/animal1/goldfish/g" <<< "$input"
done
But it prompts for user input and does not return to bash. I want it to run while using bash command line.
Also, this allowed me to capture output only.
bash | sed 's/animal2/zebra/g;s/animal1/goldfish/g'
But not user input.
If I understand you correctly, sounds like you just need to set up some aliases:
$ alias animal1=goldfish
$ animal1
bash: goldfish: command not found
This allows the shell to be used interactively as usual but will make the substitutions you want.
You can add this alias definition to one of your startup files, commonly ~/.bashrc or ~/.profile, to have them take effect on any new shell that you open.
The solution provided by Tom Fenech is good, however, if you plan to add more features to the command you can use a function like the following:
animal1() {
echo "Welcome to the new user interface!"
goldfish
# other commands
}
and put it in the user ~/.bashrc or ~/.bash_profile
The output will be:
$>animal1
Welcome to the new user interface!
-bash: goldfish: command not found
By using this approach you can, for example, create a custom output message. In the following snippet I take the return vale from the command and process it word by word. Then I remove the -bash: part of the output and reconstruct the message and output it.
animal1() {
echo "Welcome to the new user interface!"
retval=$(goldfish 2>&1)
# Now retval stores the output of the command glodfish (both stdout and stderr)
# we can give it directly to the user
echo "Default return value"
echo "$retval"
echo
# or test the return value to do something
# here I build a custom message by removing the bash part
message=""
read -ra flds <<< "$retval"
for word in "${flds[#]}" #extract substring from the line
do
# remove bash
msg="$(echo "$word" | grep -v bash)"
# append each word to message
[[ msg ]] && message="$message $msg"
done
echo "Custom message"
echo "$message"
echo
}
Now the output would:
Welcome to the new user interface!
Default return value
-bash: goldfish: command not found
Custom message
goldfish: command not found
If you comment the lines that echoes the default return value then you get exactly the output you asked for.
I am trying to make a script that will either take command line input after or if user input is null then an interactive prompt will as for a name to be used to search a text file. I'm using bash and gedit.
I want it to go like this:
./scriptName input
if inupt is null
echo Please enter a name.
I'm just getting started and I cannot figure out how to get this first part to function.
#!/bin/bash
name="$1"
if [[ "$name" == "" ]]; then
read -p "Please enter a name:" name
fi
# continue
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.
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.