Printout result to a file - linux

I am running few scripts to check size of files and folders on ubuntu.
How can i make these scripts automatic so that they might run automatically at start of day and printout result to a text file
i am new to scripting
Please reply

To printout to a text file is fairly simple.
I'm assuming you're using python. Correct me if not.
First, open a txt file with write permissions (the second argument of open).
You can then use the .write() method to write to the file.
Finally, it is good practice to close the file afterward.
f = open('file.txt', 'w')
f.write("Hello world")
f.close()
For script automation, you're going to want to read up on cron jobs: http://en.wikipedia.org/wiki/Cron

actually you can redirect du command's output to file, executing by a cron job. The full shell script is :
#!/bin/bash
du -H >> /tmp/yourfile.log
Notes:
look for du's man page for more information on option flags.
You may want to have variables in file name for separating daily logs
This sounds like a serverfault.com question.

Related

Writing a shell script that accepts as command line arguments a series of 3 strings representing file types

So i have a script called process_files, and when a file type is called, a directory should be created based on what was inputted into the command line. So for example, when process_files JPG is entered, a JPG directory should be created. However, when i try to do this it also creates the other directories that are in my code. This is the code.
#!/bin/sh
$jpg mkdir jpg
$gif mkdir gif
$docx mkdir docx
$png mkdir png
I don't think that you'll be able to do it in as short a script as that anyway.
Firstly, you need to refer to the arguments that you have passed in a different manner:
https://www.learnshell.org/en/Passing_Arguments_to_the_Script
Then you need to conditionally compare your input arguments to "jpg"/"gif"/"docx"/"png" strings and run a mkdir command if true:
https://linuxize.com/post/how-to-compare-strings-in-bash/
There are probably fancy shortcuts with the likes of awk but if you are just beginning shell scripting then it's probably better to go down the explicit, iterative route and keep things simple and obvious in early scripts.
There are many online resources for learning shell scripting and they can quickly cover most scenarios and answer most questions.

save output to file instead of screen for nested script

I have a script which calls several functions. These functions contain print commands (to the screen). How can I save the output of the script to a file without printing it to the screen and without changing the code of the functions.
Best,
Wouter
You can use the script command which stores on a file everything written to the screen:
script -c "myscript arguments ..." /tmp/myscript.log
That will at least allow you to get what was output to /dev/tty by the called functions, however, there is no simple way to prevent this output to go on your screen too.

log every linux command line ever used?

so, I'm solid on linux basics, and have never written a shell script. But there is something I would like to get, or do.
Would it be possible to have linux log ALL COMMANDS I EVER TYPE in a single file? I.e., every ls -l and cd /this/folder etc., but also the install records I have done and more.
If this file exists great. If there is a persistent file created for EACH session and user, then maybe I can write a script to conjoin the lines. Or, what other options are available?
I'd like the file to have 3 columns, user executing, datetime executed, and copy of the command string. Some kind of results or error if returned would be great. MANY THANKS from a guy who is amazed what Linux is capable of doing!
Assuming you're running bash, look at your ~/.bash_history file.

Piping SVG file into ImageMagick

Sorry if this belongs on serverfault
I'm wondering what the proper way is to use an SVG(xml) string as standard input
for a "convert msvg:- jpeg:- 2>&1" command (using linux)
Currently I'm just saving a temp file to use as input,
but the data originates from an API in my case, so feeding
the string directly to the command would obviously be most efficient.
I appreciate everyone's help. Thanks!
This should work:
convert - output.jpg
Example:
convert logo: logo.svg
cat logo.svg | convert - logo.jpg
Explanation:
The example's first line creates an SVN file and writes it to disk. This is only a preparatory stop so that we can run the second line.
The second line is a pipeline of two commands: cat streams the bytes of the file to stdout (standard output).
The first line served only as preparation for the next command in the pipeline, so that this next command has something to read in.
This next command is convert.
The - character is a way to tell convert to read its input data not from disk, but from stdin (standard input).
So convert reads its input data from its stdin and writes its JPEG output to the file logo.jpg.
So my first command/line is similar to your step described as 'currently I'm just saving a temp file to use as input'.
My second command/line does not use your API (I don't have access to it, do I?), but it demonstrates a different method to 'feeding a string directly to the command'.
So the most important lesson is this: Whereever convert would usually read input from a file and where you would write the file's name on the commandline, you can replace the filename by - to tell convert it should read from stdin. (But you need to make sure that there is actually something offered on convert's standard input which it can digest...)
Sorry, I can't explain better than this...

Getting linux terminal value from my application

I am developing a Qt application in Linux. I wanted to pass Linux commands to a terminal. That worked but now i also want to get a response from the terminal for this specific command.
For example,
ls -a
As you know this command lists the directories and files of the current working directory. I now want to pass the returned values from the ls call to my application. What is a correct way to do this?
QProcess is the qt class that will let you spawn a process and read the result. There's an example of usage for reading the result of a command on that page.
popen() , api of linux systerm , return FILE * that you can read it like a file descriptor, may help youp erhaps。
Parsing ls(1) output is dangerous -- make a few files with funny names in a directory and test it out:
touch "one file"
touch "`printf "\x0a\x0a\x0ahello\x0a world"`"
That creates two files in the current working directory. I expect your attempts to parse ls(1) output won't work. This might be alright if you're showing the results to a human, (though a human will be immensely confused if a filename includes output that looks just like ls(1) output!) but if you're trying to present something like an explorer.exe or Finder.app representation of files in the filesystem, this is horribly broken.
Instead, use opendir(3), readdir(3), and closedir(3) to read directory entries yourself. This will be safer, more portable, and (as a side benefit) slightly better performing.

Resources