Show file contents after searching word Done [closed] - linux

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 {}"

Related

I am Attempting to use the output of ls command as an array. And I want it to be line by line [closed]

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 8 months ago.
Improve this question
I need to find the encrypted(.enc) files from a folder and decrypt them.
I will find the .enc files
if [[ -n "$(ls -A /sodaman/tempPrabhu/temp/*.enc 2>/dev/null)" ]]; then
And I used enc_files=($( ls *.enc )) but it takes all the files as one and fails. It considers all the files for the output as one line. So I replaced it with mapfile to decrypt the files one by one but it throws an error
test.sh: line 31: syntax error near unexpected token `<' test.sh: line 31: ` mapfile -t enc_files < <(ls *.enc)'
Below is the script:
if [[ -n "$(ls -A /sodaman/tempPrabhu/temp/*.enc 2>/dev/null)" ]]; then
#create array of encrypted files
mapfile -t enc_files < <(ls *.enc)
enc_files=($( ls *.enc ))
#echo Creating array $enc_files
#decrypt all encrypted files.
echo Creating loop for encryped files
for m in "${enc_files[#]}"
do
d=$(echo "$m" | cut -f 1 -d '.')
echo $d
d=$d.dat
echo $d
/empty/extproc/hfencrypt_plus $m $d Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
echo /empty/extproc/hfencrypt_plus $m $d Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
if [[ -f "$d" ]]; then
mv $m /empty/sodaman/tempPrabhu/blr_temp
#echo Moving file to encrypted archive : mv "$m" /empty/sodaman/enc_archive
echo removing log file : rm log.log
rm log.log
else
echo File was not decrypted successfully
fi
done
fi
Here's a refactoring which avoids several of the http://shellcheck.net/ violations in your attempt.
for file in /sodaman/tempPrabhu/temp/*.enc; do
# avoid nullglob
test -e "$file" || continue
# prefer parameter expansion
d=${file%%.*}.dat
if /empty/extproc/hfencrypt_plus "$file" "$d" Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
then
# assume hfencrypt sets exit code
mv "$file" /empty/sodaman/tempPrabhu/blr_temp
else
# print diagnostics to stderr
# mention which file failed
# mention which script emitted the warning
echo "$0: $file was not decrypted successfully" >&2
sed "s%^%log.log: $file: %" log.log >&2
fi
rm -f log.log
done
This assumes that you wanted to loop over the files in the directory you examine at the beginning of your script, not in the current directory (perhaps see also What exactly is current working directory?) and that the encryption utility sets its exit code to nonzero if encryption failed (perhaps see also Why is testing “$?” to see if a command succeeded or not, an anti-pattern? which discusses idioms around conditions involving errors). I added a sed command to include the output from log.log in the diagnostics after a failure, though perhaps you would like a different error-handling strategy (exit immediately and let the user troubleshoot? Or rename the log file to a unique name and keep it around for later?)
Don't parse ls results, but use this:
find /sodaman/tempPrabhu/temp/ -maxdepth 1 -name "*.enc"

how to use grep command in .log file if it is not empty [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 2 years ago.
Improve this question
i have a log file. When the log file is not empty then i should grep command for policy and work flow. i have tried this code, but how can I initialize the log file and use this command?
cmd=`grep -c "POLICY" file`
if [[ $(grep -c "POLICY" file) -gt 0 ]]
then
echo "POLICY are present"
else
echo "POLICY not present"
fi
You can use grep -q. It will return 0 (success) if any match is found, otherwise (even if the files does not exist) it will return 1 (failure).
if grep -q POLICY file; then
echo "POLICY are present"
else
echo "POLICY not present"
fi

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

bash file Linux Ubuntu [closed]

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

How to write a "bash script.sh argument " [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
hi can someone help me with this.
How to Write a script that takes in as argument a filename and displays its modification date and time in this way exactly :
[user#localhost...]$ bash script.sh temp.txt
the file temp.txt was modified on May 1 20:20
And then modify that script in such a way that it lists the modification dates for directories whose names contain a given pattern in this way exactly :
[user#local....]$ bash script.sh testRegex Pub
the file testRegex was modified on May 1 20:22
the directory /home/user/Public was modified on Dec 26 08:00
the directory /home/user/Pubs. was modified on May 2 20:00
please help I need to answer this fast
Thanks
This is pretty simple to do actually. You should read up on the stat command as #John Bollinger said. I also used the date command to format the date. You can read up on taking arguments for a script here
Combining all of this would give -
#!/bin/bash
filename=$1;
dirname=$2;
file_mod_date=`date -d #$( stat -c %Y $1 ) +%m" "%B" "%H:%M`;
echo "The file ${filename} was modified on ${file_mod_date}";
if [ "$2" == "" ]; then
exit 1;
else
for i in /home/user/*${dirname}*/; do
dir_mod_date=`date -d #$( stat -c %Y $i ) +%m" "%B" "%H:%M`;
echo "The directory ${i} was modified on ${dir_mod_date}";
done
fi
A good way to do this is with passing options and values:
For example:
file_name=""
help_message="To use this script type script.sh --file /path/to/file.txt"
# -- Get input options (if any)
while [[ $# > 0 ]] ;do
key="$1"
case ${key,,} in
-f|--file)
file_name="${2,,}"
shift
;;
-h|--help)
echo -e "$help_message"
exit;
shift
;;
esac
shift
done
Call the script like this:
bash script.sh -f "temp.txt"
With regard to the "logic" of the script, you will have to figure that out ;-)

Resources