LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2015, 02:16 AM   #1
rybpdx
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Rep: Reputation: Disabled
BASH help with a line of code


Hi all,

I'm brand new to the forums here at LQ, hope I'm posting to the right place.

I am writing a command menu program and cannot figure out the correct way to add my command into a loop. I want the prompt to continue popping up as long as the output of grep = 0, so I understand the need for a while or until loop. However, I cannot get my command to be in the loop, so $? is still reading as 0 because it's outside the loop.

When I run it the first time, it doesn't let my executable file open in vi. However, after adding a text file name the second time, it still won't open in vi and keeps prompting me. I know that this is because my command is not in the loop, but as I said, I can't figure out how to put it.

file $editfile | grep -qw "executable"

while [ "$?" = "0" ]
do
echo -e "\n"
echo -e "\t THIS IS A BINARY FILE - TRY AGAIN\n"
read -p " Please Enter File To Be Edited: " editfile
done

Any help would be greatly appreciated!
 
Old 12-04-2015, 02:43 AM   #2
rybpdx
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
I figured it out

I was finally able to figure it out by adding the command to the end of the loop as well...

So instead of

file $editfile | grep -qw "executable"

while [ "$?" = "0" ]
do
echo -e "\n"
echo -e "\t THIS IS A BINARY FILE - TRY AGAIN\n"
read -p " Please Enter File To Be Edited: " editfile
done


IT BECAME:


file $editfile | grep -qw "executable"

while [ "$?" = "0" ]
do
echo -e "\n"
echo -e "\t THIS IS A BINARY FILE - TRY AGAIN\n"
read -p " Please Enter File To Be Edited: " edit file
file $editfile | grep -qw "executable"
done


Thought that I'd share my find in case it helps others in the future.
 
Old 12-04-2015, 04:40 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
probably you only mistyped, but edit file and editfile are two different things, do not mix them.
Also you may need to check if $editfile exists (see man test for example).
also remember for example pictures are not executables but still cannot be edited as a text file.
 
1 members found this post helpful.
Old 12-04-2015, 04:59 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Welcome to LinuxQuestions

In addition, not all executable files are binary.
 
2 members found this post helpful.
Old 12-04-2015, 10:30 AM   #5
LanceTaylor
Member
 
Registered: Nov 2015
Location: Houston, Texas, US
Distribution: RHEL/CentOS, Debian/Linux Mint, SLES
Posts: 40

Rep: Reputation: 19
Quote:
Originally Posted by rybpdx View Post
I am writing a command menu program and cannot figure out the correct way to add my command into a loop. I want the prompt to continue popping up as long as the output of grep = 0, so I understand the need for a while or until loop. However, I cannot get my command to be in the loop, so $? is still reading as 0 because it's outside the loop.
Here is an menu example that you may be able to modify to meet your needs:
Code:
#!/bin/sh
valid=0
until [[ $valid == 1 ]] ; do
clear
echo -n "Please enter 1, 2, or 3: "
read choice
case $choice in
        1) echo "You chose one."
                valid=1
                ;;
        2) echo "You chose two."
                valid=1
                ;;
        3) echo "You chose three."
                valid=1
                ;;
        *) echo "You must enter 1, 2, or 3."
                echo "Press enter to continue..."
                read continue
                ;;
esac
done
 
Old 12-04-2015, 11:55 AM   #6
rybpdx
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you all

Thank you guys for the help!

The binary exclusion is what I've been having the most trouble with. When using file command, I was trying to find what identifying symbols or words are used to exclude binary files. Thank you for the reminder that executables are still text that can be edited, such as my menu_script.sh file that this particular line of code comes from.

Last edited by rybpdx; 12-04-2015 at 12:32 PM. Reason: Replaced bold text
 
Old 12-04-2015, 12:51 PM   #7
rybpdx
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Would changing the command to:

file -i $editfile | grep -qw "text"

if [ "$?" = "1" ]
then
echo "This is a binary file"

work better? It is searching specifically looking for text files, not executable (which is ok to edit in vi)

Thanks!

Last edited by rybpdx; 12-04-2015 at 12:55 PM.
 
Old 12-04-2015, 01:30 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
You open a binary in vi, I think you'll know it.
 
Old 12-04-2015, 02:05 PM   #9
rybpdx
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Lol I just meant to grep out anything that specifically makes a file binary. I know the binary file is full of non-human readable characters, but I wasn't sure if using the file command would give insight to the file being binary.
 
Old 12-04-2015, 03:38 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Quote:
Originally Posted by rybpdx View Post
Would changing the command to:

file -i $editfile | grep -qw "text"

if [ "$?" = "1" ]
then
echo "This is a binary file"

work better? It is searching specifically looking for text files, not executable (which is ok to edit in vi)

Thanks!
There are usually many ways to accomplish the same task so if it works for you then yes.
 
Old 12-04-2015, 06:27 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
to compare numbers you ought to use -eq, -lt or -gt instead of =
 
Old 12-05-2015, 02:36 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just because a file is not text does not necessarily make it binary.

Also, if and while evaluate the return value, currently you are evaluating the return value from [] (which I would recommend using [[]] instead), when in fact you could simply test
the return value of your command expression:
Code:
if file -i "$editfile" | grep -qw "text"
 
Old 12-05-2015, 04:10 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by grail View Post
Just because a file is not text does not necessarily make it binary.
Yes it does. Well okay, it depends on your definition of "binary". For instance, another possible definition is that all files are binary, including text files.

Quote:
you could simply test the return value of your command expression:
+1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Create File Listing in C++ that will generate a line number on every line of code marzrocks Programming 11 04-12-2010 06:10 AM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 05:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration