Prompting the user for file names in Unix - unix

I want to write a script which prompts the user for the names of two files, lists the first file on screen and waits for user to press any key before second file is listed on the screen. But I am unsure of how to do this. Could anyone help? I think its something like:
VALID_FILENAME_ONE="Test1"
VALID_FILENAME_TWO="Test2"
echo "Please enter first file name:"
read file_name1
echo "Please enter second file name:"
read file_name2
if [ "$file_name1" == "$VALID_FILENAME_ONE"]; then
ls -l | Test1
else
echo "No further action"
fi
But this does not run correctly. Any help would be much appreciated.

In the following line:
if [ "$file_name1" == "$VALID_FILENAME_ONE"]; then
... add a space between "$VALID_FILENAME_ONE" and the closing square bracket, like this:
if [ "$file_name1" == "$VALID_FILENAME_ONE" ]; then
Otherwise, bash can't parse your syntax.

Related

Look for file ksh

I am trying to learn unix. I wanted to set up a sort of file watcher that looks for files and returns the file names so i can move them from source to processing folder to process. It echos File Found I just cannot figure out how to capture the file name.
#determines if file exists
if [ -f * ]; then
echo "File found"
else
echo "File not Found"
fi
# returns file to array
#Needs name still
NewFiles[0] =
#output what what found in 0 index
echo "Found File"
echo NewFiles[0]
Assuming the files have no fancy names (embedded spaces or similar), you might use that approach:
set -- *
[ $# -gt 0 ] && {
echo Found file
echo $1
}

How to log data of a call

