validate only alphabets using shell script in busybox - sh

I want to validate a string which should only be Alphabets (Capital/Small). I can do it in Linux easily using Bash or Shell, but not able to validate in Busybox (OpenWRT). My piece of code is
...
#!/bin/sh
. /usr/share/libubox/jshn.sh
Info=$(cat /root/Info.json)
json_load "$Info"
json_get_var value plmn_description
echo "$value"
if [[ "$value" == [a-zA-Z] ]] ;then
echo "Valid"
else
echo "Invalid information"
fi
...

You can use case conditional construct like this:
case "$value" in
*[!a-zA-Z]*) echo invalid information ;;
*) echo valid
esac

Using Busybox awk:
$ busybox awk '{ # using busybox awk
for(i=1;i<NF;i++) # iterate all json record fields (not the last, thou)
if($i=="\"plmn_description\":" && $(i+1)~/^\"[a-zA-Z]+\",?$/) {
ret="Valid" # if "plmn_description": is followed by "alphabets"
exit # exit for performance
}
}
END {
print (ret?ret:"Invalid") # output Valid or Invalid
}' Info.json # process the json file
Output:
Valid

Related

Can Org-Babel code be converted to Elisp?

I have an org-babel source, which accesses a server via tramp and runs a shell script there. Is it possible to convert this source block into an Elisp function?
#+BEGIN_SRC sh :dir "/pscp:putty-connection-xy:/dir-yz"
expect -c '
spawn bash -c "scp file-to-copy user#server:/home1/dir-xy"
expect {
"(yes/no)?" {
send "yes\r"
expect "*?assword:*"
send "secretPassWord\r"
}
"*?assword:*" {
send "secretPassWord\r"
}
}
expect eof
'
#+END_SRC
Use Tramp's multi-hop (untested):
(copy-file "/plink:putty-connection-xy:/dir-yz/file-to-copy"
"/plink:putty-connection-xy|ssh:user#server:/home1/dir-xy/")
This uses plink and ssh, 'tho. If you can connect user#server directly from your local machine, you could shorten this, using pscp:
(copy-file "/pscp:putty-connection-xy:/dir-yz/file-to-copy"
"/pscp:user#server:/home1/dir-xy/")
The solution is simple. I call the block and give it over
'sh', the code and the parameters of the block. It works
(org-babel-execute-src-block nil
'("sh"
"cd /home1/
expect -c '
spawn bash -c \"scp file-to-copy user#server:/home1/dir-xy .\"
expect {
\"*?assword:*\" {
send \"secretPassWord\\r\" } }
expect eof'
"
((:colname-names) (:rowname-names)
(:result-params "raw" "replace")
(:result-type . value) (:results . "silent")
(:exports . "code")
(:tangle . "no") (:hlines . "no") (:noweb . "no")
(:cache . "no") (:session . "none")) "" nil nil ))

Code fails with stdin from SED

