Writing a script in Unix to see if a file exist and to show its content - variables

I'm writing a program in Unix to have the user enter in the file they would like to view the contents on but i'm stuck and dont know way i keep getting error.
the errors i keep getting are :unexpected EOF while looking for matching `"'
and the other is: Syntax error: unexpected end of file
# this program allows the user to see the contents of a file
echo
clear
echo
echo "Enter in the the file you would like to see: "
read $1
if [ ! -e "$1" ]
then
echo cat /export/home/cna397/logname/$1
else
echo "This file does not exist
fi

You're missing an ending double quote here:
echo "This file does not exist
$1 is for commandline arguments. You'll need something like this if you want the user to enter a filename while the script is running:
read filename
echo $(cat "/export/home/cna397/logname/$filename")

this is the executable (without errors) version of what you wrote, now continue with a better basis, if you still have questions just update this post with code plus comments, or just make a new one.
echo "Enter in the the file you would like to see:"
read file_name
if test ! = $file_name
then
echo $(cat /export/home/cna397/logname/$file_name)
else
echo "This file does not exist"
fi
P.S if on the if test you are checking strings, keep the equal (=) i put....

Related

get exit code of a background process that completed a while ago

This may not be a completely new topic but I ran into a little bit odd situation.
I'm processing about 1000 files in a loop by kicking off a script in background. I want to take some actions on the files based on the exit code each process returns. By the time I go in a loop to wait for each process to complete I found that some of the process were already done. I modified the script to wait only if pgrep finds a process and just assumed a process completed successfully otherwise. The problem is- I have to know exit code of each process in order to take action on the corresponding file. Any ideas?
pid_list=()
for FILE in $SOME_FOLDER
do
(process with FILE as parameter) &
done
for pid in "${pid_list[#]}"
do
if pgrep $pid; then #process could have just completed as we got here
if wait $pid; then
echo "process $pid successfully completed!" >> $logfile
else
echo "process $pid failed!" >> $logfile
fnc_error_exit
fi
else
echo "assumed that process $pid successfully completed but I DON'T KNOW THE EXIT CODE!" >> $logfile
continue
fi
done
I cannot solve your problem exactly.
Since I do not know the exact situation of you, anyway, could you try another method using parent and child scripts ?
For example, the topmost script is like this:
for FILE in $HOME/*.txt
do
parent.sh $FILE &
done
Then, the parent.sh is like this:
child.sh $1
RC=$?
case $RC in
0 ) echo Exit code 0 for $1 >> parent.log
;;
1 ) echo Exit code 1 for $1 >> parent.log
;;
* ) echo Other Exit code $RC for $1 >> parent.log
;;
esac
The child script is like this:
grep -q hello $1
Then, the parent.sh will handle every exit code of the child.sh
All files will be handled by a parent.sh, without a missing handling.

How can i error out an entry if it already exists?

I'm writing a simple program to add a contact into a file called "phonebook", but if the contact already exists, i want it to return an echo saying " (first name last name) already exists", and not add it to the file. So far, i've gotten the program to add the user, but it wont return that echo and adds the duplicate entry anyway. How can i fix this?
#!/bin/bash
# Check that 5 arguments are passed
#
if [ "$#" -ne 5 ]
then
echo
echo "Usage: first_name last_name phone_no room_no building"
echo
exit 1
fi
first=$1
last=$2
phone=$3
room=$4
building=$5
# Count the number of times the input name is in add_phonebook
count=$( grep -i "^$last:$first:" add_phonebook | wc -l )
#echo $count
# Check that the name is in the phonebook
if [ "$count" -eq 1 ]
then
echo
echo "$first $last is already in the phonebook."
echo
exit 1
fi
# Add someone to the phone book
#
echo "$1 $2 $3 $4 $5" >> add_phonebook
# Exit Successfully
exit 0
Couple of things:
Should check if add_phonebook file exists before attempting to grep it, otherwise you get the grep: add_phonebook: No such file or directory output.
Your grep expression doesn't match the format of the file.
You are saving the file with space in between the fields, but searching with a colon(:) between the names. You can either update the file format to use a colon to separate the fields, or update the grep expression to search on space. In addition, you save first name, last_name, but search on last_name, first_name.
With space format:
count=$( grep -i "^$last[[:space:]]\+$first[[:space:]]" add_phonebook | wc -l )
Removed my tab separators from the echo line, used spaces, and now it can count properly

How do I search text in a file with DCL

How do I search text in a file with DCL? Yes, I have to use DCL.
The file format is straight forward:
<NUMBER OF ENTRIES>
<ID> <DIRECTORY>
<ID> <DIRECTORY>
.
.
.
<ID> <DIRECTORY>
They're separated by a few white space characters. I just need to search the file for a given ID and extract the DIRECTORY.
It's a really simple task, but I can't seem to find any decent DCL documentation anywhere.
Edited.... the forum 'eats' strings like <xx> unless marked as code.
Are there pointy brackets on the datalines or not?
Please provide a REAL example
is it
or: XX XXX-DIRECTORY
I am assuming the first.
VMS as it ships does NOT have a standard tool to select a field from a record.
But there are a bunch of standard tools available for OpenVMS which can do this.
Mostly notably (g)AWK and PERL
So that's what I would use:
$ gawk /comm="$1 == ""<xx>"" { print $2 }" tmp.tmp
<xxx-DIRECTORY>
or
$ perl -ne "print $1 if /^\s*<xx>.*?<([^>]*)/" tmp.tmp
xxx-DIRECTORY
Those can be augmented for case-and-space-sensitivity, as needed and trim that <> as needed.
And maybe you need the search ID to be a parameter or not.
Anyway, in a pure DCL script it could look like....
$ IF p2.eqs."" then exit 16
$ CLOSE/NOLOG file
$ OPEN/READ file 'p1
$loop:
$ READ/END=done file rec
$ id = F$EDIT( F$ELEM(0,">",F$ELEM(1,"<",rec)), "UPCASE")
$ IF id.NES.p2 THEN GOTO loop
$ dir = F$ELEM(0,">",F$ELEM(2,"<",rec))
$ WRITE SYS$OUTPUT dir
$ GOTO loop
$done:
$CLOSE/NOLOG file
if the <> do not exist, use this for core...
$ rec = F$EDIT(rec,"TRIM,COMPRESS")
$ id = F$EDI(F$ELEM(0," ",rec),"UPCASE")
$ IF id.NES.p2 THEN GOTO loop
$ dir = F$ELEM(1," ",rec)
And the perl would be:
$ perl -ne "print $1 if /^\s*<xx>\s+(\S+)/" tmp.tmp
Good luck
Hein
Alternatively, if the ID field looks like fixed-width, then you may convert the file to RMS INDEXED , keyed on ID field. Then you can just do lookup by calling READ/KEY='ID'.
Call HELP on CONVERT , READ /KEY and perhaps SEARCH /KEY

Why is my input not creating a folder?

I cannot figure out why my script is not creating a folder from my input.
Heres my code ...
#!/bin/bash
echo "Please enter a foldername to store archives in"
read $2
echo "++++++++++++++++++++++++++"
mkdir $2
echo "++++++++++++++++++++++++++"
You use a wrong variable to store the directory name $2 is the second parameter given to your script when you execute it (it could be empty if no second parameter). However i never tested but maybe it could work. The main problem is that for a the read function, you provide the variable without a '$'.
Try to declare a variable.
#!/bin/bash
foldername=""
echo "Please enter a foldername to store archives in"
read foldername
echo "++++++++++++++++++++++++++"
mkdir $foldername
echo "++++++++++++++++++++++++++"

How to check return value of Find statment in shell script?

How can I check the return value of "Find" statement in shell script
I am use Find in my script , if find statement don't find any file the execute exit !!
I want to check the return value of "Find" if it found any files or not
You can redirect output of the find command to a file called say output.txt then you can check if the size of that file is 0 or not by using -s option;
if [[ -s "output.txt" ]]
then
echo "File is not empty!"
else
echo "File is empty!"
fi
You can count the number of files found by find using the wc -l command:
export result=`find . -name *.txt | wc -l`
You can now check result to see how many files where found
if [ $result == "0" ]; then echo zero found; fi