Question is just as the title says. Let's say a script prompts the user to input something, and stores said input into a variable. What's the easiest way to check if that user input/variable is a number in perl?
123205
Is there a simple way in Perl that will allow me to determine if a given variable is numeric?  Something along the lines of:  if (is_number($x)) { ... }   would be ideal.  A technique that won't throw warnings when the -w switch is being used is certainly preferred.
54977
Sometimes I need to ask the user for yes/no to confirm something.  Usually I use something like this:  # Yes/no dialog. The first argument is the message that the user will see. # If the user enters n/N, send exit 1. check_yes_no(){     while true; do         read -p "$1" yn         if [ "$yn" = "" ]; then             yn='Y'         fi         case "$yn" in             [Yy] )                 break;;             [Nn] )                 echo "Aborting..."                 exit 1;;             * )                 echo "Please answer y or n for yes or no.";;         esac     done; }   Is there a better way to do it? Is this utility maybe already in my /bin folder?
4564
I can write  VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}"   the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
34578
#!/bin/bash INT=-5  if [[ "$INT" =~ ^-?[0-9]+$ ]]; then  echo "INT is an integer."  else  echo "INT is not an integer." &gt;&amp;2  exit 1  fi   What does the leading ~ do in the starting regular expression?
24174
I'm trying to create an interactive bash script where I can collect user input variables to be used as arguments in another command. Can someone provide me an example of this?  Currently i want to do:   what type of content you want to delete? what is the status?   then feed it into a mysql delete query command.
32368
How to check if the content of a NSString is an integer value? Is there any readily available way?  There got to be some better way then doing something like this:  - (BOOL)isInteger:(NSString *)toCheck {   if([toCheck intValue] != 0) {     return true;   } else if([toCheck isEqualToString:@"0"]) {     return true;   } else {     return false;   } }
80863
I have a script that calls an application that requires user input, e.g. run app that requires user to type in 'Y' or 'N'. How can I get the shell script not to ask the user for the input but rather use the value from a predefined variable in the script?  In my case there will be two questions that require input.
25712
Is it possible to check a bash script syntax without executing it?  Using Perl, I can run perl -c 'script name'. Is there any equivalent command for bash scripts?
28663
I found this line in a korn shell script:  var=${var:-/temp}   I am able to figure it out what it is doing   $ var=/data $ var=${var:-/temp} $ echo $var /data $ var= $ echo $var  $ var=${var:-/temp} $ echo $var /temp   Does it have advantage over checking the value and then assigning?
6195
Using PHP, what's the fastest way to convert a string like this: "123" to an integer?  Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array?
76187
How can I test whether a command's output is an empty string while not   interfering with buffering?  Example:  command | ??? if [ $? = 0 ] &amp;&amp; [ -n "$output_from_command"]; then    ...   I want the output from the code above to be the same as when running command.
47893
I know I have already asked this question, but I didn't get an answer that worked. So please could someone answer my question?  I have a text file, containing paragraphs of text, each separated by a single empty line. Now I need to number each paragraph with its own number. So the first paragraph will have the number 1 in the margin, the second paragraph will have the number 2 in the margin and the third will have the number 3 in the margin, etc. I know it seems obvious but I think my last post may not have got the message across clearly.  So I need a Linux command that can get that job done. However, my issue is that the command needs to be simple. By that, I mean no complicated Perl scripts or, any difficult to understand, blocks of code.  What I'm looking for, is a simple command that can easily carry out the task. Or if a script needs to be written, make it simple and explain the code so someone who has never used Perl nor Bash scripts can still apply it to their code and understand what they are doing.  Sorry if the question may come across as impolite. Responses will much be appreciated because there is nothing on the web on this topic.
80225
Evidently hash keys are compared in a case-sensitive manner.  $ perl -e '%hash = ( FOO =&gt; 1 ); printf "%s\n", ( exists $hash{foo} ) ? "Yes" : "No";' No  $ perl -e '%hash = ( FOO =&gt; 1 ); printf "%s\n", ( exists $hash{FOO} ) ? "Yes" : "No";' Yes   Is there a setting to change that for the current script?
9887
How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?  How do I view it?
32039
Is there a better/simpler way to find the number of images in a directory and output them to a variable?  function dirCount($dir) {   $x = 0;   while (($file = readdir($dir)) !== false) {     if (isImage($file)) {$x = $x + 1}   }   return $x; }   This seems like such a long way of doing this, is there no simpler way?  Note: The isImage() function returns true if the file is an image.
2830
I want to check if a variable has a valid year using a regular expression. Reading the  I understand I could use the operator =~  Looking at the example below, I would expect to see "not OK" but I see "OK". What am I doing wrong?  i="test" if [ $i=~"200[78]" ] then   echo "OK" else   echo "not OK" fi
85564
From the :  isset() will return FALSE if testing a variable that has been set to NULL.   Basically, isset() doesn't check for whether the variable is set at all, but whether it's set to anything but NULL.  Given that, what's the best way to actually check for the existence of a variable?  I tried something like:  if(isset($v) || @is_null($v))   (the @ is necessary to avoid the warning when $v is not set) but is_null() has a similar problem to isset(): it returns TRUE on unset variables!  It also appears that:  @($v === NULL)   works exactly like @is_null($v), so that's out, too.  How are we supposed to reliably check for the existence of a variable in PHP?    Edit: there is clearly a difference in PHP between variables that are not set, and variables that are set to NULL:  &lt;?php $a = array('b' =&gt; NULL); var_dump($a);   PHP shows that $a['b'] exists, and has a NULL value.  If you add:  var_dump(isset($a['b'])); var_dump(isset($a['c']));   you can see the ambiguity I'm talking about with the isset() function.  Here's the output of all three of these var_dump()s:  array(1) {   ["b"]=&gt;   NULL } bool(false) bool(false)     Further edit: two things.  One, a use case.  An array being turned into the data of an SQL UPDATE statement, where the array's keys are the table's columns, and the array's values are the values to be applied to each column.  Any of the table's columns can hold a NULL value, signified by passing a NULL value in the array.  You need a way to differentiate between an array key not existing, and an array's value being set to NULL; that's the difference between not updating the column's value and updating the column's value to NULL.  Second, , array_key_exists() works correctly, for my above use case and for any global variables:  &lt;?php $a = NULL; var_dump(array_key_exists('a', $GLOBALS)); var_dump(array_key_exists('b', $GLOBALS));   outputs:  bool(true) bool(false)   Since that properly handles just about everywhere I can see there being any ambiguity between variables that don't exist and variables that are set to NULL, I'm calling array_key_exists() the official easiest way in PHP to truly check for the existence of a variable.  (Only other case I can think of is for class properties, for which there's property_exists(), which, according to , works similarly to array_key_exists() in that it properly distinguishes between not being set and being set to NULL.)