I have this code
<?php
$date1=date('d.M',strtotime("+1 days"));
$date2=date('d.M',strtotime("+2 days"));
echo 'Ajunge la tine ' ,$date1, ' - ' ,$date2 ;
?>
I what in echo to set color red for $date1 and $date2 .
Thanx
Related
I have a code.txt file that contains morse code for example
.- .-.
I have a function called decode inside a bash file called morse as this:
decode (){
sed -i 's/ \.-/A/g' $1
sed -i 's/ \.-./R/g' $1
cat $1
}
When I type in terminal $bash morse decode code.txt
I receive:
AA.
The output I want is :
AR
How can it see separate that the string .- is A and the .-. is R?
If your intention is to encode and decode Morse messages with any tool then something like this will do :
#!/usr/local/bin/python3
import re
alphabet = { 'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-',' ':' '}
def encode(message):
return "".join([ ( alphabet[letter.upper()] + ' ' ) if letter != ' ' else ' ' for letter in message])
def decode(message):
return "".join([ list(alphabet.keys())[list(alphabet.values()).index(item if item != '|' else ' ')] for item in re.sub(r' {2,}', ' | ',message).split(' ')])
print(encode('THIS IS FINE'))
print(decode('- .... .. ... .. ... ..-. .. -. .'))
Hope it helps too.
Wow interesting idea! Based on #MatiasBarrios alphabet i made this.
#!/bin/bash
string=$1
declare -A morse=(
[A]='.-' [B]='-...' [C]='-.-.' [D]='-..' [E]='.'
[F]='..-.' [G]='--.' [H]='....' [I]='..' [J]='.---'
[K]='-.-' [L]='.-..' [M]='--' [N]='-.' [O]='---'
[P]='.--.' [Q]='--.-' [R]='.-.' [S]='...' [T]='-'
[U]='..-' [V]='...-' [W]='.--' [X]='-..-' [Y]='-.--'
[Z]='--..'
[1]='.----' [2]='..---' [3]='...--' [4]='....-' [5]='.....'
[6]='-....' [7]='--...' [8]='---..' [9]='----.' [0]='-----'
[(]='-.--.' [)]='-.--.-' [/]='-..-.' [-]='-....-' [+]='.-.-.'
[.]='.-.-.-' [,]='--..--' [?]='..--..' [!]='-.-.--' [ ]=' '
)
morse () {
while [[ "$string" ]]; do
symbol="${string::1}"
printf -- "${morse["${symbol^}"]} "
string="${string:1}"
done
}
demorse () {
declare -A demorse
for item in "${!morse[#]}"; { demorse["${morse["$item"]}"]="$item"; }
while [[ $# ]]; do
printf -- "${demorse["$1"],}"
shift
done
}
case $string in
demorse) shift; demorse "$#";;
* ) morse ;;
esac
Usage
$ ./morse 'hello world!'
.... . .-.. .-.. --- .-- --- .-. .-.. -.. -.-.--
Demorse also worsk but, spaces have to be printed like this ' '
$ ./morse demorse .... . .-.. .-.. --- ' ' .-- --- .-. .-.. -.. -.-.--
hello world!
You need to run s/ \.-\./R/g replacement first. Note the second . must be escaped to only match a dot.
Hence, use
sed 's/ \.-\./R/g;s/ \.-/A/g' file
See the online demo
Or, another way:
sed -e 's/ \.-\./R/g' -e 's/ \.-/A/g' file
Replace the file with "$1" in your code.
UPDATE
Here is the translation of encoding / decoding Python function posted by Matias below:
#!/bin/bash
### Encoding:
declare -A MORSE=( [A]='.-' [B]='-...' [C]='-.-.' [D]='-..' [E]='.' [F]='..-.' [G]='--.' [H]='....' [I]='..' [J]='.---' [K]='-.-' [L]='.-..' [M]='--' [N]='-.' [O]='---' [P]='.--.' [Q]='--.-' [R]='.-.' [S]='...' [T]='-' [U]='..-' [V]='...-' [W]='.--' [X]='-..-' [Y]='-.--' [Z]='--..' [1]='.----' [2]='..---' [3]='...--' [4]='....-' [5]='.....' [6]='-....' [7]='--...' [8]='---..' [9]='----.' [0]='-----' [',']='--..--' ['.']='.-.-.-' [';']='-.-.-.' [':']='---...' ['?']='..--..' ['!']='-.-.--' ['/']='-..-.' ['-']='-....-' ['+']='.-.-.' ['(']='-.--.' [')']='-.--.-' ['_']='..--.-' ['"']='.-..-.' ["'"]='.----.' ['$']='...-..-' ['#']='.--.-.' ['&']='.-...' [' ']=' ' )
function encode {
res=''
s="$1"
for (( i=0; i<${#s}; i++ )); do
letter="${s:$i:1}"
if [[ "$letter" == ' ' ]]; then
res="${res} "
else
res="${res}${MORSE[${letter^^}]} ";
fi
done
printf "%s" "$res"
}
echo "$(encode "THIS IS FINE")"
### Now, decoding
declare -A MORSEDEC=( ['-.--.-']=')' ['..--..']='?' ['--..--']=', ' ['-....-']='-' ['.-.-.-']='.' ['...--']='3' ['-.--.']='(' ['---..']='8' ['-..-.']='/' ['....-']='4' ['-....']='6' ['----.']='9' ['.----']='1' ['..---']='2' ['.....']='5' ['--...']='7' ['-----']='0' ['-...']='B' ['-..-']='X' ['-.-.']='C' ['--..']='Z' ['--.-']='Q' ['.-..']='L' ['-.--']='Y' ['..-.']='F' ['.--.']='P' ['.---']='J' ['...-']='V' ['....']='H' ['-..']='D' ['---']='O' ['..-']='U' ['...']='S' ['.--']='W' ['-.-']='K' ['.-.']='R' ['--.']='G' ['-.']='N' ['..']='I' ['--']='M' ['.-']='A' [' ']=' ' ['.']='E' ['-']='T' )
function decode {
res=''
tmp="$(sed 's/ \{2,\}/ | /g' <<< "$1")";
for word in $tmp; do
if [[ "$word" == '|' ]]; then
res="${res}${MORSEDEC[' ']}";
else
res="${res}${MORSEDEC[$word]}";
fi
done
printf "%s" "$res"
}
echo "$(decode "- .... .. ... .. ... ..-. .. -. .")"
See Bash demo online.
The easy answer in RE engines that support look-ahead and look-behind would be to treat the spaces as look-ahead and look-behind triggers, but sed does not support this.
Another option that avoids needing to order the letters is to inject extra symbols to help you mark each letter. Say we inject = round each space, then we can replace delimited sequences in any order, and finally get rid of the delimiters:
echo .- .-.|sed -e 's/^\(.*\)$/=\1=/;s/ /= =/g' -e 's/=\.-\.=/=R=/g;s/=\.-=/=A=/g' -e 's/= =//g;s/^=//;s/=$//'
If you have rules that need to preserve multiple spaces, then that can be accommodated.
Hope someone could help me with this. I want to get output like this:
Width (cm): 46
I want Width (cm): to be bold and value (46) to be regular.
Here is echo:
echo "<p>". __( 'Width (cm): ', 'woocommerce' ) . $_wccf_pp_width ."</p>";
$_wccf_pp_id_width = get_post_meta($product_id, "__wccf_pp_width", true);
I know I can wrap whole echo in div and put some style to p tag but how to separate styles for field label (Width (cm):) and for value (46)? Thanks in advance.
<?php
echo "<span style='font-weight:bold;'>". __( 'Width (cm):</span><span> ', 'woocommerce'
) . $_wccf_pp_width ."</span>";
$_wccf_pp_id_width = get_post_meta($product_id, "__wccf_pp_width", true);
?>
I think this should work.
echo "<p><b>". __( 'Width (cm): ', 'woocommerce' ) . "</b>" . $_wccf_pp_width ."</p>";
Use html bold tag (<b></b>)
ok.. i'm having trouble with updating new book title.. i've already searched for solutions on this website.. and ive tried them but none of them work..
function update_book
{
#echo "Title: "
read -p $'Title: ' updatetitle
#echo "Author: "
read -p $'Name: ' updatename
if grep -Fq "${updatetitle}:${updatename}" BookDB.txt
then
echo "Book found!"
if ! [ -f BookDB.txt ] ; then
touch BookDB.txt
fi
selection=0
until [ "$selection" = "f" ]; do
echo ""
echo ""
echo "Book Update System"
echo ""
echo ""
echo "a) Update title"
echo "b) Update Author"
echo "c) Update Price"
echo "d) Update Qty Available"
echo "e) Update Qty Sold"
echo "f) Back to main menu"
echo -n "Enter your option: "
read selection
echo ""
case $selection in
a) upd_title;press_enter;;
b) upd_author;press_enter;;
c) upd_price;press_enter;;
d) upd_qty_avail;press_enter;;
e) upd_qty_sold;press_enter;;
f) main_menu;press_enter;;
* ) tput setf 4;echo "Please enter a, b, c, d, e, or f";tput setf 7; press_enter
esac
done
else
echo "Error!! Book does not exist!" #not found
fi
}
ok i've made some changes to the codes for this function.. found out that i should use awk to update from old to new. and use grep to get line number..so it's best i stick with this..
function upd_title
{
read -p 'New title: ' title
#awk '/liine/{ print NR; exit }' BookDB.txt
grep -n 'regex' | sed 's/^\([0-9]\+\):.*$/\1/'
awk 'NR==n{$1=a}1' FS=":" OFS =":" n=$linenumber a = $title BookDB.txt
echo "New title successfully updated!!"
}
but after i tried that code.. this is what i got:
Advanced Book Inventory System
1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
Enter your option: 3
Title: The Notebook
Name: Nicholas Sparks
Book found!
Book Update System
a) Update title
b) Update Author
c) Update Price
d) Update Qty Available
e) Update Qty Sold
f) Back to main menu
Enter your option: a
New title: Notebook
still doesn't update the title.. >.< anyone can help me point out what's the problem? am i missing something here
help me how to do this.. thanks! :D :D
$updatetitle and $updateauthor defined by the read statements in update_book() are not visible in the upd_title() and other functions you define.
Try exporting them after they are read in. If this doesn't work, pass them in as positional parameters to update_title():
upd_title $update_title $update_author
...
function upd_title
{
update_title=$1
update_author=$2
...
}
This is my first time trying scripting and I'm trying to create a small program that does a simple division and mod in a loop, and then calculates the average of the mod results.
This is what I have tried in Linux .sh, but how could I make it compatible with Windows .bat? Your help is very appreciated.
echo "enter first number:"
read first_num
echo “enter second number:”
read second_num
while [ first_num && second_num != 999 ]
do
if [ second_num != 0 ]; then
echo "Enter first number:"
read first_num
echo"Enter second number:"
read second_num
echo first_num "/" second_num "=" $((first_num / second_ num)) >> file.txt
else
echo "ERROR. Cannot divide by 0. Enter another number:"
fi
done
if [ first_num == 999 || second_num == 999 ]; then
echo "You have exited the loop."
fi
#Mod 5 of numbers 1-100:
for i in {1...100}
do
result=$((i % 5))
echo i + "%5=" + result >> file.txt
done
#Average of results:
int sum=0
for (( i=1; i<101; i=i+1 ))
do
sum=sum+$((i % 5))
average=$((sum/100))
echo average
echo average >> file.txt
done
echo "enter first number:"
read first_num
becomes
set /p first_num="Enter first number "
while [ first_num && second_num != 999 ]
don't have a WHILE - have to wire it. Think clubs and rocks.
:loop
if %first_num%==999 goto endloop
if %second_num%==999 goto endloop
...
goto loop
:endloop
:name is a label, %var% retrieves contents of var - is always a string enclose in quotes if the string includes spaces.
if [ second_num != 0 ]; then
translated is
if NOT %second_num%==0 ( ...things... ) else (...other things...)
or, of course
if %second_num%==0 ( ...other things... ) else (...things...)
Quirk: (one of many) : the first open-parenthesis MUST occur on the same physical line as the IF and the ELSE must be on the same physical line as the ) of the on-true statemet sequence.
echo first_num "/" second_num "=" $((first_num / second_ num)) >> file.txt
Can't do a calculation in an echo
set /a result=(first_num / second_ num)
OR
set /a result=(%first_num% / %second_ num%)
SET /A applies the results of an arithmetic expression - later addition and more C-like semantics
then
echo %first_num% / %second_num% = %result% >> file.txt
purely stringing the elements together.
Next there is a small problem. During the parsing process, any %var% is replaced by its PARSE-TIME value and THEN the line is executed. Consequently, the ECHO line above would show the values as they stood when the IF statement was entered, not after the calculations.
Two cures:
You can use SETLOCAL ENABLEDELATEDEXPANSION to switch the interpreter mode. In DELAYEDEXPANSION mode, !var! may be used to retrieve the RUN-TIME value of var. A SETLOCAL is terminated by an ENDLOCAL or by reaching END-OF-FILE in the same context. Any environment changes after a SETLOCAL are undone by an ENDLOCAL which is why it's often performed immediately after the #echo off - keeps the environment clean.
second cure is to use a subroutine. CALL :SUB (the colon means 'internal subroutine - start label is in this batchfile'. Omitting it means 'this is an external executable') The CALL creates a new context, copying the then-existing environment variables, so
:sub
echo %first_num% / %second_num% = %result% >> file.txt
goto :eof
will display the variables from the environment as it stood when a CALL :SUB was executed.
Note that GOTO :EOF (the colon is REQUIRED means 'go to physical end-of-file' - the label EOF should not be declared...
(beware also flow-through to subroutines normally placed at the end of batchfiles. A judicious GOTO :EOF ensures that flow-through does not occur...
#Mod 5 of numbers 1-100:
The comments indicator is officially REM
rem Mod 5 of numbers 1-100:
BUT
::Mod 5 of numbers 1-100:
is often used as it's easier to type BUT since it's actually a misuse of a label, it is actually a label, and labels can't be used in a compound statement, you you can't use it within the parentheses of IF ... (...) else (...) or FOR...DO (...)
for i in {1...100}
becomes
for /L %%i in (1,1,100) do (
The metavariable %%i IS case-sensitive and a single character. in a FOR /L the elements are (start,step,end) - see FOR /? from the prompt (or generally command /? from the prompt) for docco...
result=$((i % 5))
becomes
set /a result=%%i %% 5
/a because the RHS is an arithmetic expression to be evaluated; %% 5 because % escapes %, the space is irrelevant and the processor needs to know that the MOD operator % is being used, not %5 (the fifth command-line argument)
int sum=0
No such thing as types; all environment variables are strings and only interpreted as integers by the set /a statement
so - that's about all there is to it...
Hope you're looking for this:
#echo off
setlocal enabledelayedexpansion
> file.txt type nul
:loopInput
echo enter first number:
set /p first_num=
echo enter second number:
set /p second_num=
if !first_num! neq 0 (
if !second_num! neq 999 (
if !second_num! equ 0 (
echo ERROR. Cannot divide by 0. Enter another number:
goto loopInput
)
goto loopDiv
)
)
goto :skipDiv
:loopDiv
set /a division=first_num/second_num
>> file.txt echo !first_num! / !second_num! = !division!
goto loopInput
:skipDiv
echo You have exited the div loop.
>> file.txt echo Mod 5 of numbers 1-100:
for /l %%a in (1,1,100) do (
set /a mod=%%a%%5
>> file.txt echo %%a %% 5 = !mod!
)
set sum=0
for /l %%a in (1,1,100) do (
set /a sum+=%%a%%5
)
set /a average=sum/100
>> file.txt echo Average of results: !average!
I'm new to linux and bash, so I don't know what am I doing wrong. I set up grub to don't show, and show after pushing shift for 3 seconds. This worked fine. Then I changed line in 30_os-prober (as you can see underneath) which contained 'shift' to 'F11' as I read here: http://www.gnu.org/software/grub/manual/grub.html (13.3.33). Now when I press F11 nothing happens, and when I press shift I can see 'Grub is loading', then default OS (Ubuntu) loads without showing grub menu.
This is part of my /etc/default/grub content:
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
GRUB_DEFAULT="Custom Menu"
GRUB_HIDDEN_TIMEOUT=1
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="splash"
GRUB_CMDLINE_LINUX=" splash vga=799 quiet"
and this is (IMHO) crucial part, from /etc/grub.d/30_os-prober:
adjust_timeout () {
#if [ "x${found_other_os}" = "x" ] ; then
if [ "x${GRUB_HIDDEN_TIMEOUT}" != "x" ] ; then
if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then
verbose=
else
verbose=" --verbose"
fi
if [ "x${GRUB_HIDDEN_TIMEOUT}" = "x0" ] ; then
cat <<EOF
if [ \${timeout} != -1 ]; then
if keystatus; then
if keystatus --F11; then << There I changed shift to F11
set timeout=-1
else
set timeout=0
fi
else
if sleep$verbose --interruptible 3 ; then
set timeout=0
fi
fi
fi
EOF
else
cat << EOF
if [ \${timeout} != -1 ]; then
if sleep$verbose --interruptible ${GRUB_HIDDEN_TIMEOUT} ; then
set timeout=0
fi
fi
EOF
fi
fi
#fi
}
Thank you.
The keystatus documentation at the link you provided (and other keystatus documentation that I've come across) indicates that it accepts only --shift --ctrl or --alt as key-specific parameters. Perhaps it does not work for other keys such as F11.
The other thing to be aware of is that keystatus apparently does not work on all platforms. When that is the case, your first keystatus call will return false and the rest of the keystatus conditional logic will be skipped.