bash file Linux Ubuntu [closed] - linux

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I don't know why my bash file in Linux Ubuntu doesn't work.
#!/bin/bash`
echo -n "Write user name : "
read NAME
cd
if grep -c "$NAME" /etc/passwd -eq 0; then
echo "$NAME doesn't esist"
else
echo "$NAME already esist"
fi`
It gives the error:
$ bash ./name.sh
Write user name : root
grep: root: No such file or directory
/etc/passwd:3
grep: 0: No such file or directory

The code is not executing the grep in a sub-shell, and the if-condition needs to be wrapped in square brackets.
#!/bin/bash
echo -n "Write user name : "
read NAME
cd
if [ `grep -c "$NAME" /etc/passwd` -eq 0 ]; then
echo "$NAME doesn't esist"
else
echo "$NAME" already esist
fi
Output:
$ bash ./name.sh
Write user name : root
root already esist
$ bash ./name.sh
Write user name : ddd
ddd doesn't esist

You have some strange backquotes in your script which you should remove
#!/bin/bash
read -p "Write user name : " name
if grep -q "$name" /etc/passwd; then
echo "$name already exists"
else
echo "$name doesn't exist"
fi
as you can see
read can show a prompt
variables should be lowercase
the exit value of grep determines the case and no need for parenthesis or brackets
also notice that is looking for name everywhere, not just user names
because you included the shebang you can
$ chmod +x script
$ ./script

Related

Is there a way to pipe user input within a bash script into the cat command and have it save at a destination of my choosing as a text file

Something similar to this maybe:
#! /bin/bash
echo What is your name?
read name | cat > ~/Documents/file.txt
if [[ $name==Bob ]]
echo something
fi
The command creates an empty file on manjaro mint.
Your problem is that read doesn't create any output.
And you have a syntax error further down the line, it would be a good idea to put your script(s) through shellcheck.
#! /bin/bash
echo What is your name?
read -r name
echo "$name" > ~/Documents/file.txt
if [[ "$name" == "Bob" ]]; then
echo something
fi

I am trying to write a shell script to read username from a file, but it is not working. I am posting script I am writing and output [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
#!/bin/bash
if [ -f "$1" ]
then
for users in 'cat $1'
do
useradd $users
done
else
echo "input is not a file"
fi
You just have to get the input for the do loop right:
#!/bin/bash
if [ -f "$1" ]
then
for user in $(cat "$1")
do
useradd "$user"
done
else
echo "input is not a file"
fi
Remarks: this works for reading out a file word-by-word and I tried to keep your structure.
For reading out files line by line this is an elegant way: https://stackoverflow.com/a/4642213/2819581

Converting content of files to uppercase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
So I have a shell script called "concat" that currently takes command line arguments and prints the contents of files named on the command line. I need to now create a script called "concatconvert" that calls the "concat" script, takes the contents of files and converts them.
The following is the code of my script "concat":
#!/bin/bash
if [ $# -eq 0 ]; then
printf "Usage: concat FILE ... \nDescription: Concatenates FILE(s)
to standard output separating them with divider -----.\n" >&2
exit 1
fi
for var in "$#"
do
if [[ ! -e "$var" ]]; then
printf "One or more files does not exist\n" >$2
exit 1
fi
done
for var in "$#"
do
if [ -f "$var" ]; then
cat $var
printf -- "-----\n"
fi
done
exit 0
I am going to be calling "concat" using
#!/bin/bash
./concat
in the concatconvert script.
Concatconvert is going to take arguments "-u" and "-l"
Ultimately the script would be executed as:
./concatconvert -u test1.txt test2.txt
-u converts contents of files to uppercase.
For example, "This is a test" becomes "THIS IS A TEST".
-l converts contents of files to lowercase.
For example, "This is a test" becomes "this is a test".
Only one option can be provided at a time.
I am not too sure where to begin on this. I appreciate any help.
You should use tr command as mentioned by #jenesaisquoi.
The tr command in UNIX is a command-line utility for translating or
deleting characters.
To use it to change everything to lower case command would be :
echo "This is Test" | tr [:upper:] [:lower:]
this is test
To use it to change everything to upper case command would be :
echo "This is Test" | tr [:lower:] [:upper:]
THIS IS TEST
To use it for a file use below command :
tr '[:upper:]' '[:lower:]' < filename

How can I modify script to print information about all files in directory [duplicate]

This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
How can I finish the script?
Linux version 3.10.0-957.21.3.el7.x86_64 .(Red Hat 4.8.5-36)
#!/bin/bash
echo "Enter the file name"
read x
if [ -f $x ]
then
echo "This is a regular file"
else
echo "This is a directory"
fi
Need modify script which will output all files and directory in /etc/ directory and indicate which one is what (e.g.:
dir1 is a directory
fileA is a file
dir2 is a directory
2nd part of the job I did. need help with
Use a for loop instead of getting the filenames from the user.
#!/bin/bash
for file in /etc/*; do
if [ -f "$file" ]
then
echo "$file is a regular file"
elif [ -d "$file" ]
then
echo "$file is a directory"
else
echo "$file is something else"
fi
done
Don't forget to quote variables, in case the value contains a space. And there are other possibilities than just files and directories.

Show file contents after searching word Done [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to display the file contents after searching for a word. If the word is found, display the file.
My code is below:
GNU nano 2.2.6 File: work
#!/bin/bash
while read -p "Welcome what would you like to do (S) to search or (Q) to quit " option
do
case $option in
"S") echo "What is the name of the file you would like to search for?"
read file
echo "What word would you like to find in the file?"
read word
grep -q $word $file
if [ $? -eq 0 ]; then
echo "$word found in $file"
cat $file
else
echo "$word NOT found in $file"
fi
;;
"Q") echo "Goodbye!"
exit ;;
*) echo "invalid option" ;;
esac
done
Replace
echo $file
with
cat $file
I believe you are looking for command cat $file. Stick it inside of your if block.
I need to load up what a file says with out loading up the file.
There is no way to access the contents of the file without accessing the file.
grep -l word file | xargs -r cat
shows file content if word is found. This also shows name of file
grep -l word file | xargs -r -i bash -c "echo {}:; cat {}"

Resources