I want to log data of asterisk command line. But the criteria is I want log data for calls separately, i.e. I want to log data for each call in separate file.
Is there a way to do that?
In case there is no inbuild feature in asterisk to do this, here is a bash solution:
#!/bin/bash
echo "0" >/tmp/numberoflines
IFS=''
pathToLogFile = /path/to/log/file
while [ 1 ]
do
NUMBER=$(cat /tmp/numberoflines)
LINECOUNT=$(wc -l < $pathToLogFile)
DIFFERENCE=$(($LINECOUNT-$NUMBER))
if [ $DIFFERENCE != 0 ]; then
lines=($(tail -n $DIFFERENCE $pathToLogFile))
for line in $lines; do
callID = `expr "$line" : 'CALLID_REGEX (see below)'`
$(echo "$line" >> /path/to/log/directory/$callID)
done
fi
sleep 5;
echo "$LINECOUNT" >/tmp/numberoflines
done
untested
it should be used to get ab idea to solve this problem.
the regular expression: normaly: /\[(C\d{8})\]/. sadly I don't know the syntax in bash. I'm sorry. you have to convert it by yourself into bash-syntax.
The idea is: remember the last line in the logfile that was processed by the bash script. check the line count of the log file. if there are more lines then the remembered line: walk through the new lines and extract the call id at the beginning of each line (format: C******** (* are numbers). in words: a C followed by a number with 8 digits). now append the whole line at the end of a log file. the name of the file is the extracted callid.
EDIT Information about the call id (don't mistake it with the caller id): https://wiki.asterisk.org/wiki/display/AST/Unique+Call-ID+Logging

Unix step won't complete

First, I am not a Unix scripter, but have been tasked to have the step below to check for the file size. If it is greater than 0, then it will continue to process that file.
However, if its 0, it will echo "Skip it - NO DATA IN THE dnt_pln_inconj.dat FILE"
Problem is when I run this step, it only gets as far as "Received file with data - Continue"
How can I make it continue processing if the file size is greater than 0? What am I doing wrong?
Any help would be awesome.
#Step 12: Verify pln proc inconjunction
step_num=$((step_num+1))
echo "stepnum $step_num: Verify pln proc inconjunction"
if [[ $last_step -eq $step_num ]]
then
if [[ -s ${source_dir}/${file} ]]
then
echo "Received file with data - Continue"
$scriptdir/dental_script/dnt_verify_counts.sh dnt_pln_inconj.dat PLN-130
ret_val=$?
if [[ $ret_val -ne 0 ]]
then
next_step_num=$((step_num+1))
echo $next_step_num > $logdir/$scriptname"_"step_num.log
exit $step_num
fi
else
echo "Skip it - NO DATA IN THE dnt_pln_inconj.dat FILE"
fi
last_step=$((last_step+1))
else
echo "Skip it"
fi
It all depends on what dnt_verify_counts.sh script is doing.
In your code line:
ret_val=$?
is checking for exit code of previous command - which is dnt_verify_counts.sh with parameters. I think it doesn't return file size..
If you want to check file size, play with 'du' utility for example, which gives you size of given file, e.g.:
du /path/to/dnt_pln_inconj.dat
and then, cut given data with cut, to return only size part; line:
$(du /path/to/dnt_pln_inconj.dat | cut -f 1 -)
will return given file size which you can use in your if - then - else block.conditions checking.

How to grep a particular position line from the result?

I grep a pattern from a directory and the 4 lines before that pattern, I need to further grep the top line from each result , but not getting how to do .
Please suggest regarding this.
The problem explained with example :
in a directory 'direktory'
there are multiple files with different name like 20130611 and 2013400 etc..
the data wrote in the files, which I am interested in is like this :
[
My name is
.....
......
......
Name has been written above
]
now in every instance "Name has been written above" is written in the unit of lines but the value keep on changing in place of "My name is" so I want to grep this particular line from every occurrence .
Please suggest some method to get the result.
Thanks in advance.
a#x:/tmp$ cat namefile
[
My name is
.....
......
......
Name has been written above
]
a#x:/tmp$ cat namefile | grep -B 4 "Name has been written above" | head -1
My name is
Where "4" can be replaced by N i.e. number of lines the target data lies above the grepped line
Try something like
for file in $(ls <wherever>)
do
# Tell the user which file we're looking at
echo ""
echo $file
echo ""
# Output the first line of the file
head -1 $file
# Output the line continaing <pattern> and the four
# preceding lines
<your grep command here>
done

UNIX valid_password

why in Cygwin Terminal - the if statement work
and ubuntu - unix - not working for
this code :
#!/bin/sh
valid_password="pass"
echo "Please enter the password:"
read password
if [ "$password" == "$valid_password" ]
then
echo "You have access!"
else
echo "Access denied!"
fi
#emil pointed the answer:
if [ "$password" = "$valid_password" ]
instead of
if [ "$password" == "$valid_password" ]
Also: did you give the script executing permissions? Try
chmod +x script_name
because the correct syntax to [ is:
[ a = b ]
From your error message it sounds like you wrote:
if ["$password" = "$valid_password" ]
change this to:
if [ "$password" = "$valid_password" ]
notice the space after [. if just takes a shell command, try to run it and depending if the exit code from the program is 0 it will run the commands inside the if statement.
In your terminal, write i.e.:
user#localhost$ true; echo $?
0
to test your if statement:
user#localhost$ pass=pass; valid=pass
user#localhost$ if [ "$pass" = "$valid" ]; then echo 'You have access!'; fi
As #nullrevolution said, the ! is evaluated if you use double quotes, it will try to run last command in your shell history, in this case that is matching u.
user#localhost$ uname
Linux
user#localhost$ !u
uname
Linux
user#localhost$ echo "!"
sh: !: event not found
This is because the ! is evaluated before the double quotes are matched, and echo is run. If you still want to use double quotes, you will have to escape the ! outside the quotes:
echo "Access denied"\!
#nullrevolution also said you could try with bash, which has a builtin syntax for the expression inside if statements.
#!/bin/bash
valid_password=pass
echo "Please enter the password:"
read password
if [[ "$password" == "$valid_password" ]]; then
echo 'You have access!'
else
echo 'Access denied!'
fi
Also in your program I guess you do not want to echo the password in the terminal, to turn off echo temporary change:
read password
to
stty -echo
read password
stty echo
if you forgot to write stty echo to turn on echo again, just write reset in your terminal, and it will reset the terminal to default settings.
A useful tutorial for bourn shell script can be found here:
http://www.grymoire.com/Unix/Sh.html

Resources