Command not found error - bash

I've received an error command not found not sure what is wrong.
i think there is a problem with my code. i need user to enter the pay.
first user enter the ID, then the program will find the person with that ID.
then program will find the type of employee he is [salaried, or hourly]
then from there it will goes to if [$type="Salaried"] or' Hourly' code and
prompt user to key in the respective data
please advise how do i go about doing it?
payroll()
{
line=`grep -i "^${update_empID}," $data`
empID=`echo $line | cut -d "," -f1`
name=`echo $line | cut -d "," -f2`
job=`echo $line | cut -d "," -f3`
phone=`echo $line | cut -d "," -f4`
type=`echo $line | cut -d "," -f5`
clear
echo -e "Enter the pay"
echo -en "Enter ID: "
read empid_search
#Check if particular entry to search for existed to perform deletion
if [ `count_lines "^${empid_search},"` -eq 0 ]
then
echo "Error: This particular record does not exist!!"
else
echo "Please verify update of this employee's record: " #Prompt for confirmation of employee details
echo
echo "Employee's Details: "
locate_lines "^${empid_search}," #Find location of the entry
if [$type="Salaried"]
then
echo "$name is a Salaried"
echo "Enter Salary :"
read salary
echo "${empID},${name},${job},${phone},${Type},${salary}" >> tmpfile ; mv tmpfile $data
echo " particulars has been updated!!"
fi
else
echo "f"
fi
}
TEXT FILE
3,Frak,IT,9765753,Salaried
1,May,CEO,9789292,Salaried
5,Samy,Sales user,92221312,Commission
2,Orange,cleaner,935233233,Hourly
error:
line 371: [=Salaried]: command not found

This is the problem line:
if [$type="Salaried"]
You need to have spaces while comparing values in [ and ]:
if [ "$type" = "Salaried" ]

Related

Linux : need help in finding the syntax error (line 3)

So, the idea is to insert all the active users who have user id -le 500 into a mysql table.
The syntax error is at line 3; I've searched the whole script and still haven't found it.
Here's the script :
#!/bin/bash
for name in $(cat /etc/passwd) do
username=`echo $name | cut -d : -f1`
userid=`echo $name | cut -d : -f3`
userpass=`cat /etc/shadow | grep $username | cut -d : -f2`
if [[ "`expr $userpass`" =~ "!" ]]; then
echo "$username is disabled, skipped
else
if [[ $userid -le 500 ]]; then
echo "User id of $username is over 500, skipped"
else
fullname=`echo $name | cut -d : -f5`
"INSERT INTO`mailbox`(username,`password`,`name`,`maildir`,`quota`,`local_part`,`domain`,`created`,`modified`,active)VALUES($username,'$pass','$fullname$',0,'xxx','date --rfc3339=date','date --rfc3339=date',1)
fi
fi
done
The syntax error here is a missing ; before do.
for name in $(cat /etc/passwd) do
should be
for name in $(cat /etc/passwd); do
After you added the ; also add a closing " in echo "$username is disabled, skipped. Besides these syntax errors there are many things you could improve in your script. I recommend https://www.shellcheck.net/.
You are missing a semicolon in your line 3:
for name in $(cat /etc/passwd); do

compare 2 values within a while read loop, one from stdout and one from file

I have a while read line loop where i am assigning $user and $quota twhich are coming from stdout but i have also a file from another server where i am checking if $user is existing in this file, which is also containing the $user's quota.
I need to compare the $quota value which i am getting in stdout with the value within the file.
example:
zmsoap -z GetQuotaUsageRequest -v -u https://$sourceserver:7071/service/admin/soap/ |awk {'print $4" "$5'} | cut -d'"' -f2,4 | sed -r 's/["]+/ /g' | while read line
do {
user1=`echo $line|cut -f1 -d " "`
quota1=`echo $line|cut -f2 -d " "`
echo "$user1 has $quota1 on $sourceserver"
if grep -q "$user1" "$allremotequotasnew"; then
echo "OK: $user1 is EXISTING on $destserver"
else
echo "ERROR: $user1 NOT FOUND on $destserver" && echo "Creating $user1 on $destserver now..."
fi
}
done
any ideas? thanksa lot in advance
The file looks like: user33 quota and i need to check if $user is existing in the file, and if so, then also cut out it's quota
You can solve this question by the following script clips.
grep "$user1" "$allremotequotasnew" > /dev/null
if [ $? -eq 0 ];then
echo "OK: $user1 is EXISTING on $destserver"
dest_line=`grep "$user1" "$allremotequotasnew"`
dest_user=`echo $dest_line|cut -f1 -d " "`
dest_quota=`echo $dest_line|cut -f2 -d " "`
else
echo "ERROR: $user1 NOT FOUND on $destserver"
fi
dest_user and dest_quota are what you want to get from file.

grep function not working

