Is there a way to use exec, but if exec fails to go on with the script?
Example:
Code: #!/usr/bin/env bash exec startx echo "Starting of X failed"
If startx fails, the echo will be seen on the screen. I tried all kind of stuff, but guess it ain't of much use to post it here. I searched the web, but searching for "exec and bash" in one sentence does give results which are not what i am looking for.
I have written the following script in my linux server to add users for LDAP database.But i can't able to run this.
The script is as following
#!/bin/bash echo "Mention the username which you want to convert LDIF format" read username if ["$username" -e "/ldiffile/passwd"]; then echo "Username already exists" else cat /etc/passwd | grep -i "$username" >> /ldiffile/passwd fi The output which i got : . ldapadd.sh Mention the username which you want to convert LDIF format yal2361 -bash: [yal2361: command not found
What options should I use when I'm using the sort command to sort the top 5 CPU processes (ps -eo user,pid,ppid,%cpu,%mem,fname | sort ??? | head -5) showing max to min usage?
I have a requirement to find the files having its name as ack_reply. However, there are many other files in the same directory as these resides. Now I have to remove these files from the folder and retain others after 7 days. So I tried to write the below script with grep command.
find $directory -type f -mtime +7 | grep ack_reply
how can I pass this output to -exec command.
If I am not using grep command my script would be as
find $directory -type f -mtime +7 -exec remove.sh {}\;;
I am trying to automate an svnadmin dump command for a backup script, and I want to do something like this:
find /var/svn/* ( ! -name dir -prune ) -type d -exec svnadmin dump {} > {}.svn ;
This seems to work, in that it looks through each svn repository in /var/svn, and runs svnadmin dump on it.
However, the second {} in the exec command doesn't get substituted for the name of the directory being processed. It basically just results a single file named {}.svn.
I suspect that this is because the shell interprets > to end the find command, and it tries redirecting stdout from that command to the file named {}.svn.
Within a bash script, I'm attempting to redirect file descriptors with exec, e.g. exec 3>&1 1>&2; however, I'd like to do something like exec $FD>&1 1>&2, which doesn't work because bash tries to execute the value of $FD. Various placements of eval fail, as well. Is there a way around this, or am I stuck hard-coding the descriptor?
Just coming over from Windows here... where I've been using "CreateProcess" which returns a value depending on whether the process was created successfully or not.
Now I'm trying to create a process on linux and I've been learning about fork/exec, and I've been struggling with the fact that there seems to be no easy way to know (within the original parent process) if exec succeeded or not. (without forcing the parent process to hang around polling it or something).
Anyway, I've now just discovered the posix_spawn function, and it seems exactly what I need. However I'm finding the documentation a little hard to understand. In particular, I can't find actual confirmation that it will definitely return an error if creating the process fails (like CreateProcess does on windows). So can anyone confirm that for me?
The text on the documentation states: "If posix_spawn() or posix_spawnp() fail for any of the reasons that would cause fork() or one of the exec family of functions to fail, an error value shall be returned as described by fork() and exec"
This makes me think it uses exec/fork under the hood, and so I wonder if it can be trusted..?
And also, most of the examples on the internet for creating processes use fork/exec, so I wonder if there is some catch with posix_spawn I haven't read about... (since it seems a much simpler way of doing things)
Im using gdb for debugging my application.. I was able to debug child process(when fork comes) .. and in child process we have an exec call to .... So the problem is, when the control come to exec , the exec process is executing at a time... I could not debug the exec. process... error is stack curruption due to same frame So, is there any way to debug the exec process
I have been struggling with this for about a week now. I'm working with QuickUSB to send video streams to and from a device. I get the fastest results by fork()ing and exec()ing new processes everytime I run a pipeline, which works great when reading from files, but now i have to transition it so that i am reading/writing data over a QuickUSB bus. I have it working for each individual process, but i can't run multiple processes because it won't let me open the device. After looking through QuickUSB's API a little closer, i found that the QHANDLE type that they use to describe the usb device is actually just a usb_dev_handle *. From what i have found, this is an incomplete structure, so i am having trouble with opening it in the main process and passing it to each new process to write to it. I already have semaphores set up to prevent multiple processes using it at the same time. I just CANT find out how to utilize the same usb device between two process that are forked and exec. i also must do the exec() because otherwise my gtk and gstreamer fails for trying to share resources.
i had a problem with the find command in bash (which i deem is close enough to a promming language, if not please move this thread :P). i tried to reduce the command to the problem. i want the backticks, or $() for that matter; to be evaluated by -exec of find, not by bash. is that a caveat of find?
Code:
$ find testd -exec echo `basename {}` ; #confused me test test/a test/b
[code]...
edit: i found out whats causing this. `basename {}` gets evaluated by bash before find is invoked, returns {} and `find . -exec echo {} ;" is run. now my question is, how to escape this eveluation from happening before.
Groovy is an object-oriented programming language for the Java platform. I do not have experience in Java, only perl and shell scripts. Recently I have been asked to maintain a software written in groovy (also to make enhancements). So can I learn groovy without knowing java language. or isit I have to learn java before venturing into groovy.
I have a monitoring program ( GIT link to sourceforge ) which I'm trying to use to track when a child exits/dies/whatever. I'm calling fork(), then close for 0,1,2, and then opening /dev/null, monitored.stdout, and monitored.stderr as a replacement. I'm not sure if I've done something incorrect (perhaps I should use dup2 for explicit assignment?) but it appears that printf() messages are just being blackholed. I've tried setting the line buffering as a last ditch effort. On a different system, using code similar to the spawn_monitor() function, this appears to work fine, which makes me think I'm relying on some implementation specific detail.
Relevant function, for those who don't click links:
I need to create a zip file of jpg and bmp files. The zip file is named after the first file it finds which ends with .dat. Here is my script:
Code:
DAT_FILE= `find . -maxdepth 1 -iname "*.dat" | head -1 | sed 's/..(.*)..../1/'` (cd pics; find . ( -name "*.bmp" -o -name "*.jpg" ) -print | zip ../$DAT_FILE -@ )
BTW my sed command cut off the first two chars and last four chars since find will return the filename is the form of "./filename.dat" and I just want to extract filename. When I run this script, it creates a zip file named ".zip". How do I fix this so the zip file is named after my dat file?
some basic programming/scripting/etc. ability but I am not able to do what I now need to do. I would like to have a link on a website that when a user clicked on that link, one script or another would execute based on what operating system that person is using. To wit: If a Mac User clicked on it, it would run a terminal script and would set up a served printer for him; if a PC user clicked on it, it would run a script to set up the served printer on his machine, you get the drift. The printer is being served on a Windows Server 2003 machine, the users are NOT domain users, the print server is also hosting the files, scripts, webpage.
How do i find out if a particular item is a file or a folder through the terminal ls -la gives 'd' before the permissions for every folder and '-' before every fileLike i want to write a script that backup data if it is a folder and deletes if it is a file
I'm using the linux 'script' command [URL]... to track some interactive sessions. The output files from that contain unprintable characters, including my backspace keystrokes.
Is there a way to tidy these output files up so they only contain what was displayed on screen?
Or is there another way to record an interactive shell session (input and output)?
I am attempting to create a script for Grid job submissions. I have most of it completed but when testing I've run into some problems.
How it works is that I have 2 wrapper files, one is the top of the script, the other the bottom, the original script imports commands for the middle.
Basically this is what is to happen: cat script-start.sh >> workerscript.sh echo commands >> workerscript.sh cat script-end.sh >> workerscript.sh
workerscript.sh gets created with no problems but when it is run I get: export: command not found
I think that the new script is not calling bash which gives the problem. I don't know how to fix the problem though because I have the shell set in the script-start.sh.
#!/bin/sh - tried /bin/bash, same result export PROJECT=/...... export PROJSOURCE=/......
Given that I want my shell script be invoked at the command line using the above parameters - where [these brackets] denote that they are optional - what is the best method to parse them?
I want to know which is the best scripting language right now and what you guys think its going to be the best or most used in the future, that is, in about 5-10 years. Take into account system administration and applications. This are the languages i had in mind, add more if you think other scripting languages are better or worth considering.
i'm in the process of learing C++. currently i'm creating shell scripts to get things done. i'm just curious how, as a programmer using C++ you would get a similar job done.as an example i have a script that takes the contents of files, pipes it to some sed and awk commands, which is piped to create a new file. that file is then imported into a mysql database.if you were going to do this in C++, would you call the sed/awk programs to modify the file, or can it be done within the program itself? i'm probably jumping the gun here because i've just started learing about pointers so this is above my ability
I am very new to shell scripting.How does one pass a command-line parameter to a shell script?for the below program #/bin/bash mount -t cifs -o user=ramkannan,password=Linux123@ //10.200.1.125/ramkannan /MT cd /MT/test date=`/bin/date "+\%Y-\%m-\%d-\%H-\%M-\%S"` mysqldump -uroot -pram2@ employeedb > $date.sql gzip $date.sql
I want to pass parameter for everything,i tried in google and did but iam getting error while passing parameter to all
#/bin/bash mount -t cifs -o user=$1,password=$2 //10.200.1.125/ramkannan /MT cd /MT/test date=`/bin/date "+\%Y-\%m-\%d-\%H-\%M-\%S"` mysqldump -uroot -pram2@ employeedb > $date.sql gzip $date.sql
i was getting error while passing parameter to all.
I need to find a way to download the attachment from a daily report e-mail to me. The kicker is it will need to be down with a cron tabbed bash script.For example, which linux based CLI client is best suited to be scripted?
I'm trying to put together a script that will quickly run through an archive directory of log files that are named by day of the month 01.gz, 02.gz, 03.gz.... 31.gz. The script uses gunzip -c | grep | wc to count up the total number of hourly occurrences of a filename and outputs the results to stdout.
The only snag I have left is the octal limit when it gets to 08 and 09. I've seen examples using perl and awk, but this script uses a number of nested for loops and if statements that I don't want to have to rewrite in a different syntax. I found that I can use num=10#08 to set that variable to a base 10 instead of a base 8, but then I lose the leading 0 again when it passes the number to the next filename variable.
We have to (re)write a bunch of scripts to download files from remote ftp servers, where we won't know the names beforehand; move the files to an archive folder on the FTP server; copy the files to the correct servers on our LAN; and log the file information in Oracle.Our current scripts are for a windows product called ScriptFTP that provides a nice little language. It does everything except the logging, but we're wanting to get rid of Windows.
I have recently been given the job of maintaining the code, and I'm looking for a good language for simple, robust ftp scripts that will work on Linux. We have some customers with remarkably unreliable FTP servers. If necessary, I can write another program to handle the logging to Oracle. I'm usually a C++ developer, so a similar syntax is a bonus. I also want to be able to hand this over to an entry-level programmer later.
So far, I'm intrigued by PHP, but I've hardly had a chance to look at it for any scripting. I like the idea of being able to loop through the files individually and even be able to skip over anything that looks like it's currently being written (either based on the file name or date/time). I've thought about writing something in C++ that would take a simple config file instead of a full blown script. However, I don't want the maintenance, unless someone writes a quality FTP library that is inexpensive (hopefully free) for commercial use.