BASH grep script - linux

I am trying to work on a bash script that checks for a username in the argument of the script and then outputs the relevant lines from the /etc/passwd and /etc/group files (not the /etc/shadow file). Currently, I am utilizing a if then else loop to check the contents of the /etc/* directory and output the relevant information. My intention was to output simple text line if a match user is not found in the two files, thus a null value. However, it is outputting information that is totally incorrect for what I am looking for.As a new user to BASH, and linux is general, I am sure there are some glaring issues right away. However, I am trying to learn.
Any help with the code of my script or a point in the right direction would be greatly appreciated. Thank you.
#! /bin/bash
USERLOOK='grep -h $USERID ~/etc/* | grep :x:'
grep $1 ~/etc/*
if [ -z $1 ]; then
echo "User not found."
else
echo "$USERLOOK"
fi
exit 0

Finds any lines in /etc/passwd or /etc/group that contain the inputted username:
#!/bin/bash
USERLOOK=$(grep -h "$1" /etc/passwd /etc/group)
if [ -z "$1" ] || [ -z "${USERLOOK}" ]; then
echo "User not found."
else
echo "$USERLOOK"
fi

I want to have the script function where I input ./script
user_to_check. If the username is found, I want to output all lines
where it was found... However, if the username was not found, I wanted
to echo that.
It can be as simple as
#!/bin/bash
grep "^${1}:" /etc/passwd /etc/group
[ $? -ne 0 ] && echo "User : ${1} not found"
As the user name appears in the beginning in both /etc/passwd & /etc/group we placed a ^ in grep to match stuff at beginning and by tradition a : appears just after the username.
Run the script as
./script 'username'

Stop, you're thinking about this all wrong.
A UNIX shell is an environment from which to call UNIX tools with a language to sequence those calls, that is all. The general purpose UNIX tool to manipulate text is awk. So if you need to look for text in a file and have control logic to do anything with it, that should be an awk script, not a shell script. Shells role is to just call awk, something like this:
awk -v user="$1" '
$0 ~ user { line = $0 }
END { print (line != "" ? line : "User not found") }
' /etc/passwd /etc/group
but note that it's trivial with awk to focus the search on just one field, even a different field for each file, unlike how difficult that is in general with grep.

Related

BASH save stdout to new file upon execution

please bear with me if my terminology or syntax is less than stellar (still learning). I currently have a simple bash script that checks the arguments of the command and outputs files names with matching text. This part of my script works correctly via a grep command and piped to xargs for proper formatting.
When running the script, I run through a simple loop to check if the value is null and then move to running my variable/search if not.
My question is: Is it possible to have this script output via stdout AND also save a new file each time it is run with the user input and date/time? (but not overwrite) EX: report-bob-0729161500.rpt
I saw same other suggestions to use tee with the command, but I was trying to get it to work within the script. Similarly, another suggestion stated to utilize exec > >(tee -i logfile.txt), but I am unsure how to properly format this to include the date/time and $1 input into new files each time the script is executed.
Any help or suggested resources?
Thank you.
SEARCH=`[search_variable]`
if [ -z "$SEARCH" ]
then
echo "$1 not found."
else
echo -e "REPORT LISTING\n\n"
echo "$SEARCH"
fi
EDIT: I did try simply piping the echo statements to the tee command, which does work. However, I am still curious if anyone has other suggestions to accomplish this same task via alternative methods. Thank you.
With echo statements piped to tee:
SEARCH=`[search_variable]`
DATE=`date +"%m%d%y%k%M"`
if [ -z "$SEARCH" ]
then
echo "$1 not found."
else
echo -e "REPORT LISTING\n\n" | tee tps-list-$1-$DATE.rpt
echo "$SEARCH" | tee tps-list-$1-$DATE.rpt
fi
If you want to do it within the script, why then not just write to
both standard output and the file (using append where appropriate?).
Maybe a bit more writing, but it gives complete control.
Leon

How to read the first line user types into terminal in bash script

I'm trying to write a script where to run the script, the user will type something along the lines of
$./cpc -c test1.txt backup
into the terminal, where ./cpc is to run the script, -c is $option, test1.txt is $source and backup is $destination.
How would I assign the values typed in to the terminal to use them in my script, for example in
if [[ -z $option || -z $source || -z $destination ]]; then
echo "Error: Incorrect number of arguments." (etc)
as when checking the script online the following errors return: 'option/source/destination is referenced but not assigned.'
Sorry in advance if any of this doesn't make sense, I'm trying to be as clear as possible
The arguments are stored in the numbered parameters $1, $2, etc. So, just assign them
option=$1
source=$2
destination=$3
See also man getopt or getopts in man bash.

How to create a shell script that can scan a file for a specific word?

one of the questions that I have been given to do for my Computer Science GCSE was:
Write a shell script that takes a string input from a user, asks for a file name and reports whether that string is present in the file.
However way I try to do it, I cannot create a shell script.
I don't need you to tell me the whole number, however, I have no idea where to start. I input the variable and the file name, however, I have no idea how to search for the chosen word in the chosen file. Any ideas?
Using grep can get this working, for example
viewEntry()
{
echo "Entering view entry"
echo -n "Enter Name: "
read input
if grep -q "$input" datafile
then
echo ""
echo -n "Information -> "
grep -w "$input" datafile
echo ""
else
echo "/!\Name Not Found/!\\"
fi
echo "Exiting view entry"
echo ""
}
dataFile is the file you would be reading from. Then making use of -q and -w arguments of grep, you should be able to navigate your chosen file.
This site does a great job explaining grep and your exact problem: http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
The following shell-script is a very quick approach to do what you suggested:
#!/bin/sh # Tell your shell with what program this script should be exectued
echo "Please enter the filename: "
read filename # read user input into variable filename
count=`grep -c $1 $filename` # store result of grep into variable count
if [ $count -gt 0 ] # check if count is greater than 0
then
echo "String is present:" $1
else
echo "String not found:" $1
fi
You should look at some tutorials to get the basics of shell-scripting. Your task isn't very complex, so after some reading you should be able understand what the script does and modify it according your needs.

Replacing awk with bash built-ins

I have been told to write a bash script for adding all the GroupID's in "/etc/passwd" file, this is my scrip
#!/bin/sh
# script input should be (sh groupsum.sh /etc/passwd)
if [ -f $1 ] ; then
awk -F ':' '{print $4}' $1 > /tmp/numb
A=`awk '{s+=$1} END {print s}' /tmp/numb`
echo $A
else
echo "its not a file"
fi
The script is working fine but to make it fast I should use bash built-in commands instead of using "awk". So I need information to achieve this using built-in commands it would be great if someone gives the explanation on this.
You said "bash built-ins", but your script starts with #!/bin/sh -- which requests POSIX sh, not bash. I'll assume, though, that you really do want bash.
#!/bin/bash
[[ -f "$1" ]] || { echo "Not a file" >&2; exit 1; }
exec <"$1"
total=0
while IFS=':' read -r _ _ _ groupid _; do
(( total += groupid ))
done
echo "$total"
To explain the specific operations being used to replace components of your awk script: The read command iterates through lines (by default), splitting them by characters in IFS; so IFS=: read -r _ _ _ groupid _ discards the first three columns, puts the fourth in in a variable named groupid, and discards the rest. (( )) is a math context in bash; inside it, C-style syntax is usable for integer arithmetic operations, hence the addition.
By the way, reading /etc/passwd directly is a bad idea -- it won't work on systems using LDAP, or NIS, or any other alternate directory service. If you're on a Linux host, you can use the getent program to do a lookup that works with whatever your current directory service is:
$ yourscript <(getent passwd)
All that said, the premise for this question is a poor one -- though there's overhead for spawning any external program, awk included, once it's running awk is much, much faster than bash. If speed were your only priority, you'd do better to not use a shell at all, and have your script start with a shebang that runs the awk interpreter directly.

I keep getting a 'while syntax' error on the output of the at job in unix and I have no idea why

#!/usr/dt/bin/dtksh
while getopts w:m: option
do
case $option in
w) wflag=1
wval="$OPTARG";;
m) mflag=1
mval="$OPTARG";;
?) printf 'BAD\n' $0
exit 2;;
esac
done
if [ ! -z "$wflag" ]; then
printf "W and -w arg is $wval\n"
fi
if [ ! -z "$mflag" ]; then
printf "M and -m arg is $mval\n"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: $* \n"
at $wval <<ENDMARKER
echo $* >> Search_List
tr " " "\n" <Search_List >Usr_List
while true; do
if [ -s Usr_List ]; then
for i in $(cat Usr_List); do
if finger -m | grep $i; then
echo '$i is online' | elm user
sed '/$i/d' <Usr_List >tmplist
mv tmplist Usr_List
fi
done
else
break
fi
done
ENDMARKER
Essentially I want to keep searching through until it is empty. Each time an element of the list is found, it is deleted. Once the list is empty quit.
There are no error messages when I first run the command, it only shows up in an email containing the output of the at job.
Thanks in advance for any advice
EDIT: The script uses getopts and takes one argument for -w and one for -m, the w value is set as the time for the at job, the m still has to be used. Any arguments after the one for m are sent to a file called Search_List, Search_List is edited and saved as Usr_List. Then in the while loop, while Usr_List is not empty, the script checks the results of finger -m against the names in Usr_List. If a name is found, it is removed from Usr_List. Once Usr_List is empty, the program should stop.
elm is a way to send an email, so elm user sends an email to user.
The error is :
while: Expression syntax
at uses /bin/sh by default.
at now <<ENDMARKER
<code here>
ENDMARKER
All of this executes under /bin/sh, which on some systems can be Bourne Shell (Solaris for example).
You need to figure out what /bin/sh is for your system, then modify things accordingly. Plus, read the gurantees about what is and what is not in your "at" environment. I think the problem lies there. You have both UNIX and linux tags. So I cannot give a lot more help than that.
You can enable logging -- the way YOU need it -- of the at code chunk:
exec 2&>1 > /tmp/somefile.log
Then write debugging messages to stdout or stderr.
Your HEREDOC is being interpolated. Try quoting the delimiter:
at $wval << 'ENDMARKER'
Although ( I haven't looked closely) it appears that you want some interpolation. But you definitely do not want it on the line in which you reference $i, so quote that $ if you do not quote the entire heredoc:
if finger -m | grep \$i; then
You need to pass the -k option to at:
...
at -k $wval <<ENDMARKER
...
at is otherwise defaulting to your login shell which is csh or one of its derivatives.
It turns out that the while command and the if command needed to be combined.
while [[ -s Usr_List ]]; do
......
done

Resources