ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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
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
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.
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:
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.