how to create and send output directly to printer using do shell script - applescript

I am trying to get a shell script that works in the sh shell to work as an applescript do shell script command. The script simply takes a series of elp2 commands and pipes them to lpr for printing. This works when typed directly into the shell, but when run in applescript it does not print. The printer is receiving data but it is not formated correctly so nothing prints. I believe the issue is with the way the quotes are escaped. Here is the code:
do shell script "{ echo N
echo OD10
echo q812
echo Q1218,24
echo D15
echo ZT
echo A70,40,0,5,3,3,N,\\'F\\'
echo A610,70,0,3,1,1,N,\\'U.S.\\'
echo A590,95,0,3,1,1,N,\\'POSTAGE\\'
echo A580,120,0,3,1,1,N,\\'REQUIRED\\'
echo A43,240,0,4,2,1,N,\\'USPS FIRST-CLASS MAIL\\'
echo A43,300,0,3,1,1,N,\\'Name\\'
echo A43,325,0,3,1,1,N,\\'street\\'
echo A43,350,0,3,1,1,N,\\'city, VA 12345\\'
echo A43,400,0,3,1,1,N,\\'ADDRESS SERVICE REQUESTED\\'
echo A140,490,0,4,1,1,N,\\'ship to\\'
echo A140,520,0,4,1,1,N,\\'company\\'
echo A140,550,0,4,1,1,N,\\'address\\'
echo A140,580,0,4,1,1,N,\\'city, state, 12345\\'
echo A140,610,0,4,1,1,N,\\'country\\'
echo A140,640,0,4,1,1,N,\\'\\'
echo LO10,10,760,4
echo LO10,210,760,2
echo LO10,275,760,2
echo LO10,750,760,10
echo LO10,1050,760,10
echo LO10,1185,760,4
echo LO10,10,4,1175
echo LO770,10,4,1175
echo LO210,10,2,200
echo LO540,45,200,2
echo LO540,45,2,125
echo LO540,170,200,2
echo LO740,45,2,125
echo P1
echo N;} | lpr -P Label -o raw"

One way you can check how to write something in applescript is to write the normal unescaped text into a text file. Then you read the file into applescript. What you see in the result field is how you should write it. You can then copy/paste the result into your applescript.
For example, if I make a text file on my desktop called myText.txt with this inside...
echo A70,40,0,5,3,3,N,\'F\'
echo A610,70,0,3,1,1,N,\'U.S.\'
Then I use this applescript...
set f to (path to desktop as text) & "myText.txt"
read file f
My result is...
"echo A70,40,0,5,3,3,N,\\'F\\'
echo A610,70,0,3,1,1,N,\\'U.S.\\'"
So give that technique a try with your entire code. Good luck.

Related

Bash Scripting - User Input a Command

Very easy question but I really can't find this when I Google. Sorry!
I'm trying to write a script that runs a user's command that he or she enters but I can't run the command that the user enters.
#!/bin/bash
echo -e "Enter a Command: "
read $COMMAND
echo "Output: $COMMAND" # I can't figure how to implement and print the command
Enter a Command: ls
Output: folder1 folder2 folder3 test.txt)
All you need is to delete the dollar sign from the read command
#!/bin/bash
echo "Enter a Command: "
read COMMAND
echo "Output: $COMMAND"
Happy scripting!, please don't forget to marked as answered ;)
Use eval to execute a command from a variable.
Also, you don't put $ before the variable name in the read command. That's only used when you want to get the variable's value.
#!/bin/bash
echo -e "Enter a Command: "
read COMMAND
echo Output:
eval "$COMMAND"
DEMO
Thanks! This answered my question:
#!/bin/bash
echo -e "Enter a Command: "
read COMMAND
echo Output:
eval "$COMMAND"

Read from .txt file containing executable commands to be executed w/ output of commands executed sent to another file