what am I doing wrong in the code below?
I am replacing the salary data within the text file, but the Telephone number field (the 3rd column) is being updated instead of the salary field (the 5th column) which is 0.
In the example below, the calculated salary for Ruben is 500.
My Desired output is:
Ruben,1223,97707001,Salaried,500
But instead, I get this (replacing zero between 9770 and 7001 with 535):
Ruben,1223,9775007001,Salaried,0
payroll_employee()
{
echo
echo "[Option: $input]"
echo "Enter Payroll of an employee "
echo
echo -en "Enter employee name: "
read Name
#Retrieve current entry into individual fields
line=`grep -i "$Name" $PAYROLL`
Name=`echo $line | cut -d "," -f1`
EmployeeID=`echo $line | cut -d "," -f2`
EmployeeHP=`echo $line | cut -d "," -f3`
EmployeeType=`echo $line | cut -d "," -f4`
Salary=`echo $line | cut -d "," -f5`
#Check if entry exist in records
if [ `count_lines "^${Name},"` -eq 0 ]
then
echo "Error: This particular record does not exist!!"
else
echo "$Name is ${EmployeeType} employee."
if [ "$EmployeeType" = "Salaried" ]
then
echo $EmployeeType
echo -en "Enter Weekly Salary:"
read swages
if [ -z $swages ]
then
swages=$Salary
else
grep -vi "$Name" $PAYROLL > tmpfile #Perform updating to salary field entry
grep -x "$line" $PAYROLL | sed -e "s/$Salary/$swages/" >> tmpfile
mv tmpfile $PAYROLL
echo "$Name's weekly payroll has been updated to \$$swages!!"
fi
echo
}
Sample code:
update_employee()
{
echo
echo "[Option: $input]"
echo "Updating employee record... "
echo "Please enter the name of the employee to update: "
echo -en "[1]Name: "
read update_name
#Retrieve current entry into individual fields
line=`grep -i "$update_name" $PAYROLL`
oldname=`echo $line | cut -d "," -f1`
oldjob=`echo $line | cut -d "," -f2`
olddept=`echo $line | cut -d "," -f3`
oldsal=`echo $line | cut -d "," -f4`
#Check if entry to update exist in records
if [ `count_lines "^${update_name},"` -eq 0 ]
then
echo "Error: This particular record does not exist!!"
else
while [ "$choice" != "6" ]
do
update_menu #Display update menu for user input,allows update of individual field or all at once
read update_choice
case $update_choice in
"1") echo -en "Please enter employee's new name: "
read new_name
if [ -z $new_name ]
then
new_name=$oldname
elif [ `count_lines "^${new_name},"` -ne 0 ] #Check if name already exist in records
then
echo "Error: Employee [$new_name] already exist in records!"
else
grep -vi "$oldname" $PAYROLL > tmpfile #Perform updating to name field entry
grep -x "$line" $PAYROLL | sed -e "s/$oldname/$new_name/" >> tmpfile
mv tmpfile $PAYROLL
echo "Employee's name $oldname has been updated to [$new_name]!!"
fi
break
;; }
All I changed was add one more column.
Salary=`echo $line | cut -d "," -f5`
payroll_employee()
{
echo
echo "[Option: $input]"
echo "Enter Payroll of an employee "
echo
echo -en "Enter employee name: "
read Name
#Retrieve current entry into individual fields
if [ $(grep -ciw "$Name" $PAYROLL) -eq 0 ]
then
echo "No matches found in $PAYROLL";
else
line=$(grep -iw "$Name" $PAYROLL);
Name=$(echo $line | awk -F "," print $1);
EmployeeID=$(echo $line | awk -F "," print $2);
EmployeeHP=$(echo $line | awk -F "," print $3);
EmployeeType=$(echo $line | awk -F "," print $4);
Salary=$(echo $line | awk -F "," print $5);
fi
if [ "$EmployeeType" == "Salaried" ]
then
echo $EmployeeType
echo -en "Enter Weekly Salary:"
read swages
if [ -z $swages ]
then
swages=$Salary
else
sed "/$Name/d" $PAYROLL
echo "$Name $EmployeeID $EmployeeHP $EmployeeType $swages" >> $PAYROLL;
echo "$Name's weekly payroll has been updated to \$$swages!!"
fi
echo
}
I Prefer use of AWK for delimination
also you can delete line using
sed "/$Name/d" $PAYROLL
Why to store it in other file using grep -vi and rename back and replace with original (Waste of memory & resources)
also "`" backtick should be replaced with new method of using $(..)
Also one make sure when you match strings use == sign
also in grep use -w also just to make sure that you select proper complete string
Ruben baruben rubenner all will be searched in grep if -w is not given
Seems like you are replacing every occurrence of the string $Salary in the payroll line, so if salary is 100 and the employee ID number is 2100, it will be replaced.
Instead of using sed at the end, you would probably be better off generating the output using printf and building the fields up that way.
Something like:
printf "%s,%s,%s,%s,%s\n" $Name $EmployeeID $EmployeeHP $EmployeeType $swages >> tmpfile
EDIT: You should fix the = to == as pointed out in the comments.
Also, to illustrate what I think is happening:
line="ABC,5100,DEF,100"
Salary=100
echo $line | sed -e s/${Salary}/XXX/
ABC,5XXX,DEF,100
If you "anchor" the query by putting a $ at the end of the search string, it will only match the last value.
echo $line | sed -e s/${Salary}$/XXX/
ABC,5100,DEF,XXX
Add some echo statements in your code to check the status of variables....

