This question already has answers here:
How to print a bash array on the same line
(6 answers)
Closed 3 years ago.
I am running this script in bash to add a list of user to an array and then write it to a file. This is the script:
echo "Insert the first user of the list"
read -r user_name
user_list=()
echo "User $user_name inserted!"
user_list+=($user_name)
echo "Do you want to insert another user?(yes or no)"
read -r answ
while [ $answ == "yes" ]; do
echo "Enter a new user to insert"
read -r new_user
user_list+=($new_user)
echo "Users list contains:"
echo "$user_list"
echo "Do you want to add another user?(yes or no)"
read -r answ
done
echo "$user_list" > user_list.txt;
Everything works fine except that the array only contains the first element.I don't understand why the $new_user are not added to the array.
Last line could be
printf "%s\n" "${user_list[#]}" >user_list.txt
Related
This question already has answers here:
How do I read user input into a variable in Bash?
(6 answers)
Closed 4 years ago.
Using Bash, I tried to read input from the user like this:
#!/bin/bash
function read_from_user {
cat | echo
}
echo 'Do you want to create the folder "new.folder" ?'
var=`read_from_user`
if [[ ${var} == yes ]]; then
mkdir new.folder
fi
echo 'var is: ${var}'
But it's not working, var is empty, even though the user input is not empty.
How can I read user input from my Bash script?
You should use read:
#!/bin/bash
echo 'Do you want to create the folder "new.folder" ?'
read var
if [[ "$var" == "yes" ]]; then
mkdir new.folder
fi
echo "var is: $var"
If you really want to use cat, you could do this, as cat without any argument reads from stdin:
#!/bin/bash
echo 'Do you want to create the folder "new.folder" ?'
var=$(cat)
if [[ "$var" == "yes" ]]; then
mkdir new.folder
fi
echo "var is: $var"
However, you would have to use CTRL + D to send on EOF to your program after typing your input. Otherwise cat will wait for more. read is a cleaner way to ask a user for input.
Your code is almost correct, you just need to change your function to read user input into a variable call var. Also you need to change your code in two place. One in the function and one at the place where you are calling your function. I have modified your code like below:-
#!/bin/bash
function read_from_user {
read -r var #here you are reading user input to variable `var`
}
echo 'Do you want to create the folder "new.folder" ?'
#var=`read_from_user`
read_from_user #here you are calling the function to read user input
if [[ ${var} == yes ]]; then
mkdir new.folder
fi
echo "var is: ${var}"
Also always compare two string like if [[ "${var}" == "yes" ]]; but still your above if condition will also work perfectly.
Also best way to do it like below where you don't need a separate echo statement and input will be read at the end out output message:-
#!/bin/bash
function read_from_user {
read -p 'Do you want to create the folder "new.folder" ? ' var
}
#echo 'Do you want to create the folder "new.folder" ?'
#var=`read_from_user`
read_from_user
if [[ "${var}" == "yes" ]]; then
mkdir new.folder
fi
echo "var is: ${var}"
This question already has answers here:
Escape dollar sign in string by shell script
(6 answers)
Closed 5 years ago.
How I can print $$ as text. For example script:
PASS="Pa$$w0Rd"
echo $PASS
This structure show me Pa12515w0Rd where 12515 it's current PID but I need to see Pa$$w0Rd.
Thanks in advance.
For more detail:
echo "Enter password:" #pass will be - Pa$$w0Rd
read PASS
echo $PASS
You have to add an escape char
like that :
$var = "PA\$\$ss";
echo $var;
Displaying Pa$$w0rd with echo can be achieved with:
echo Pa\$\$w0rd
This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 6 years ago.
My code below:
echo "====================================="
echo " Test Programme "
echo "====================================="
echo
read -p "Enter Name: " name
if [$name -eq ""]; then
sleep 1
echo "Oh Great! You haven't entered name."
exit
fi
read -p "Enter age: " age
According to that code,I expected "Oh Great! You haven't entered name." to show up when user skips entering the name which WORKS WELL
But, when you enter a proper string for name, it gives this message/ error:
./cool_ham.sh: line 13: [Franco: command not found
I want to know the reason for that.
I have even tried "$name" = "" after #Jack suggested, but still din't work .
Put a space between the square braces of your if condition and its contents.
if [ "$name" = "" ]; then
Additionally note that I use = over -eq to compare strings. -eq is used to compare integer values, while = will compare strings, which can be unintuitive coming from other languages.
I also quoted $name to prevent globbing and word splitting.
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 7 years ago.
This one is probably pretty simple. I've got a simple while loop which asks the user to input data
while [ $i -le $numMasterNodes ]; do
echo "Enter hostname #$i: "
read masterHost$i
((i+=1))
done
I'm trying to get the value of $masterHost$i in my loop, for example
while [ $i -le $numMasterNodes ]; do
echo "Enter hostname #$i: "
read masterHost$i
echo $masterHost$i
((i+=1))
done
However, it just returns 1 2 3, etc... How can I get the value of $masterHost$i so I can add it to an array?
Thanks!
You probably would be happier with an array. See http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html
Note if you already know about arrays, I'm not sure why you're not just using one directly in your loop.
Here's your example recoded to do that:
#!/bin/bash -
i=1
numMasterNodes=3
declare -a masterHost
while [ $i -le $numMasterNodes ]; do
echo "Enter hostname #$i: "
read masterHost[$i]
echo ECHO ${masterHost[$i]}
((i+=1))
done
I am trying to make my script to repeat till the user leaves the block question empty. I just got the loop to run, but I can not find a way to make it possible to stop it when block is empty.
I hope some one can help me!!
#!/bin/tcsh -f
#
set word="start"
until ($word !=""); do
#First ask for Compound and Block Name.
echo -n "please enter block name: "
read block
echo -n "please enter compound name: "
read compound
#Now coping template with new name
#
cp Template $block
#
for line in `cat $block`;do
echo $line | sed -e "s/test1/${block}/g" -e "s/test2/${compound}/g" >>./tmp124.txt
done
mv ./tmp124.txt $block
done
Do you want to use bash or csh? You are using bash syntax but tagged your question csh and call tcsh in the first line of your code.
To answer your question, here are examples of how to iterate on standard input until some input is empty:
For tcsh:
#!/bin/tcsh
while ( 1 )
set word = "$<"
if ( "$word" == "" ) then
break
endif
# rest of code...
end
For bash:
#!/bin/bash
while read word; do
if [ -z $word ]; then
break
fi
# rest of code...
done
Use "Until do" loop,
Eg :
For session variable i am assigning default value, Then entering loop. User can pass any value on each prompt when the value is empty, Loop will Terminate and exit the script.
session="Mysession"
until [$session -eq $null]
do
echo $session
echo "Leave Blank to Terminate session"
read -p "Enter session name : " session
done
echo "Exiting.."