Well I've written a bat file which is used to load my pokemon backups for when I'm away from my gameboys, however I've converted to linux and I'm having issues getting the .BAT file in my exe (I've decompiled the exe back the the source) to work as a .SH and I can't really find much on how to use shell commands as the same function they would be in a BAT file :/ I would also love to know how to set the SH file to load out of the current directory AND run said program in wine.
Here is my .BAT file which works 100% perfectly under windows but refuses to run under wine or a CMD prompt portable under wine
`:MENU
CLS
ECHO ============= RawX GBA's =============
ECHO -------------------------------------
ECHO 1. Pokemon Crystal
ECHO 2. Pokemon Green
ECHO 3. Pokemon Gold
ECHO 4. Pokemon Pikachu
ECHO 5. Pokemon Ruby
ECHO 6. Pokemon Chaos Black
Echo 7. Pokemon Silver
ECHO 8. Pokemon White (NDS)
ECHO 9.
Echo 10.
Echo 11.
Echo 12. Pokemon Black (NDS)
ECHO ==========PRESS 'Q' TO QUIT==========
ECHO.
color fc
SET INPUT=
SET /P INPUT=Please select a number:
IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='4' GOTO Selection4
IF /I '%INPUT%'=='5' GOTO Selection5
IF /I '%INPUT%'=='6' GOTO Selection6
IF /I '%INPUT%'=='7' GOTO Selection7
IF /I '%INPUT%'=='8' GOTO Selection8
IF /I '%INPUT%'=='9' GOTO Selection9
IF /I '%INPUT%'=='10' GOTO Selection10
IF /I '%INPUT%'=='11' GOTO Selection11
IF /I '%INPUT%'=='12' GOTO Selection12
IF /I '%INPUT%'=='Q' GOTO Quit
CLS
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection1
:1
".\VisualBoyAdvance.exe" ".\Pokemon Crystal.zip"
goto menu
:Selection2
:2
".\VisualBoyAdvance.exe" ".\Pokemon Green.zip"
goto menu
:Selection3
".\VisualBoyAdvance.exe" ".\Pokemon Gold.zip"
goto menu
:Selection4
".\VisualBoyAdvance.exe" ".\Poke'mon Pikachu.zip"
goto menu
:Selection5
".\VisualBoyAdvance.exe" ".\Pokemon Ruby.zip"
goto menu
:Selection6
".\VisualBoyAdvance.exe" ".\Pokemon - Chaos Black.zip"
goto menu
:Selection7
".\VisualBoyAdvance.exe" ".\Pokemon Silver.zip"
goto menu
:Selection8
".\desmume.exe" ".\PokeWhite.zip"
goto menu
:Selection12
".\desmume.exe" ".\PokeBlack.zip"
goto menu
:Quit
CLS
ECHO ==============THANK YOU===============
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE>NUL
EXIT`
The conversion to Bourne shell is pretty straightforward:
Add #!/bin/sh or similar as the first line, to tell Linux which interpreter to use. Unlike on Windows, there's more than one command shell, and more script interpreters than just shells besides.
The equivalent of the cmd.exe command CLS is clear on Linux.
Linux is case-sensitive, so all your ECHOs have to be lowercase. If I'd kept your IF statements in the translated version, they'd have to be lowercase, too.
echo in shell scripts doesn't just print everything literally to the end of the line, as in cmd.exe. Bourne shell is a much richer language: it can apply meaning to what appears after a command before sending it to the command. In your code, the single quotes (') and multiple spaces won't survive this command processing.
To avoid problems of this sort, I've double-quoted all of your echo strings. I could have done it selectively, double-quoting only the problem strings, but chose to double-quote everything for consistency. I don't want you to get the mistaken idea that echo in Bourne shell requires the double-quotes.
If I wasn't interested in keeping the translation simple, so you can see more 1:1 correspondences between batch files and shell scripts, I'd replace the two big blocks of echo commands with a heredoc.
echo. is just echo in Bourne shell. You don't need the dot in Bourne shell because echo in Bourne shell isn't overloaded to turn command echoing on and off, as with ECHO ON/OFF in cmd.exe. (Bourne shell does have a similar feature, enabled via set -x.)
It is possible to get colored output in Bourne shell, but there is no simple built-in command for it as in cmd.exe. If you want pretty colored menus, you can replace much of the code in this script with a call to dialog(1).
You use read to get input in a shell script; set does other things.
There is no goto in Bourne shell. We don't need it, because Bourne shell has decent control structures. I think a case statement expresses the intent of your inner selection code, and an infinite while loop expresses the outer "keep doing this until they hit q" scheme.
I don't see how code flow gets to your "press any key to continue" bit at the end, so I removed it. If I'm wrong, the rough equivalent of PAUSE is read -n 1 -s.
I have changed the calls to the external programs, dropping the .exe and changing .\ to ./ to match the way things are done on Linux. You still need to come up with Linux equivalents of VisualBoyAdvance.exe and desmume.exe.
The result looks something like this:
#!/bin/sh
clear
while true do
echo "============= RawX GBA's ============="
echo "-------------------------------------"
echo "1. Pokemon Crystal"
echo "2. Pokemon Green"
echo "3. Pokemon Gold"
echo "4. Pokemon Pikachu"
echo "5. Pokemon Ruby"
echo "6. Pokemon Chaos Black"
echo "7. Pokemon Silver"
echo "8. Pokemon White (NDS)"
echo "9."
echo "10."
echo "11."
echo "12. Pokemon Black (NDS)"
echo "==========PRESS 'Q' TO QUIT=========="
echo
echo -n "Please select a number: "
read input
case $input in
1)
./VisualBoyAdvance "Pokemon Crystal.zip"
;;
2)
./VisualBoyAdvance "Pokemon Green.zip"
;;
# etc.
q)
clear
exit
*)
clear
echo "============INVALID INPUT============"
echo "-------------------------------------"
echo "Please select a number from the Main"
echo "Menu [1-12] or select 'Q' to quit."
echo "-------------------------------------"
echo "======PRESS ANY KEY TO CONTINUE======"
esac
done
Related
I should put ENTER key value as a shell input to avoid shell waiting for user input.
echo 'enter'
read FRUIT
case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
;;
"banana") echo "I like banana nut bread."
;;
"kiwi") echo "New Zealand is famous for kiwi."
;;
esac
export TEST6=TEST
I have learned that 'echo' implicitly has an ENTER value, so tried to run below command to accomplish my requirement.
echo | source ~/.bash_profile
As I expected, I can see the OS prompt instead of seeing Shell wait the user input.
However, I noticed that the last line exporting TEST6 variable doesn't get exported. I mean I can't find the value of the variable TEST6.
Can you let me know what I am missing?
The value is being exported in a subshell run by the pipeline. Use input redirection instead.
source ~/.bash_profile <<< ""
Any string could be used, since you don't appear to care about the actual value used, but an empty string is the equivalent of what echo with no arguments produces. (All here strings implicitly end with a newline.)
If your shell does not support <<<, you can use a POSIX-standard here document instead.
. ~/some_file <<EOF
EOF
I was wondering if you could help me with something.
I want to write a menu for one of the scripts I'm working on. Currently, my script is:
echo "########################################"
echo "# Script tasks #"
echo "# #"
echo "# 1 Show running processes #"
echo "# 2 Show logged in users #"
... (continued)
echo "########################################"
However, when I run this inside of my script, for some reason some of the # signs at the end of the line get either indented into the box, or pushed further out, resulting in the right side of the box looking very bad and not well thought-out. I would like the right side of the box to look even (i.e., actually like a box).
I'm using Bash on Ubuntu 14.04 LTS and gedit as my text editor.
This isn't your question, but the way to do menus in a shell script is with the select command
# an array with the menu choices
choices=(
"Show running processes"
"Show logged in users"
"..."
)
# define the prompt
PS3="What is your choice? "
# display and get user input
# select is an infinite loop: `break` when you've done something successfully
select choice in "${choices[#]}"; do
case "$choice" in
"Show running processes")
some code here
break
;;
"Show logged in users")
some code here
break
;;
*)
echo "Please select a number from the menu."
# do not break, select will re-display
;;
esac
done
# if you need the choice afterward, you still have it
echo "$choice"
I have this script start.sh
#!/bin/bash
while[1]
do
read -sn3 key
if [$key=="\033[[A"]
then
./test1
else
./test2
fi
done
I want to set up a forever loop check see if F1 key pressed. If pressed execute test1 else test2. I did start.sh & running in background so other programs can run.
I got error
while [1] command not found
syntax error near unexpected token 'do'
[f==\033]: command not found
Also where is this read command located? I type which read, it didn't find it.
Also, if try ./start.sh & it gives totally different behavior. I enter a key and it says that key is not found. I though & just run the script at background
There are several basic syntax problems in your code (consider using shellcheck before posting to clean up these things), but the approach itself is flawed. Hitting "q" and "F1" produces different length inputs.
Here's a script relying on the fact that escape sequences all come in the same read call, which is dirty but effective:
#!/bin/bash
readkey() {
local key settings
settings=$(stty -g) # save terminal settings
stty -icanon -echo min 0 # disable buffering/echo, allow read to poll
dd count=1 > /dev/null 2>&1 # Throw away anything currently in the buffer
stty min 1 # Don't allow read to poll anymore
key=$(dd count=1 2> /dev/null) # do a single read(2) call
stty "$settings" # restore terminal settings
printf "%s" "$key"
}
# Get the F1 key sequence from termcap, fall back on Linux console
# TERM has to be set correctly for this to work.
f1=$(tput kf1) || f1=$'\033[[A'
while true
do
echo "Hit F1 to party, or any other key to continue"
key=$(readkey)
if [[ $key == "$f1" ]]
then
echo "Party!"
else
echo "Continuing..."
fi
done
Should be
while :
or
while true
Try this:
#!/bin/bash
while true
do
read -sn3 key
if [ "$key" = "$(tput kf1)" ]
then
./test1
else
./test2
fi
done
It is more robust to use tput to generate the control sequence, you can see a full list in man terminfo. If tput isn't available, you can use $'\eOP' for most terminal emulators or $'\e[[A' for the Linux console (the $ is necessary with the string to make bash interpret escape sequences).
read is a bash builtin command - try help read.
I'm trying to set an environmental variable that will persist once the script has finished running. It can go away after I end an ssh session.
Sample bash script:
# User picks an option
1) export dogs = cool
2) export dogs = not_cool
Running the script as source script.sh doesn't work since it kicks me out of my shell when ran and also requires the interactive menu so it won't work. Basically I want the user to be able to pick an option to switch between environmental variables in their shell. Is that even possible?
Source:
#!/bin/bash
set -x
show_menu(){
NORMAL=`echo "\033[m"`
MENU=`echo "\033[36m"` #Blue
NUMBER=`echo "\033[33m"` #yellow
FGRED=`echo "\033[41m"`
RED_TEXT=`echo "\033[31m"`
ENTER_LINE=`echo "\033[33m"`
echo -e "${MENU}*********************************************${NORMAL}"
echo -e "${MENU}**${NUMBER} 1)${MENU} Option 1 ${NORMAL}"
echo -e "${MENU}**${NUMBER} 2)${MENU} Option 2 ${NORMAL}"
echo -e "${MENU}*********************************************${NORMAL}"
echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}"
read opt
}
function option_picked() {
COLOR='\033[01;31m' # bold red
RESET='\033[00;00m' # normal white
MESSAGE=${#:-"${RESET}Error: No message passed"}
echo -e "${COLOR}${MESSAGE}${RESET}"
}
clear
show_menu
while [ opt != '' ]
do
if [[ $opt = "" ]]; then
exit;
else
case $opt in
1) clear;
option_picked "Option 1";
export dogs=cool
show_menu;
;;
2) clear;
option_picked "Option 2";
export dogs=not_cool
show_menu;
;;
x)exit;
;;
\n)exit;
;;
*)clear;
option_picked "Pick an option from the menu";
show_menu;
;;
esac
fi
done
The problem here is that . ./script.sh or source ./script.sh cannot run an interactive menu style script like this one. No way that I'm aware of to set local environmental variables from a bash script like I am trying to do here.
Redirect your normal echo's for user interaction to stderr (>&2)
echo the value that you want to have in your parent's environment to stdout (>&1)
if you changed your script that way you can call it like:
ENV_VAR=$( /path/to/your_script arg1 arg2 arg3 arg_whatever )
and now you have "exported" a variable to the "parent"
Try running your script with "./myscript.sh" which will use the current shell without invoking the new shell (still i doubt the hash bang might invokes the new shell).
Have a look here
Can also be solved with ~/.bashrc. The required environments can added in this file. If you need new shell with your own environment, you invoke "bash" with "bash --rcfile ".
I just upgrade to Ubuntu Karmic 9.10 and there is now emphaty.
The problem is that em-TAB- doesn't autocomplete to emacs anymore ...
I there a way to the my Bourne-Shell to complete em-TAB- always to emacs ?
Thank you
If you add an alias for em to your .bashrc you can skip the Tab-completion all together:
alias em=emacs
With this you can start emacs by simply typing the command em.
I use another approach - simple e command:
#!/bin/sh
# Written by Oleksandr Gavenko , 2008.
# File placed by author in public domain.
# View file in emacs buffer using emacsclient.
# If emacs not already running, run it.
# Put this file 'e' in your PATH.
# Name `e' because `edit'.
case "$1" in
-h|-help|--help)
echo "Emacsclient run script."
echo "Usage:"
echo " e [-h|--help] file..."
exit 0
;;
"")
echo "What file you want to open?"
exit 0
;;
esac
if [ -n "$COMSPEC" ]; then
# We probably under Windows like OS. I like native Emacs over Cygwin.
for arg; do
emacsclientw -a emacs -n "$arg"
done
else
# We under UNIX like OS.
for arg; do
emacsclient -a emacs -n "$arg"
done
fi
Also I write similar script in batch file (.bat) language for MS Windows:
#echo off
REM Written by Oleksandr Gavenko , 2008.
REM File placed by author in public domain.
REM View files in emacs buffer using emacsclientw.
REM If emacs not already running, run it.
REM Put this file (e.bat) in your PATH.
REM Name `e' because `edit'.
REM If path to file contain spaces it must be inclosed into quotes.
if x%1 == x-h goto usage
if x%1 == x-help goto usage
if x%1 == x--help goto usage
:loop
emacsclientw -a runemacs -n %1
shift
if not x%1 == x goto loop
goto :eof
:usage
#echo Alias for emacsclient without waiting for editing ending.
#echo Usage:
#echo e [-h^|--help] file...