Bash script: how to replace text from user input - linux

I need to make an interactive bash script where the user will input a word, for example 'xyz' and it will replace a word in a text file.
For example, I have a script called example.sh. When i run it, it should ask:
What is your name?:
and the user will input 'xyz'.
The script should take that input and replace 'username' inside a text file called userlist.txt to 'xyz'
to be more clear, I need user input to run
sed -i -e s/username/userinput/g userlist.txt

You can use the read builtin to read user input, and you can then use sed(1) to do the text replacement:
if read -p "What is your name? " name; then
sed -i~ -e "s/username/${name}/g" userlist.txt
else
# Error
fi

#!/bin/bash
echo "What is your name?"
read name
sed -i -e s/username/"$name"/g userlist.txt

Related

Creating Users from a text file using Bash

I've been looking at some of the other answers for this question but it hasn't solved my issue.
Basically, I need to create a script that reads from a text file, that is given by a user input, and creates users from the contents of the file. I've managed to get it to create the first user but it doesn't seem to be creating the other users in the file.
My text document is literally:
user1
user2
user3
And heres the code I have:
echo -n "Enter name of text file "; read text
while read USER;
do
USERNAME=$(cut -d$'\n' -f $text)
echo $USERNAME
useradd -m "${USERNAME}"
done < $text
It seems to only be reading the very first entry in the text file but I thought using the \n would mean it cut the other lines and use them next? I tried using the 'cat' command instead but wasn't having much luck with it and this is the furthest I've managed to get but I was hoping someone would help me find where I've gone wrong.
Thanks :)
First, you should start your script with
#!/usr/bin/env bash
to ensure that it is interpreted as bash.
Second, the value you need is already available in the USER variable. There is no need to use cut.
#!/usr/bin/env bash
echo -n "Enter name of text file: "; read FILENAME
while read USER; do
echo "$USER"
useradd -m "${USER}"
done < "${FILENAME}"
You can use awk to print and pipe to shell
$ awk '{print "useradd -m "$1}' <file> | sh

Shell script that inserts text to last line of a file based on user input

#!/bin/sh
...
read var
#user enters: it doesn't work
...
echo 'Some text and $var' > myfile.txt
Expected output:
cat myfile.txt
#Some text and it doesn't work
Actual output:
cat myfile.txt
#Some text and $var
So how to echo the content into the file with the value of the $var variable?
use double quote instead of simple quote to make variable remplacement available
you can also remove your quotes to make it work but it's not recommanded
http://wiki.bash-hackers.org/syntax/quoting
if you want to insert $var to last line(=append the content $var to file), please use >>
should be:
echo "Some text and $var" >> myfile.txt
the > is to override the content, whereas >> is to append the content to a file

Write variables to a file which contains the variabe name and value

I want to be able to read and write to a file with a bash script. I am able to read the variables from the file but I am not sure how to write to it.
The file data.txt contains the variable names and their values like this:
var1=2
var2=3
The shell script looks like this:
#!bin/bash
echo "Read"
var1=$(grep -Po "(?<=^var1=).*" data.txt)
echo "Do Something"
var2=${var1}
echo "Write"
# var2 should be saved in data.txt like this: var2=<Here is the Value>, e.g. var2=2
I can read the specific values of the variables with the grep command, which I got from this answer. But I am not sure how to implement the functionality to be able to save the values of the variables at the correct position (see the last comment in the bash script).
The variable names are unique in data.txt.
Any ideas how to achieve this?
I found a solution with the sed command:
echo "Write"
sed -i -e "s/$(grep -Po '(?<=^var2=).*' data.txt)/${var2}/g" data.txt
You can use sed to do the job.
!/bin/bash
echo "Read"
var1=$(grep -Po "(?<=^var1=).*" data.txt)
echo "Do Something"
var2=${var1}
echo "Write"
sed -i "/var2/ s/.*/var2=${var2}/" data.txt # replace the line contains 'var2' with var2=2
I assume you use GNU sed, for BSD sed, you need to feed an extra suffix to -i.

Replacing a string with a given text using Linux shell script

I have a file named testfile.It contains some information like
methun:x:500:500:comment:/home/methun:bin/bash
salahuddin:x:501:500:comment:/home/methun:bin/bash
Now implemented a following shell program:
echo "Enter a Name:"
read username
users='cat /mypractice/myfiles/testfile | awk -F ':' '{print $1}''
for user in $users
do
if [ "$user" == "$username" ]; then
echo "Name found and Enter a new name to change."
read newUsername
#need code to change text on my file --->testfile
fi
done
Now suppose I need to change methun to Moin. Comment to newcomment. I used
sed -i 's/"$user"/"$newuser"/g' /mypractice/myfiles/testfile
But it not working here. I test with it in my testfile singly it change and replace all.But i need to change only that position i want .
I also tried with usermod but it will not works here..
Can anyone give me the solution or correct my code...Thanks
You are using g flag in your sed command which means global substitution (will change all occurrences). Also, the variables although quoted are wrapped inside single quotes and hence are not interpolated.
Try this:
sed -i "s/^$user/$newuser/" /mypractice/myfiles/testfile
I have placed a ^ anchor in the substitution part which means only substitute if the word is at the beginning of the line. This protects you from making a change if the name of the user is not at the start but somewhere in the middle.

Shell script for finding a file name using a string input

Hi I'm starting to learn how make and use shell scripts, one the shell scripts I want to make is a shell script that takes in a string input from a user that is asking for a file name and it reports whether the file is present or not. I unsure of what to do.
Here's what I've written:
#!/bin/bash
read string1
echo${#string1} ; grep
I don't know what to do after typing in grep.
Please help.
grep looks for a string that occurs in the file's contents. If you just want to test whether a file with a given name exists, grep is not necessary.
#!/bin/bash
read string1
if test -e ${string1}; then
echo The file ${string1} exists
fi
will do.
[copied from comment to answer as suggested by #glenn jackman]
Use the file test operator instead of grepping for the name:
#!/bin/bash
printf "Enter a filename: "
read string1
if [[ -f "$string1" ]]; then
echo "The file '$string1' exists."
else
echo "The file '$string1' does not exist!"
fi
Don't forget to quote your variables so that names with spaces and odd characters are parsed correctly.
$ bash test.sh
Enter a filename: hash.pl
The file 'hash.pl' exists.
$ bash test.sh
Enter a filename: odd file.txt
The file 'odd file.txt' exists.
$ bash test.sh
Enter a filename: somefile
The file 'somefile' does not exist!

Resources