I have a bash script that finds files with particular extension and then pass the files into a function that checks every line in the file for only files that contain a library imported. For example:
function testing() {
while IFS='' read -r line; do
if [[ "$line" =~ .*log\" ]]; then
echo "log is imported in the file" $1
break
else
echo "log is not imported in the file" $1
break
fi
done < <(sed -n '/import (/,/)/p' "$1")
}
function main() {
for file in $(find "$1" -name "*.go"); do
if [[ $file == *test.go ]]; then
:
else
var1=$(testing $file)
echo "$var1"
fi
done;
}
main $1
The problem is the script works without the else block in the testing function but with the introduction of the else block in the testing function it just defaults to echoing the log is not imported in the file blah even if log is used in some of the files.
Any idea(s) on what is the problem?
Thanks.
Here is a sample input file:
package main
import (
"fmt"
"io/ioutil"
logger "log"
"net/http"
)
type webPage struct {
url string
body []byte
err error
}
...
And the output is basically to echo if log is imported or not.
You need to rewrite the logic of your testing function as it will only test the first line of the file. Indeed, each branch of the if [[ "$line" =~ .*log\" ]] has a break statement, so in practice, a break is reached whenever the first line is read.

Redirect mongo shell error to output

I have a script that seeds my database, but I want to only redirect the stderr to the user.
I'm trying this:
echo "Seeding pokemon"
mongo mongodb_1:27017/pokemon pokemon.js > /dev/null 2>&1
But I'm not getting the error output.If I remove the redirection, the error outputs to my console.
Mongo Shell does not currently support separate output stream for errors.
Have can subscribe to SERVER-18643 to get notified once this is implemented.
Workaround suggested in the above ticket is to tag your output inside the Mongo Shell:
...
print("<STDOUT>")
print(multiline_json)
print("</STDOUT>")
print("<STDERR>")
print(multiline_json)
print("</STDERR>")
...
Then you can redirect to the correct output stream using the following script:
#!/bin/bash
COMMAND="mongo <args>"
OUTPUT=$(${COMMAND})
function STDERR {
cat - 1>&2
}
function STDFILE {
if [ -z "$1" ]; then
return
fi
cat - >> $1
}
WRITE_ERR=0;
for line in $OUTPUT; do
if [[ "$line" == "<STDERR>"* ]]; then
WRITE_ERR=1
continue
elif [[ "$line" == "</STDERR>"* ]]; then
WRITE_ERR=0
continue
fi
if [ "$WRITE_ERR" -eq "1" ]; then
printf "%s\n" "$line" | STDERR
else
printf "%s\n" "$line"
fi
done

How do I port a shell script to Perl?

This is a shell script , How do I accomplish the same thing in Perl?
prfile=~/sqllib/db2profile
profile()
{
if [ -f $prfile ] && [ "$prfile" != "" ];
then
. $prfile
else
read -p "Enter a valid Profile : " prfile
profile
fi
}
profile
Here it checks for the profile file , if found it executes it with . $prfile else it again asks user for the proper profile file
Update
#!/usr/bin/perl
use strict;
use warnings;
my $profile = "$ENV{'HOME'}/sqllib/db2proile";
# default profile
while (not -e $profile) { # until we find an existing file
print "Enter a valid profile: ";
chomp($profile = <>); # read a new profile
}
qx(. $profile);
This worked. I want the home directory to be dynamic rather than hardcoded as they differ for different machines. I'm just trying to accomplish with Perl what I have achieved with shell.
If I understand your objectives, I don't think you can use perl to accomplish this because perl will be running as a child process and it can not change the environment of your shell (it's parent process). Maybe this would work for you (untested, off the cuff)?
prfile=~/sqllib/db2profile
if [ -s "$prfile" ] ; then
. "$prfile"
else
while true ; do
read -p "Enter a valid Profile : " prfile
if [ -s "$prfile" ] ; then
. "$prfile"
break;
fi
done
fi

pass variable to coffeescript

from command line, how to pass a variable to coffeescript, so it can replace a corresponding placeholder, something like this:
$ echo "module.exports = {version: '$VERSION'}" | coffee -p -s VERSION=0.0.0
Expected JS:
(function() {
module.exports = {
version: '0.0.0'
};
}).call(this);
Thank you
Two things:
You need to define VERSION in the echo, not in the coffeescript compiler; by the time the coffeescript compiler sees it it's already translated $VERSION into ''.
echo is a shell builtin, and therefore the standard VERSION=0.0.0 echo "$VERSION" construct doesn't work.
So you want to create a new subshell so that the setting of VERSION doesn't propagate into your main shell, then perform the echo and coffee, like so:
$ (VERSION=0.0.0; echo "module.exports = {version: '$VERSION'}" | coffee -ps)
(function() {
module.exports = {
version: '0.0.0'
};
}).call(this);
The parentheses around the expression stop VERSION from being set:
$ echo $VERSION
$