bash

BASH script to copy files based on date, with a catch...

Let me explain the tree structure: I have a network directory where several times a day new .txt files are copied by our database. Those files sit on directory based on usernames. On the local disk I have the same structure (directory based on usernames) and need to be updated with the latest .txt files. It's not a sync procedure: I copy...

Extracting Nodes from multiple xml files

Hi there, I have three xml files of similar structure and I would like to use an xpath-expression to extract all matching nodes in these files and write them to a thrid one. Do you know a good tool to handle this? I am thinking of something like $supermagicxpathtool -x "//whoopdee" file1.xml file2.xml file3.xml > resultfile.xml ...

bash trick to copy files to a previously visited directory

here's the scenerio - you are in bash :~/dirA$cd /dirb :/dirb$cp filex __here_i_want_trick_to_reference_dirA example of a similar trick is "cd -", which puts you into the previously visited directory. I want this because, in reality, the paths I am dealing with are huge and I'm looking for a shortcut. furthermore, a trick which h...

Why does ditto send errors to stdout and output to stderr?

Maybe I'm missing something, but when I type do a verbose ditto command (ditto [-v] [-V] 2>>some.log), it will send regular output to stderr. Therefore, my some.log file will be filled with entries like copying file .... This doesn't make logical sense to me, but it's what the manual says as well: -V Print a line of output ...

how to split a string in shell and get the last field

hi, suppose I have the string "1:2:3:4:5" and I want to get its last field ("5" in this case). how do I do that using Bash? I tried cut, but I don't know how to specify the last field with -f. ...

how do I set bash to show autocomplete options on a single tab press?

If bash autocomplete finds multiple files, it only autocompletes the common part and I have to press TAB again to have it show me a list of them. I know there's a way to make this happen on the first TAB press, but how? In other words, current behaviour: $ cd fTAB turns into: $ cd foo another TAB gets me a list "foobar foobiz". ...

checking file size

I want to check the file size in shell script. I am trying to check if the file in a specific directory exceeded 2 gb i.e 2147483648 bytes. How can I easily do this in shell script? I have the following 2 files: -rw-rw-rw- 1 op general 1977591120 Jul 02 08:27 abc -rw-rw-rw- 1 op general 6263142976 Jul 01 18:39 x...

How to call shell script from php that requires SUDO?

I have a file that is a bash script that requires SUDO to work. I can run it from the command line using SUDO but I will be prompted to put in the SUDO password. I want to run this script from php via shell_exec but I if I call SUDO, its not like a command line where I can be prompted for the password. Is there a way to pass the passwo...

Bash script that edits itself

I'm trying to write a bash script that can edit itself. The line of code I'm trying to use is: sed "s/$STRING_TO_REPLACE/$NEW_STRING/" < $0 > $0 But after running the script I end up with an empty file. Any suggestions? I am creating a script that has two variables that contain default values. The only problem is, these default value...

Small help with bash

Hello, I'm using accessing thorough DBUS from some process. The trouble is that it runs from another user and session of DBUS is different for them. So I can't acces application which uses DBUS through another process if the sessions are different. I found the way to resolve this trouble: some script writes into file dbus session data fr...

redirect COPY of stdout to log file from within bash script itself

I know how to redirect stdout to a file: exec > foo.log echo test this will put the 'test' into the foo.log file. Now I want to redirect the output into the log file AND keep it on stdout i.e. it can be done trivially from outside the script: script | tee foo.log but I want to do it from the inside I tried exec | tee foo.log ...

Bash script to prepend a random number to all files

I have a "smart" phone that doesn't seem to have a music shuffle function, so the next best thing is to write a bash script to prepend all filenames in the current directory with a random number. Is this difficult to do? Thanks. ...

Writing a simple backup script in bash and sending echo output to email.

Hi all. I've written a script that backs up my financial spreadsheet document to another hard drive and another computer. I have also set up my server with email to send cronjob messages to my email instead of system mail. In my script I can't figure out how to use if then to check date of backed up file and current date. Here is the...

How to do a while loop with a string redirected into it

Im trying to loop though a string with HTTP links inside and newlines, I want to loop over a line at a time. At the moment I have echo -e "$HTTP_LINKS" | while read HTTP_S_LINK ; do TEST_STRING="test" done But this way I don't have access to the TEST_STRING out side the loop, which is what I want. I'm using the while loop so that...

Bash function calling command given as argument

How do you write a function in bash that executes the command that it is given as an argument, where The given command may be an alias Arguments must be passed on exactly as given; no evaluating may be done In other words, how to write an as-transparent-as-possible wrapper function. The goal of the wrapper function could for example...

How Can i Rename All Files in a Directory using For Loop?

Here is my snippet: #!/bin/sh for fname in * do if [! -d "$fname"] then WHAT CAN I DO fi done All new named files has to have this format O.fname - as you see preceeded by O. Can you please help? I know i have to use mv somewhere. ...

Check for valid link (URL)

I was reading though this other question which has some really good regex's for the job but as far as I can see non of them work with BASH commands as BASH commands don't support such complex rexeg's. if echo "http://www.google.com/test/link.php" | grep -q '(https?|ftp|file)://[-A-Z0-9\+&@#/%?=~_|!:,.;]*[-A-Z0-9\+&@#/%=~_|]'; then ...

Create a wrapper for wine that runs under another user?

I have created a user called wine to run Wine under for two reasons: any malicious activity can only damage /home/wine Wine tends to pollute the home folder with heaps of configuration files in ~/.local However, I wish to create a wrapper for wine so that when run, sets the UID and runs Wine under that user. So far, my current idea ...

What is the bash "test" command evaluation order?

What is the order in which the test or [ expressions are evaluated in a bash script? Is it the same as the order in which they are written? Are they all evaluated? In the following example: if [ expr1 -a -n expr2 ] will expr2 be evaluated if expr1 is false? ...

How do I call a Python/Perl script in bin folder from a Bash script?

I previously used to copy Python/Perl scripts to access from my bash script. Duplication is not a good idea I know! Is there a way to call them from bin or libs folder that we have set up? For instance : My python script resides in /home/ThinkCode/libs/python/script.py My bash script resides in /home/ThinkCode/NewProcess/ProjectA/run...