Here I created a shell script to run another script in terminal and also I need to keep a log file, I knew 'script' command is used to track all user inputs and output. Then how can I use 'script' command in below lines.
#!/bin/bash
gnome-terminal --full-screen -x ./user_script_file.sh
I need to use script user_screen.log in above code. Please help me to find a solution,
gnome-terminal --full-screen -x script mylogfile.txt -c ./user_script_file.sh
-x can take many arguments so there is no need to quote.
Related
When write #!/bin/bash -x in the first line of a bash file (e.g. test.sh), I expect to see some execution information but get nothing... Moreover, when I use the command of bash -x test.sh to run the bash file, it will show information.
Any help for what is going on?
Thanks a lot!
To set debugging to on within your script you will need to use set -x and so:
#!/bin/bash
set -x
.......
The shebang only applies when you execute the file directly:
myfile
./myfile
/path/to/myfile
If you instead execute an interpreter, that interpreter will be used instead, whether or not it's correct and compatible for the script. The best practice is therefore to avoid either of:
bash myfile
sh myfile
I am trying to run a single command using bash in a sh script. There is no way to use bash for the script, I have to use sh. However, I need to run a bash-only command in sh.
Basically, I want something like the following:
bash --command_in "echo foobar"
Is this possible? I don't want to make a second script file just to run that one command in bash (like bash my_script.bash).
Derp, it's the -c flag. This wasn't easy to Google, and the --help is prety brief.
Yes. I know I shouldn't be using it for scripting. Yes. It is rubbish. I am indeed a fool.
Nevertheless.
I'd like to see the commands the script is executing, for debugging purposes. I think you get this effect with set -x or set -v in bash scripts.
So e.g., if the script were
#!/bin/tcsh
echo "Hello"
then when it ran, I'd see
echo "Hello"
Hello
Put:
set echo
and/or
set verbose
at the top of your script. That will print out everything that happens in your script and are equivalent to the bash set -x and set -v commands.
Lets say your script name is tcsh_file
Lets assume this file includes shebang as well.
now run this command on terminal
tcsh -x tcsh_file
this will print every line before executing it. it's basically an interactive mode of execution.
Is this what you needed?
I have a script that connects to an FTP server and while doing so prints out a load of junk.
Is there a way to 'mute' the ouput of the script, similar to windows #echoff? I saw on another question very similar to mine, not the same though that you could use scriptname >/dev/null. When I put this in my program, obviously replacing scriptname with the name of the script, I got a command not found error message.
Does anyone have any idea why this might be happening.
If you mean you want to redirect all output to /dev/null from within the script, rather than when you execute the script, you can do this:
#!/bin/bash
echo Hi
and run that, like this
./script
then do this
#!/bin/bash
exec 1> /dev/null
echo Hi
and run it again exactly the same way, and see that the output is discarded.
You wouldn't put that in your program, but rather, would use it when calling your program. On the command line:
$ scriptname >/dev/null
I'm still new to Unix. Is it possible to run multiple commands of Unix in one time? Such as write all those commands that I want to run in a file, then after I call that file, it will run all the commands inside that file? or is there any way(or better) which i do not know?
Thanks for giving all the comments and suggestions, I will appreciate it.
Short answer is, yes. The concept is known as shell scripting, or bash scripts (a common shell). In order to create a simple bash script, create a text file with this at the top:
#!/bin/bash
Then paste your commands inside of it, one to a line.
Save your file, usually with the .sh extension (but not required) and you can run it like:
sh foo.sh
Or you could change the permissions to make it executable:
chmod u+x foo.sh
Then run it like:
./foo.sh
Lots of resources available on this site and the web for more info, if needed.
echo 'hello' && echo 'world'
Just separate your commands with &&
We can run multiple commands in shell by using ; as separator between multiple commands
For example,
ant clean;ant
If we use && as separator then next command will be running if last command is successful.
you can also use a semicolon ';' and run multiple commands, like :
$ls ; who
Yep, just put all your commands in one file and then
bash filename
This will run the commands in sequence. If you want them all to run in parallel (i.e. don't wait for commands to finish) then add an & to the end of each line in the file
If you want to use multiple commands at command line, you can use pipes to perform the operations.
grep "Hello" <file-name> | wc -l
It will give number of times "Hello" exist in that file.
Sure. It's called a "shell script". In bash, put all the commands in a file with the suffix "sh". Then run this:
chmod +x myfile.sh
then type
. ./myFile
or
source ./myfile
or just
./myfile
To have the commands actually run at the same time you can use the job ability of zsh
$ zsh -c "[command1] [command1 arguments] & ; [command2] [command2 arguments]"
Or if you are running zsh as your current shell:
$ ping google.com & ; ping 127.0.0.1
The ; is a token that lets you put another command on the same line that is run directly after the first command.
The & is a token placed after a command to run it in the background.