How do I read a value from user input into a variable - linux

In ksh, how do I prompt a user to enter a value, and load that value into a variable within the script?
command line
echo Please enter your name:
within the script
$myName = ?

You want read:
echo Please enter your name:
read name
echo $name
See read(1) for more.

You can do it in a single line, like so:
read -p "Please enter your name:" myName
To use variable in script
echo "The name you inputed is: $myName"
echo $myName

ksh allows you to specify a prompt as part of the read command using this syntax:
read myName?"Please provide your name: "

Related

execute multiple commands assigned to a single variable - bash

I would like to ask the user to enter a command that will be executed, but I don't understand why I can't assign two commands to the same variable.
Here is the code:
user#localhost:~# read -ep "command: " cmd ; "$cmd"
and the result:
command: id ; date
-bash: id ; date : command not found
but if I type a single command, it works.
Thanks for your help

Pass in bash terminal variables to a bash script

If I am in a Linux terminal and I start setting variables such as export AGE=45.
Then I have a script to read user data from terminal variables and process it, is this possible to do?
IE:
user#linux$ export AGE=45
user#linux$ ./age.sh
#script asks for input
read -p "what is your age?" scriptAGE
#user inputs variable set in terminal
$AGE
#echo output
echo "your age is: " $scriptAGE"
#should say your age is: 45
There is no such thing as a terminal variable. read just assigns a string to your variable scriptAGE.
If this string contains some $NAME you want to expand, you could apply eval to it, but this is of course extremely dangerous because of possible code injection.
A safer way to do this is using envsubst, but this requires that the variables to be substituted must be environment variables. In your case, AGE is in the environment, so this condition is met.
In your case, you would have to do therefore a
envsubst <<<"$scriptAGE"
which would print on stdout the content of scriptAGE with all environment variables in it substituted.
Variables are not expanded in input, only in the script itself.
You could use eval to force it to process the variable value as shell syntax.
eval "echo 'your age is:' $scriptAGE"
But this will also process other shell syntax. If they enter $AGE; rm * it will say their age is 45 and then delete all their files.
you could just do
age=$1
echo "Your age is $1"
where $1, $2, $3, .., $N are the passed arguments by order
And then run your script
bash script sh Noureldin
For more Info read this:
passing names args

Xterm: How can I direct input from one terminal tab to another?

As I am a beginner coder, I apologize in advance for improper terminology.
This is the main script which calls the script ping.sh in a new tab.
#!/bin/bash
echo "The script is running!"
rm ping.txt
echo "Enter your desired IP address:"
read ADDRESS
osascript -e 'tell application "System Events" to tell application "Terminal"
do script "./ping.sh"
end tell'
echo "The script has ended!"
exit 0;
So, as I said the script ping.sh is called now. It goes like this.
#!/bin/bash
echo "Welcome to the new tab!"
ping -c 3 $ADDRESS > ping.txt
exit 0
The problem I have is that the read input from the first tab isn't recognizable in the second tab. Is there a way to solve this? I am probably missing a linking constructor or something like that. Please help!
I have no idea what osascript is, or how it works, but it might be helpful to know that shell scripts can access command line arguments with the special variables $1, $2, $3, etc.
This means you can rewrite your ping.sh script like so:
#!/bin/bash
echo "Welcome to the new tab!"
ping -c 3 "$1" > ping.txt
exit 0
And then call it like so:
#!/bin/bash
echo "Enter your desired IP address:"
read ADDRESS
./ping.sh "$ADDRESS"
Otherwise, to make sure subsequent commands have access to the same environment variables, you have to export them. From help export:
export: export [-fn] [name[=value] ...] or export -p
Set export attribute for shell variables.
Marks each NAME for automatic export to the environment of subsequently
executed commands. If VALUE is supplied, assign VALUE before exporting.
To make your original ping.sh work you could do the following:
#!/bin/bash
echo "Enter your desired IP address:"
read ADDRESS
export ADDRESS
./ping.sh

How to compare user input with array and execute command?

I am trying to write a script to login into different systems by providing system name as input and making it variable by read option. However when i try to compare it with defined Array it's throwing me error and stating command not found.
Succeeded in making use input as variable but not able to compare it properly with defined array.
Below is the code i have written.
#!/bin/bash
cluster=("namico1c.mylabserver.com","namico2c.mylabserver.com")
echo "Please enter a Cluster Name to login: "
read clname
for item in ${cluster[#]};do
echo ${item};
if ["${clname}"="${item}"]; then
ssh test#$clname
else
echo "Cluster is not correct"
fi
done
[test#namico3c ~]$ ./test.sh
Please enter a Cluster Name to login:
namico1c.mylabserver.com
namico1c.mylabserver.com,namico2c.mylabserver.com
./test.sh: line 7: [namico1c.mylabserver.com=namico1c.mylabserver.com,namico2c.mylabserver.com]: command not found
Cluster is not correct
alternative:
#!/bin/bash
cluster=("namico1c.mylabserver.com" "namico2c.mylabserver.com")
select clname in "${cluster[#]}"; do
ssh test#$clname
break
done

Can't populate variable from command in Bash

I'm new to bash scripting, and I'm working on a script where the user enters a username and gets a list of the associated information from /etc/passwd. Unfortunately, I seem to be having trouble populating a variable from a command. The error message I'm getting suggests the if statement isn't being entered into, but I'm not sure why.
The script currently looks like this:
#!/bin/bash
#readifs
FILE=/etc/passwd
read -p "Enter a username > " user_name
file_info=$(grep "^$user_name:" $FILE)
if [ -n "$file_info" ]; then
IFS=":" read user pw uid gid name home shell <<< "$file_info"
echo "User = '$user'"
echo "UID = '$UID'"
echo "GID = '$GID'"
echo "Full Name = '$name'"
echo "Shell = '$shell'"
else
echo "No such user '$user_name'" > &2
exit 1
fi
When I run it, using a valid username, I get the following two lines:
readifs.sh: line 20: syntax error near unexpected token `&'
readifs.sh: line 20: ` echo "No such user '$user_name'" > &2'
I'm pretty sure I'm missing something obvious, or doing something bash doesn't allow but I'm too new to catch. Can anyone point out and correct the error in my script?
Thank you to Charles Duffy for all the great feedback on not just this script, but bash scripting and Stack Overflow in general.
I was able to fix the script as I wanted. I removed the ^ and : from the file_info line, which was stopping the grep command from finding the line I wanted. I also renamed $UID and $GID to use lower case letters, and removed the space in "> &2".
Thank you again for your assistance.

Resources