Python: how to run a command while using a defined variable? - file

I'm a beginner with Python and I'm trying to list the contents of a directory which is defined as a variable but to no avail.
This is the code:
#!/usr/bin/python
import os
location = "/home/itaig/testdir"
command = os.system('ls -l')," location"
My aim is to count the number of files in the location and print the number.
How can it be achieved?
Edit #1:
In bash I'd do ls -l $location | wc -l , what would be the equivalent in Python?
In any case, I've looked at the links from the comments but wasn't able to get it to work... can you please show me an example?
Thanks

You can use os.listdir.
This is platform independent as the os module will handle the low level work
print len(os.listdir(location))

You can use the good old os.popen function:
p = os.popen('ls -l %s' % location)
nb_lines = 0
while p.readline():
nb_lines += 1

Related

Run C program from shell script [duplicate]

I have a script in unix that looks like this:
#!/bin/bash
gcc -osign sign.c
./sign < /usr/share/dict/words | sort | squash > out
Whenever I try to run this script it gives me an error saying that squash is not a valid command. squash is a shell script stored in the same directory as this script and looks like this:
#!/bin/bash
awk -f squash.awk
I have execute permissions set correctly but for some reason it doesn't run. Is there something else I have to do to make it able to run like shown? I am rather new to scripting so any help would be greatly appreciated!
As mentioned in #Biffen's comment, unless . is in your $PATH variable, you need to specify ./squash for the same reason you need to specify ./sign.
When parsing a bare word on the command line, bash checks all the directories listed in $PATH to see if said word is an executable file living inside any of them. Unless . is in $PATH, bash won't find squash.
To avoid this problem, you can tell bash not to go looking for squash by giving bash the complete path to it, namely ./squash.

ls not working well with nohup

I want to list the number of files in a directory in shell-script.
This command works well:
let number_of_files=`ls $direc -l| wc -l`
My problem is that when I use this command with nohup, it doesn't work well.
The same happens when trying to get a file:
file_name=` ls -1 $direc | head -$file_number | tail -1`
Do you know any other option to do it?
I know that in c there is a function:
num_of_files=scandir(directory,&namelist,NULL,NULL);
I also include the full command-line:
nohup sh script_name.sh > log.txt &
Do you know any other way in shell-script that works well with nohup?
Thanks.
Try something like this,
NUMBER_OF_FILES=$(find . -maxdepth 1 -type f | wc -l)
echo $NUMBER_OF_FILES
That is find (from the current directory) to a max depth of 1 (e.g. the current directory only) everything that is of type "file", and then count the number of lines. Finally, assign the result of that to NUMBER_OF_FILES.

Exact command for starting a batch file by using powershell

I know this question has been asked before and I found a thread on here which almost gives me the solution I need.
Here is the link: How to run batch file using powershell
But this only works when I write out the full path. For example:
c:\Users\Administrator\Desktop\dcp_bearbeitet\start.bat -p c:\Users\Administrator\Desktop\dcp_bearbeitet\start.prop
What I want to reach is a solution which accepts a path with parameters, like this one here:
c:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.bat -p c:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.prop
Whereas $title contains the name of my file which I am using in this case. I know that I can create another parameter for the -p command and I know that this works, but unfortunately when I try the same method for the first command I always get an error message.
I hope you guys know a way to solve this problem.
I think Invoke-Expression could help here.
Just construct your path like you want it to be, for example:
$title = "file"
$path = "C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.bat -p c:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.prop"
and then invoke it:
Invoke-Expression $path
Regards Paul

Redirect lldb output to file

I'm using lldb inside Xcode, and one of my variables contains a huge chunk of JSON data. Using po myVar isn't much helpful to analyse this data, as it will output in the tiny Xcode debug console.
Is there a way to redirect lldb output to a file ?
I saw here that such a feature seems to be available on gdb as :
(gdb) set logging on
(gdb) set logging file /tmp/mem.txt
(gdb) x/512bx 0xbffff3c0
(gdb) set logging off
and is "translated" in lldb as :
(lldb) memory read --outfile /tmp/mem.txt --count 512 0xbffff3c0
(lldb) me r -o/tmp/mem.txt -c512 0xbffff3c0
(lldb) x/512bx -o/tmp/mem.txt 0xbffff3c0
However, the memory read command will not help in my case, and apparently, --outfile is not available for the print command.
You can use a Python script to do so (and much more), as explained here:
LLDB Python scripting in Xcode
Create a file named po.py in a directory of your choice (for example "~/.lldb"):
import lldb
def print_to_file(debugger, command, result, dict):
#Change the output file to a path/name of your choice
f=open("/Users/user/temp.txt","w")
debugger.SetOutputFileHandle(f,True);
#Change command to the command you want the output of
command = "po self"
debugger.HandleCommand(command)
def __lldb_init_module (debugger, dict):
debugger.HandleCommand('command script add -f po.print_to_file print_to_file ')
Then in lldb write:
command script import ~/.lldb/po.py
print_to_file
I found session save <filename> to be a much better, easier option than those listed here. It's not quite the same as you can't use it (to my knowledge selectively) but for generating logs, it's quite handy.
Here is a slight modification incorporating some of the comments from above:
def toFile(debugger, command, result, dict):
f=open("/Users/user/temp.txt","w")
debugger.SetOutputFileHandle(f,True);
debugger.HandleCommand(command)
f.close()
debugger.SetOutputFileHandle(sys.stdout, True)
This allows the command to be supplied as an argument, and reverts the output file handle to stdout after the command is run.
Assuming that you have a variable named jsonData (which has a Data type) you can save it to a file with this command:
expr jsonData.write(to: URL(fileURLWithPath: "/tmp/datadump.bin"))
Alternatively instead of above command you could dump memory used by this variable to a file as in the example below:
(lldb) po jsonData
▿ Optional<Data>
▿ some : 32547 bytes
- count : 32547
▿ pointer : 0x00007fe8b69bb410
- pointerValue : 140637472797712
(lldb) memory read --force --binary --outfile /tmp/datadump.bin --count 32547 0x00007fe8b69bb410
32547 bytes written to '/tmp/datadump.bin'

get physical hdd's list in c

People, I need get the list of hard disk connected in C language on Linux system:
Example, running a program on a computer with 2 IDE disks and 1 SATA disk connected.
./a.out
Out required:
/dev/hda
/dev/hdb
/dev/sda
help?
Use libsysfs, the recommended way to query the kernel about attached devices of all kinds.
FILE *fp = popen("fdisk -l | grep \"Disk /\" | awk '{print $2};' | sed 's/://'", "r");
while(fgets(path, sizeof(path) -1,fp) != NULL)
//your code
pclose(fp);
The simplest way would be simply read and parse /proc/partitions.
Maybe you can reference fdisk's source code.
Follow this website:
ftp://ftp.gnu.org/gnu/fdisk
Command Line
"ls /sys/block/"
will return output as:
sda sdb sdc
From there, you could creat a script that pipes it to a file, then read in that file as an array or linked list to manipulate the data however you see fit (such as adding /dev/ in front of all device names in the list).

Resources