When I run my script, the .txt file is read, the executable commands are assigned to $eggs, then to execute the commands and redirect the output to a file I use echo $eggs>>eggsfile.txt but when I cat the file, I just see all the commands and not the execution output of those commands.
echo "Hi, $USER"
cd ~/mydata
echo -e "Please enter the name of commands file:/s"
read fname
if [ -z "$fname" ]
then
exit
fi
terminal=`tty`
exec < $fname #exec destroys current shell, opens up new shell process where FD0 (STDIN) input comes from file fname
count=1
while read line
do
echo $count.$line #count line numbers
count=`expr $count + 1`; eggs="${line#[[:digit:]]*}";
touch ~/mydata/eggsfile.txt; echo $eggs>>eggsfile.txt; echo "Reading eggsfile contents: $(cat eggsfile.txt)"
done
exec < $terminal
If you just want to execute the commands, and log the command name before each command, you can use 'sh -x'. You will get '+ command' before each command.
sh -x commands
+pwd
/home/user
+ date
Sat Apr 4 21:15:03 IDT 2020
If you want to build you own (custom formatting, etc), you will have to force execution of each command. Something like:
cd ~/mydata
count=0
while read line ; do
count=$((count+1))
echo "$count.$line"
eggs="${line#[[:digit:]]*}"
echo "$eggs" >> eggsfile.txt
# Execute the line.
($line) >> eggsfile.txt
done < $fname
Note that this approach uses local redirection for the while loop, avoiding having to revert the input back to the terminal.

Issue exporting bash shell output to text file

Below is my code:
#!/bin/bash
echo 1. date
date
echo -e "\n"
echo 2. hostname
hostname
echo -e "\n"
echo 3. arch
arch
echo -e "\n"
echo 4. uname -a
uname -a
echo -e "\n"
echo 5.uptime
uptime
echo -e "\n"
echo 6. whoami
whoami
echo -e "\n"
echo 7. who
who
echo -e "\n"
echo 8. last
last
echo -e "\n"
echo 9. finger
finger
echo -e "\n"
echo 10. w
w
echo -e "\n"
echo 11. top
top -n 1
echo -e "\n"
echo 12. history
tail -100 ${HOME}/.bash_history
echo -e "\n"
echo End of Script!
when I run it works fine and displays all the code on screen, however when I save to txt file using ./keithbrazill.sh > keithbrazill.txt, it does not show all the text output and only part output. Any advice? I tried a few different variations of output command such as &>, >> etc.
Here's an easier way to reproduce the problem:
echo "Hello"; whoami # Shows output from both commands
echo "World"; top -n 1 # Shows output only from top
This happens because top is a ncurses program that outputs terminal control sequences to draw itself fullscreen, overwriting anything already on screen. While all the data does end up in the file, it'll show a lot of gibberish in a text editor, and if you cat it, the terminal sequences will overwrite the output of other commands.
You can use top -b -n 1 to instead get plaintext output.

How do I programmatically execute a carriage return in bash?

My main file is main.sh:
cd a_folder
echo "executing another script"
source anotherscript.sh
cd ..
#some other operations.
anotherscript.sh:
pause(){
read -p "$*"
}
echo "enter a number: "
read number
#some operation
pause "Press enter to continue..."
I wanted to skip the pause command. But when I do:
echo "/n" | source anotherscript.sh
It doesn't allow to enter the number. I want the "/n" to occur so that I allow the user to enter a number but skip the pause statement.
PS: can't do any changes in anotherscript.sh. All changes to be done in main.sh.
Try
echo | source anotherscript.sh
Your approach does not work, because the script to be sourced expectes two lines from stdin: First a line containing a number, then an empty line (which is doing the pause). Hence you would have to feed two lines, the number and the empty line, to the script. If you still want to get the number from your own stdin, you would have to use a read command before:
echo "executing another script"
echo "enter a number: "
read number
printf "$number\n\n" | source anotherscript.sh
But this still has some danger lurking: The source command is executed in a subshell; hence, any changes in the environment performed by anotherscript.sh won't be visible in your shell.
A workaround would be to be to put the number-reading logic outside of main.sh:
# This is script supermain.sh
echo "executing another script"
echo "enter a number: "
read number
printf "$number\n\n"|bash main.sh
where in main.sh, you simply keep your source anotherscript.sh without any piping.
As user1934428 comments, the bash pipeline causes the cascading
commands to be executed in subshells and the variable modifications
there are not reflected in the current process.
To change this behavior, you can set lastpipe with shopt builtin.
Then bash changes the job control so that the last command in the
pipeline is executed in the current shell (as tsch does).
Then would you please try:
main_sh
#!/bin/bash
shopt -s lastpipe # this changes the job control
read -p "enter a number: " x # ask for the number in main_sh instead
cd a_folder
echo "executing another script"
echo "$x" | source anotherscript.sh > /dev/null
# anotherscript.sh is executed in the current process
# unnecessary messages are redirected to /dev/null
cd ..
echo "you entered $number" # check the result
#some other operations.
which will properly print the value of number.
Alternatively you can also say as:
#!/bin/bash
read -p "enter a number: " x
cd a_folder
echo "executing another script"
source anotherscript.sh <<< "$x" > /dev/null
cd ..
echo "you entered $number"
#some other operations.

