When I'm trying to send some message to all terminals through echo "some message" > /dev/pts/* it works good. But when I do the same thing through bash script then error occurrs: myscript.sh: line 2: /dev/pts/*: Permission denied. Even when I set highest privileges to myscript.sh. What can I do to make it work?
read msg
echo $msg > /dev/pts/*
Did you have a look at the wall command?
See http://linux.die.net/man/1/wall
You need privileges to do this, but here is described a workaround
How do I broadcast messages to all bash terminal in python using wall command with stdin?
Related
Out of curiosity, im looking for way of displaying an image when an error like "Permission is denied" appears on terminal
is there like a function or package that can replace this:
for this :
The "Permission denied" error comes from commands that you run and are not able to do their purpose because of lack of permissions.
You call these command from your shell (probably bash) and normally these commands sends their errors to stderr special file handle in Linux.
You could write your own shell (bash is open source), catch these errors and call a command like :
eog error.png
This kind of command will work only if and when you are in the graphic environment.
If you write a shell (let's say you call it myshell and place it in /usr/local/bin) - you could set yourself this shell as default shell by using chsh command (or editing /etc/passwd file as root).
I am writing shell script to install my application. I have more number of commands in my script such as copy, unzip, move, if and so on. I want to know the error if any of the commands fails. Also I don't want to send exit codes other than zero.
Order of script installation(root-file.sh):-
./script-to-install-mongodb
./script-to-install-jdk8
./script-to-install-myapplicaiton
Sample script file:-
cp sourceDir destinationDir
unzip filename
if [ true]
// success code
if
I want to know by using variable or any message if any of my scripts command failed in root-file.sh.
I don't want to write code to check every command status. Sometimes cp or mv command may fail due to invalid directory. At the end of script execution, I want to know all commands were executed successfully or error in it?
Is there a way to do it?
Note: I am using shell script not bash
/* the status of your last command stores in special variable $?, you can define variable for $? doing export var=$? */
unzip filename
export unzipStatus=$?
./script1.sh
export script1Status=$?
if [ !["$unzipStatus" || "$script1Status"]]
then
echo "Everything successful!"
else
echo "unsuccessful"
fi
Well as you are using shell script to achieve this there's not much external tooling. So the default $? should be of help. You may want to check for retrieval value in between the script. The code will look like this:
./script_1
retval=$?
if $retval==0; then
echo "script_1 successfully executed ..."
continue
else;
echo "script_1 failed with error exit code !"
break
fi
./script_2
Lemme know if this added any value to your scenario.
Exception handling in linux shell scripting can be done as follows
command || fallback_command
If you have multiple commands then you can do
(command_one && command_two) || fallback_command
Here fallback_command can be an echo or log details in a file etc.
I don't know if you have tried putting set -x on top of your script to see detailed execution.
Want to give my 2 cents here. Run your shell like this
sh root-file.sh 2> errors.txt
grep patterns from errors.txt
grep -e "root-file.sh: line" -e "script-to-install-mongodb.sh: line" -e "script-to-install-jdk8.sh: line" -e "script-to-install-myapplicaiton.sh: line" errors.txt
Output of above grep command will display commands which had errors in it along with line no. Let say output is:-
test.sh: line 8: file3: Permission denied
You can just go and check line no.(here it is 8) which had issue. refer this go to line no. in vi.
or this can also be automated: grep specific line from your shell script. grep line with had issue here it is 8.
head -8 test1.sh |tail -1
hope it helps.
I've recently started using bash to automate a windows rescue disk with chntpw. I'm trying to set up the program to use the expect command to listen for certain chntpw dialog questions and input the right answers without any user input. For some reason after setting up the bash script to use #!/usr/bin/expect rather than #!/bin/bash then many standard terminal commands are no longer understood.
I'm running the script by typing this into terminal:
user#kali:~/Desktop/projects/breezee$ bash breezee1.sh
The terminal output is as follows:
BREEZEE 1.0
Welcome to BREEZEE
breezee1.sh: line 9: fdisk: command not found
[Select] /dev/:
Here is my code:
#!/usr/bin/expect
clear
echo "BREEZEE 1.0"
echo "Welcome to BREEZEE"
fdisk -l
#list partitions
echo -n "[Select] /dev/:"
#ask user to choose primary windows partition
read sda
clear
echo /dev/$sda selected
umount /dev/$sda
sudo ntfsfix /dev/$sda
sudo mount -t ntfs-3g -o remove_hiberfile /dev/$sda /mnt/
cd /mnt/Windows/System32/config
clear
chntpw -l SAM #list accounts on windows partition
chntpw -u Administrator SAM
#now supply chntpw with values to run the password clear (this answers the prompts)
expect '> '
send '1\r'
expect '> '
send '2\r'
expect '> '
send '3\r'
expect ': '
send 'y\r'
expect '> '
send 'q\r'
expect ': '
send 'y\r'
clear
echo "Operation Successfull!"
chntpw -l SAM #list accounts on windows partition
In short, I'm trying to use standard bash/terminal commands alongside the expect commands. I'm probably going about this all wrong, so please correct me as I've been troubleshooting this for about three days and haven't gotten far :(
When you specify the application that should run your script, you can only use the scripting language that application will understand.
Clearly, Expect is not bash, and does not understand bash commands.
i suggest you separate those two scripts. Write the first part for !#/bin/bash, the second for Expect. Make the first script invoke the second script and redirect it to chntpw.
expect uses tcl not bash. So you can write your script in TCL when you use #!/usr/bin/expect.
For example, echo "BREEZEE 1.0" should be written as:
puts "BREEZEE 1.0"
And you should use exp_send instead of send.
From expect manual:
exp_send is an alias for send. If you are using Expectk or some other variant of Expect in the Tk environment, send is defined by Tk for an entirely different purpose. exp_send is provided for compatibility between environments. Similar aliases are provided for other Expect's other send commands.
Is there a way to execute an if statement if I see a specific output? For example, when the console says "bad interpreter permission denied" I want to execute a command like "dos2unix file_name"?
So the logic will be like the following,
if (output is "bad interpreter permission denied")
{
send dos2unix file_name
}
fi
This is an expect script.
Edit:
Could I do something like this in an expect script?
if (grep -cim1 '^M$' lruload.sh) -eq 1; then
send dos2unix filename
fi
When you say execute the commands, I hope you meant to execute the command in the shell. You can use exec command for this purpose.
I'm not sure where you are interacting. I mean like telnet or ftp or bash. Anyway, under any case, you will be sending a command and expecting a prompt.
send "command 1"
expect "prompt"
send "command 2"
expect {
timeout { puts "timeout happened"}
"bad interpreter per mission denied" {
set result [exec dos2unix <filename>]
}
}
# if need to intact with three application, further use 'send' and 'expect'
You have the result variable to store the dos2unix output.
How about this logic/psuedo-code?
export cmdText=`myCmd param1 param2 2>&1`
if ($cmdText is "bad interpreter permission denied")
{
send dos2unix file_name
}
fi
The permission denied text probably went to stderr, not stdout, so the redirect 2>&1 lumps both of them together, making the test simple.
I have a script I am trying to run with grunt using the grunt-shell plugin. This script prompts for input using read -p "enter foo" bar. When I run it, I get a cursor that correctly takes my input, but I don't see "enter foo".
I tried modifying the grunt-shell config to dump err, stderr, and stdout to the console, but they all come out empty once the command completes. The man page for read says about the -p flag "The prompt is displayed only if input is coming from a terminal.", which seems like it may be my issue since node is calling the prompt, but the read command is executing in my terminal, and I am entering input in a terminal, so why wouldn't that work?
So, my question is, is there any way to prompt for user input from the grunt-shell plugin?
Also, as a P.S., yes i know it would be very easy to just echo the prompt and deal with the input being below it, but dammit it's the principle of the thing.
I redirected my code back to /dev/tty.
Ran it as bash -c run.sh </dev/tty