add specific string to a specific line at the end. -bash

I have a textfile(particulars.txt) which contains
personId,personName,employmentType
particulars.txt
1,jane,partTime
2,bob,fullTime
3,john,fullTime
How to do I make it such that if I key in the name of the worker, it will check whether that person is full time or part time worker and prompts the user to key in the salary and rewrite back to the file just for that person. I will explain in more details.
for example
Enter Name:jane
jane is a partTime staff
Enter Hourly Salary:10
so the textfile(particulars.txt) will now be
1,jane,partTime,10
2,bob,fullTime
3,johnfullTime
example two
Enter Name:bob
bob is a fullTime staff
Enter monthly Salary:1600
so the textfile(particulars.txt) will now be
1,jane,partTime,10
2,bob,fullTime,1600
3,john,fullTime
this is what I have
my code
#!/bin/bash
fileName="particulars.txt"
read -p "Enter name:" name
if grep -q $name $fileName; then
employmentType=$(grep $name $fileName | cut -d, -f4)
echo "$name is $employmentType" staff"
if [ $employmentType == "partTime" ]; then
echo "Enter hourly pay:"
read hourlyPay
#calculations for monthly salary(which I have not done)
elif [ $employmentType == "fullTime" ]; then
echo "Enter monthly salary:"
read monthlySalary
fi
else
echo "No record found"
fi
read -p "Press[Enter Key] to Contiune.." readEnterKey
I am only able to find which employment type does the person belongs to, but I am not sure of how/what should I do to add the salary at the end of the line just for that particular person. i have read up on sed , but I'm still confused on how to use sed to achieve my results and thus seeking help from you guys. Thanks in advance
Unless you need to do it in an interactive manner, you can say:
sed '/\bbob\b/{s/$/,1600/}' filename
This would add ,1600 to the line matching bob. Note that by specifying word boundaries \b, you'd ensure that the change is done only for bob, not abob or boba.
You can use the -i option to make the changes to the file in-place:
sed -i '/\bbob\b/{s/$/,1600/}' filename
EDIT: In order to use shell variable, use double quotes for the sed command:
sed "/\b$employeeName\b/{s/^$/,$monthlySalary/}" filename
I just modified your script.
#!/bin/bash
fileName="particulars.txt"
read -p "Enter name:" name
if grep -q $name $fileName; then
employmentType=$(grep $name $fileName | cut -d, -f3)
emp_name=$(grep $name $fileName | cut -d, -f2) # Getting emp name
emp_id=$(grep $name $fileName | cut -d, -f1) # Getting id
echo "$name is $employmentType staff"
if [ $employmentType == "partTime" ]; then
echo "Enter hourly pay:"
read hourlyPay
#calculations for monthly salary(which I have not done)
sed -i "s/^$emp_id.*$/&,$hourlyPay/g" $fileName # Modifying the file by using id.
elif [ $employmentType == "fullTime" ]; then
echo "Enter monthly salary:"
read monthlySalary
fi
else
echo "No record found"
fi
read -p "Press[Enter Key] to Contiune.." readEnterKey
I have added the below lines.
emp_id=$(grep $name $fileName | cut -d, -f1)
emp_name=$(grep $name $fileName | cut -d, -f2)
sed -i "s/^$emp_id.*$/&,$hourlyPay/g" $fileName

grep exact integer match

read -p "Please enter ID: " staffID
id=$(grep -w "$staffID" record | cut -d ":" -f1 | sort -u );
echo $id
I have some issues with trying to grep the correct value from a file.
The following is stored in the record file.
12:Griffin:Peter:13:14:16
14:Griffin:Meg:19:19:10
10:Griffin:Loi:19:20:20
130:Griffin:Stewie:19:19:19
13:Wayne:Bruce:19:20:2
My first column stores the id which is always unique and is what I am looking for in grep. With the code above, I only want to find the unique ID which is entered by a user and is displayed on screen but my echo produces a blank value if I enter an ID of 13 when it should produce 13 obviously. Any ideas which would solve this?
#!/bin/bash
read -p "Please enter ID: " staffID
#your code was commented out
#id=$(grep -w "$staffID" record | cut -d ":" -f1 | sort -u );
id=$(grep -oP "^${staffID}(?=:)" record)
line=$(grep "^${staffID}:" record)
echo $id #use this line if you just want ID
echo $line #use this line if you want the line with given ID
see comments in codes
Note
I don't know the exact requirement, but I suggest that before do grep, check user input, if they inputted a valid id ([0-9]+) maybe? Because user could input .*
Seems adding a ^ to grep should solve your problem.
read -p "Please enter ID: " staffID
[[ "$staffID" =~ ^[0-9]+$ ]] || { echo "Enter only Numbers. Aborting" ; exit 2 ; }
id=$(grep -w "^$staffID" record | cut -d ":" -f1 | sort -u );
if [ "$id" == "" ]; then
echo "ID : Not found"
else
echo $id
fi
I have added a line to check that your input is a valid number.

Resources