Meta refresh (refresh only browser)

Can someone please tell me when I do "meta refresh" like below,will that also run bash program-run-tmp-directory3.sh &> stdout.out & again? OR, only the browser will be refreshed keeping the apache alive?
"pid" is the "process ID" of the program run in the background.
If this code below also reruns the program bash program-run-tmp-directory3.sh &> stdout.out &. Please let me know how can I avoid it?
#!/bin/sh
echo "Content-type: text/html"
echo ""
bash program-run-tmp-directory3.sh &> stdout.out &
pid=$!
if [[ `ps -p $pid | wc -l` -gt 1 ]]
then
output="Program is running. Running time depends on the number of alternatively spliced proteins the submitted gene has. Results will be displayed here."
echo "<html>"
echo "<head>"
echo "<meta http-equiv=\"refresh\" content=\"10\"/>"
echo "</head>"
echo "<body>"
echo "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
echo "<tr><td><img src=\"../../images/calc.gif\" align=\"absmiddle\"> <strong> $output </strong></td></tr>"
echo "</table>"
echo "</body>"
echo "</html>"
fi
Thanks.
At this point, it seems like your question is how to detect within a bash script that another script is running and avoid respawning it. A quick and dirty method that is often good enough is to grep the output of ps for the command-line of your script. This is slightly complicated by the fact that, depending on the options you use for ps, it may also display the grep process command-line, which obviously also includes the script's command-line as part of the grep pattern. One of many ways to fix this is here. All this explanation is longer than the actual script.
One more note. Just want to make sure you understand what the bash construct $! means:
($!) Expands to the process ID of the job most recently placed into
the background, whether executed as an asynchronous command or using
the bg builtin (see Job Control Builtins).
So, that's only going to refer to things that the current execution of your CGI script knows about. When your browser decides to refresh again, that sends another HTTP GET to your server, which once again spawns your CGI script, at which point $! only refers to the job most recently spawned by that instance of your script.
If I intuit what you're trying to do, you might want something like this (untested):
#!/bin/sh
echo "Content-type: text/html"
echo ""
# if other script not already running
if ! ps aux | grep "[b]ash.*program-run-tmp-directory3.sh"
then
bash program-run-tmp-directory3.sh &> stdout.out &
# I'm superstitious; let's give it a moment to start
sleep 1
fi
if ps aux | grep "[b]ash.*program-run-tmp-directory3.sh"
then
output="Program is running. Running time depends on the number of alternatively spliced proteins the submitted gene has. Results will be displayed here."
echo "<html>"
echo "<head>"
echo "<meta http-equiv=\"refresh\" content=\"10\"/>"
echo "</head>"
echo "<body>"
echo "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
echo "<tr><td><img src=\"../../images/calc.gif\" align=\"absmiddle\"> <strong> $output </strong></td></tr>"
echo "</table>"
echo "</body>"
echo "</html>"
else
: # ... some useful error output composed as HTML
fi

Resources