How to extract a subsstring in a shell script

Hi,
I need to extract a substring from a string in a shell script. Ideally using a regular expression, but a certain position (character n through n+4) would work in this case, too.
I am fairly familiar with regular expressions, I just don't know the syntax to use in a shell script
Thanks for any help!
Ingo

The equivalent of Bill's command in bash is:
expr $junk : '..\(...\)'Read the man page for expr, particularly the ':' operator. You can use any "basic" regular expressions, but not "extended" REs.
You can also use the stripping operators to remove stuff from the front or end of the string, optionally with a "greedy" match. So:
<pre>p=/usr/local/clamXav/man/man5/clamd.conf.5
echo ${p#????} # strip first 4 chars
/local/clamXav/man/man5/clamd.conf.5
echo ${p%????} # strip last 4 chars
/usr/local/clamXav/man/man5/clamd.co
echo ${p##*/} # greedy strip path components (basename)
clamd.conf.5
echo ${p%%man*} # Greedy strip from end
/usr/local/clamXav/</pre>I dare say these all work in zsh too.

Similar Messages

  • How to submit request(Report+Template) using Shell Scripts?

    Hi Friends,
    How to submit request + Add layout using shell scripts..
    If anybody has sample code..Can you please send me to [email protected]
    Please help me..
    Its Urgent.
    Thanks and Regards,
    A Swain
    Message was edited by:
    SwainA

    Following is a package where the request is submitted and the layout is added.
    Also check iin the other way by adding the layout first n then giving the request.
    CREATE OR REPLACE PACKAGE BODY try
    AS
    PROCEDURE try_proc
    errbuf OUT VARCHAR2,
    retcode OUT NUMBER
    IS
    l_mode BOOLEAN;
    l_request_id NUMBER;
    xml_layout BOOLEAN;
    BEGIN
    fnd_file.put_line (fnd_file.output, 'USER_ID :' || fnd_global.user_id);
    fnd_file.put_line (fnd_file.output, 'REQU_ID :' || fnd_global.resp_id);
    fnd_file.put_line (fnd_file.output,
    'RESP_APPL_ID :' || fnd_global.resp_appl_id
    l_mode := fnd_request.set_mode (db_trigger => TRUE);
    IF l_mode IS NOT NULL
    THEN
    fnd_file.put_line (fnd_file.output,
    'Concurrent MODE Option is Success'
    END IF;
    -- if wrong paramters error recd give all 100 arguments as null
    l_request_id :=
    fnd_request.submit_request (application => 'app_short_name',
    program => 'shortname',
    sub_request => FALSE
    fnd_file.put_line (fnd_file.output,
    'Request_Id 1 is :' || l_request_id
    xml_layout :=
    fnd_request.add_layout (template_appl_name => 'SQLAP',
    template_code => 'CAPINEF01',
    template_language => 'en',
    template_territory => 'US',
    output_format => 'PDF'
    COMMIT;
    EXCEPTION
    WHEN OTHERS
    THEN
    fnd_file.put_line (fnd_file.LOG,
    'Error in procedure package procedure :'
    || SUBSTR (SQLCODE, 1, 20)
    || ':'
    || SUBSTR (SQLERRM, 1, 250)
    END try_proc;
    END try;

  • How we can call or execute a SHELL script through Oracle forms or Reports

    How we can call or execute a SHELL script through Oracle forms or Reports.Its urgent.......

    Use HOST command.

  • How to use the result of simple shell script?

    The shell script below retrieves the length of an audio file:
    set aFile to choose file
    do shell script "afinfo " & quoted form of (POSIX path of aFile) & "|grep duration"
    I'm wondering, how can I copy the result to the clipboard or set the value of a variable to it?
    Total newbie question. I have no idea about shell scripts - I just found the script above online.
    Thank you so much!

    Here:
    set the clipboard to (do shell script "afinfo " & quoted form of (POSIX path of aFile) & "|grep duration")
    or:
    set A to do shell script "afinfo " & quoted form of (POSIX path of aFile) & "|grep duration"
    (53997)

  • How to get name of batch file/shell script that starts program?

    I don't think there's a way to do it, but I'm asking the question anyway...
    of course, the startup script could pass its name as a parameter to the Java program, but I was wondering if the information could be determined "directly," via Java code. The idea is to allow your Java program to modify the startup configuration specified by your startup shell script.
    For example, a "startup configuration" menu item would pop up a dialog box that would display- and allow you to change- the startup parameters, and then let you write the changes back to the script file.

    There are always some ways to do things.
    I guess for your case, you need to go through JNI and have a native program to back track the Process ID of who started who. Never done that but I guess should work.
    But a more natural way to do this is to have your startup configuration stored in an xml file or a properties file inside your classpath.
    --lichu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How to pass arguments to a Unix shell script from PL/SQL?

    We want to run a Linux shell script from PL/SQL (10g). This is our code to run the script only and it works fine.
    dbms_scheduler.create_job
              job_name=>'RUN_LINUX_SCRIPT_' || v_job_name,
              job_type=>'EXECUTABLE',
              job_action=>'/vol0/FileLoadDir/Bank/DATA_FILES/spell_check.sh',
              enabled=>TRUE,
              auto_drop=>FALSE
            );Now we have a requirement to pass 2 arguments to the .sh file. In the .sh file the 2 arguments are defined as $1 and $2.
    I used this method.
    dbms_scheduler.create_job
              job_name=>'RUN_LINUX_SCRIPT_' || v_job_name,
              job_type=>'EXECUTABLE',
              job_action=>'/vol0/FileLoadDir/Bank/DATA_FILES/spell_check2.sh',
              --job_action=>'/vol0/FileLoadDir/Bank/DATA_FILES/spell_check2.sh /vol0/FileLoadDir/Bank/DATA_FILES/ebill2.fmt_form_strings_.txt /vol0/FileLoadDir/Bank/DATA_FILES/abcdefghij.txt',
              --job_action=>'#!/bin/bash spell /vol0/FileLoadDir/Bank/DATA_FILES/ebill2.fmt_form_strings_.txt > /vol0/FileLoadDir/Bank/DATA_FILES/abcde.txt',
              number_of_arguments => 2,
              enabled=>FALSE,
              auto_drop=>FALSE,
              comments => 'Testing by Channa'
          DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE (
            job_name                => 'RUN_LINUX_SCRIPT_' || v_job_name,
            argument_position           => 1,
            argument_value          => '/vol0/FileLoadDir/Bank/DATA_FILES/ebill2.fmt_form_strings_.txt');
          DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE (
            job_name                => 'RUN_LINUX_SCRIPT_' || v_job_name,
            argument_position       => 2,
            argument_value          => '/vol0/FileLoadDir/Bank/DATA_FILES/abcdefghij.txt');
          DBMS_SCHEDULER.enable (name => 'RUN_LINUX_SCRIPT_' || v_job_name);But I get an error saying:
    STANDARD_ERROR="/vol0/FileLoadDir/Bank/DATA_FILES/spell_check2.sh: line 4: read: `/vol0/FileLoadDir/Bank/DATA_FILES/ebill2.fmt_form_strings_.txt': not a valid identifier
    /vol0/FileLoadDir/Bank/DATA_FILES/spell_check2"

    Check this post:
    pass parameter from PL/SQL to Unix "as is"

  • How to Prompt for User/Pass Running Shell Script Remotely through ARD

    So I finally got my Active Directory Script working! However, I realized that I'm not the only one going to be running this script and to have my user name and password in the script itself is beyond foolish. While I know it's possible just to have whoever's running the script to manually add their username and password, I'd prefer to have prompts to make things easier for the rest of my team.
    Here's my current script. I've tested it on the machine I'm connecting to Active Directory and it works fine if you run it on the machine itself in terminal. However, I'm going to be doing this on about 150 machines and the whole point of the script is to do it remotely.
    #! /bin/bash
    MACNAME=$(scutil --get ComputerName)
    read -p "User Name: " USER
    read -p "Password for $USER: " PASS
    dsconfigad -add "CORP.DOMAIN.NET" \
    -username $USER \
    -password $PASS \
    -computer $MACNAME \
    -mobile disable \
    -mobileconfirm disable \
    -localhome enable \
    -useuncpath enable \
    -shell /bin/bash \
    -ou OU=Macs,CN=Computers,DC=corp,DC=DOMAIN,DC=net \
    -force \
    -localpassword "PASSWORD" \
    -groups "GROUPS"
    When I run the UNIX script through ARD to a machine, I get no prompts for USER or PASS. In fact, it gives me the following error "dsconfigad: Authentication server encountered an error while attempting the requested operation. (5202)" So it's not asking for a user name and password.
    Is there any way to make a shell script prompt you for a User Name and Password when you're sending commands remotely through ARD? Or is there another way to do this?
    Any suggestions would be greatly appreciated.
    -rks

    Best solution is to create an account that is exclusive to binding machines.  By doing this, you can embed the user name and password in the script.  Heck, you can post it on your website.  If the account is configured properly, it will only be able to create machine records in a defined container.  If you are mixing Macs and PCs in your AD domain, I also recommend creating an isolated container for your Mac records.  Now, the account has even less access rights as you can make it so it only has rights to the Mac container.
    Otherwise, ARD does not prompt.  You can play around with an expect script but the reality is that you are still embedding the credentials in the script so it really does not achieve what you want.  ARD is not an interactive shell scripting tool.  It is more a fire and forget.
    Reid
    Apple Consultants Network
    Apple Professional Services
    Author "Mavericks Server – Foundation Services" :: Exclusively available in Apple's iBooks Store

  • How to use su in a Korn shell script???

    I'm developing a script which needs to log in to a second user id to complete its processing. When I use su, it prompts for a password. I enter the password and then it hangs at the system prompt. If I enter 'exit', it returns to the original user id and continues the script from there. How can I make it continue executing the script in the second user id?
    The system that I'm using doesn't have the 'expect' command, so I have to find another way.

    This cannot be done by standard Unix commands. But
    this is possible by
    1. Expect
    2. Sudo with NOPASSWD option.
    3. SSH by private/public authentication.
    Among this SSH is very safe, but sudo is easy to configure and use. Try sudo.
    - Hemant Rumde

  • How to Call pl/sql procedure from shell script

    Hi,
    I've one procedure which takes the xml file name as input from one of the remote location. Every day users keep one file in that location.
    For eg:
    ACS_RPO00020110316_20110316220956.xml
    ACS_RPO00020110319_20110319220956.xml
    I need to read the latest file in this case. In this case second one is the latest file.
    Can some body please help me how to write script to pass parameter to that procedure that via shell program and execute that.
    Thanks in advance.
    Regards
    Nagendra

    hi
    I executed the below mentioned steps, but I did not get any result:
    I am pasting all the steps which I did
    -bash-3.2$ ls -lrt
    total 316
    -rw-r--r-- 1 oratest dba 4884 Mar 16 03:56 XXGEBIZ_EPO160320110356.xml
    -rw-r--r-- 1 oratest dba 4884 Mar 16 03:57 XXGEBIZ_EPO160320110357.xml
    -rw-r--r-- 1 oratest dba 4884 Mar 16 17:52 GEBIZ_ACS_RPO00020110316_20110316175216.xml
    -rw-r--r-- 1 oratest dba 38 Mar 16 18:18 GEBIZ_ACS_RPO00020110316_20110316181858.NODATA
    -rw-r--r-- 1 oratest dba 63 Mar 16 22:09 GEBIZ_ACS_RPO00020110316_20110316220956.xml
    -rw-r--r-- 1 oratest dba 9716 Mar 16 22:14 GEBIZ_ACS_RPO00020110316_20110316221429.xml
    -rw-r--r-- 1 oratest dba 12988 Mar 17 01:42 GEBIZ_POC_RPO000EPO20110317_20110317014247.xml
    -rw-r--r-- 1 oratest dba 13103 Mar 17 01:53 GEBIZ_POC_RPO000EPO20110317_20110317015356.xml
    -rw-r--r-- 1 oratest dba 38 Mar 17 01:57 GEBIZ_POC_RPO00020110317_20110317015712.NODATA
    -rw-r--r-- 1 oratest dba 38 Mar 17 18:44 GEBIZ_ACS_RPO00020110317_20110317184423.NODATA
    -rw-r--r-- 1 oratest dba 12326 Mar 17 18:46 GEBIZ_ACS_RPO00020110317_20110317184603.xml
    -rw-r--r-- 1 oratest dba 67578 Mar 18 01:32 GEBIZ_POF_RPO000EPO20110318_20110318013232.xml
    -rw-r--r-- 1 oratest dba 14484 Mar 18 20:27 GEBIZ_POF_RPO000EPO20110318_20110318202754.xml
    -rw-r--r-- 1 oratest dba 47653 Mar 19 01:29 GEBIZ_PYS_RPO00020110319_20110319012953.xml
    -rw-r--r-- 1 oratest dba 937 Mar 21 09:34 GEBIZ_SUPP.xml
    -rw-r--r-- 1 oratest dba 947 Mar 21 09:35 GEBIZ_SUPP1.xml
    -rwxrwxrwx 1 oratest dba 121 Mar 21 17:43 test
    -bash-3.2$
    -bash-3.2$ cat > test
    ls -lrt | tail -1
    filelist = $(ls GEBIZ_SUPP*.xml)
    for file in $filelist ; do
    echo "inside loop"
    file = $file
    echo "file name " $file
    done
    XGBZ_SUPP_MAST_XMLTAG_PROC $file
    -bash-3.2$ chmod 777 test
    -bash-3.2$ ./test
    -bash: ./test: Text file busy
    -bash-3.2$
    Initially I listed down all the files, in a sample file I've written the steps. But nothing is executed.
    One thing I want to ask here XGBZ_SUPP_MAST_XMLTAG_PROC is my procedure. I think I need to invoke sqlplus in the so as to execute my pl/sql script.
    Gurus, please let me know how to write them.
    Regards
    Nagendra

  • How to pass a variable to the shell script and crontab?

    Friends,
    here is my script.....
    exp userid=username/password@realdb file=/u02/logical_backup/abc_+$date+.dmp log=/u02/logical_backup/abc_+$date+.log owner=oraadmin statistics=none
    i want the exported file name as abc_2101.dmp and abc_2101.log (2101 is a date and month)
    I want to execute this script daily at 02:00 AM.
    so that i edited the crontab -e as....
    00 02 * * * ./u02/script/dailybkp.sh
    Now what i want is....
    1. is the steps are correct for crontab -e?
    2. Is the script of the crontab will execute daily at 02:00 AM?
    3. how can i rename the .log filename and .dmp file name daily according to the date. for example abc_2001 is today's date and month.how can i replace with the variable.
    thanks
    sathyguy

    sorry....i tested with cp command it was working fine...
    so, i thought it should work for exp command also.
    but its not working....also its not throwing any error....
    the crontab -e is having...
    00 02 * * * exp userid=system/password@realdb file=/u02/test/n22.dmp log=/u02/test/n22.log owner=scott statistics=none
    also i have tested by calling the script.
    #!/bin/bash
    00 02 * * * /u03/script/testbkp.sh
    its not working
    but the below one is working....
    00 02 * * * cp /u02/test.txt /u02/test/test.txt
    also this one is working....
    #!/bin/bash
    00 02 * * * /u03/script/testbkp.sh
    i listed the chkconfig --list crond
    crond 0,1,6 = off
    2,3,4,5 are on
    what might be the reason?
    thanks
    sathyguy

  • How do I perform a call to shell script from another shell script

    In /jacorb/bin I have to compile some IDL files to java, but I do not know how:
    In the terminal window, I am able to run #sh compile.sh
    But in compile.sh there is another call to a an idl.sh and a ir.sh at the same line
    The error say that it can not find these two files.
    What to do...
    Br Morten Bo

    My compile.sh file looks like this:
    #!/bin/sh
    PATH=/usr/jdk/jdk1.5.0_07/bin:$path;
    # my path --> /JacORB/bin/ and /JacORB/lib
    idl.sh -ir -d ../src -i2jpackage mortens:com.nokia.m2m.orb.idl.egsmt egsmt.idl
    %RESOLVE_JACORB_HOME%
    JACORB_HOME=${RESOLVED_JACORB_HOME}
    @RESOLVE_JAVA_CMD@
    JAVA_CMD=${RESOLVED_JAVA_CMD}
    exec "$JAVA_CMD" \
    @JACORB_BOOTCLASSPATH@ \
    -Djacorb.home="${JACORB_HOME}" \
    -Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB \
    -Dorg.omg.CORBA.ORBSingletonClass=org.jacorb.orb.ORBSingleton \
    -classpath "${CLASSPATH}" \
    "$@"
    And my idl.sh looks like this:
    #!/bin/sh
    # @DONT_EDIT@
    @RESOLVE_JACORB_HOME@
    JACORB_HOME=${RESOLVED_JACORB_HOME}
    @JAVA_CMD@ -classpath ${JACORB_HOME}/lib/idl.jar:${JACORB_HOME}/lib/logkit-1.2.jar:${CLASSPATH} org.jacorb.idl.parser "$@"
    So, what d I have to put in instead of these @@ and $$
    I really do not know how to set up pathnames in solaris

  • How to schedule jobs in cron using shell script

    Pls help me regarding that.
    Thanks

    Not sure about what do you want exactly.
    How to schedule jobs in cronhttp://www.adminschoice.com/docs/crontab.htm
    http://www.rahul.net/raithel/MyBackPages/crontab.html
    Nicolas.

  • How to unzip files in Shell script

    Hi Gurus,
    I have one requirement, where in I need to unzip files at at unix level and then move to the respective folders. How can I code this in the shell script.
    As it is entirely new thing, please let me know how to do this. Seeking few program steps.
    Thanks in advance for your help.
    Regards
    Nagendra

    You can specify unzip -d directory to tell unzip where to extract the files. For instance:
    dest_dir="/tmp/$(date +%N)"
    # zip file is cmd line argument $1
    unzip -d $dest_dir "$1"
    cd $dest_dirI don't know of any way to automatically go to a zip'ed directory stored inside the zip file. Besides, where do you want to go in case there are several directories stored?

  • How to use "do shell script" when Terminal replies with a prompt?

    Hey all,
    I'm wondering how to properly use "do shell script" when Terminal replies with a question? In this example, I'm building a small tool in AppleScript that will auto-update the Android SDK via command line prompts. The problem is when I tell it to update all ("update sdk --no-ui"), I need to accept licence agreements before it continues, and the amount of licence agreements brought up is different every time.
    So, basically, I'm wondering how to handle a case where "do shell script" encounters a case where Terminal would normally need a user's response to?
    Thanks,
    Behn

    If it is variable number of prompts that is a problem.  Look around hd and figure out how many prompts there will be. Ask around android forum for a bash solution. There are some test case apps that will let you put conditionals on output from running programs.
    # traditional way.  Pipe output to command.
    do script "echo 'ok' | update sdk --no-u"
    Here is the preferred way of getting administrator privileges.
    It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
        Author: rccharles
        For testing, run in the Script Editor.
          1) Click on the Event Log tab to see the output from the log statement
          2) Click on Run
        For running shell commands see:
        http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
    on run
        -- Write a message into the event log.
        log "  --- Starting on " & ((current date) as string) & " --- "
        --  debug lines
        set unixDesktopPath to POSIX path of "/System/Library/User Template/"
        log "unixDesktopPath = " & unixDesktopPath
        set quotedUnixDesktopPath to quoted form of unixDesktopPath
        log "quoted form is " & quotedUnixDesktopPath
        try
            set fromUnix to do shell script "sudo ls -l  " & quotedUnixDesktopPath with administrator privileges
            display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
        on error errMsg
            log "ls -l error..." & errMsg
        end try
    end run

  • How to check from a shell script that a particular software is installed

    Hai friends
    I want to write a shell script which has to check whether a particular software is installed on the machine, or not. If installed, then what version is it using and the get the version number and which type of installer is it? i mean is it a .rpm installation or a tar.gz installation.
    how can i check this from a shell script. If any of you have any idea please give me a sample script to check this
    Thank you

    @Raja_Abilash
    I don't think this is a right thread & right forum to POST this question.
    better go ahead with forums related to LINUX.

Maybe you are looking for

  • I am having trouble opening files saved in CS 5.5 Indesign after spending time on the cloud. Help me please

    The last year(2013-2014) of school used CS5.5( I won and is on home computer) at home along with the cloud. School was using CS 6.I learned to save all files at home and at school in CS 4 or better. Saved files in each format so I could work on each

  • Itunes 10.5.2 wont open

    I downloaded itunes 10.5.2 last week and it opened immediately upon downloading it. Since then i cannot get it to open. I have clicked on the desktop shortcut, i have clicked on the icon in start menu and I have tried attaching my ipod to get it to o

  • Syncing a subset of Library

    G'day, I've been struggling with this issue for a while and cannot seem to find an resolution in the forums. I have a 30Gb IPOD and a 60GB library. I'd like to be able to specify certain content (i.e. and Audiobook and a couple of albums) and then ha

  • Itunes won't load on my mac.. the icon just bounces

    This is a problem with all my apple apps it seems... I try loading and the icon just sits on the dock bouncing, then I have to force quit.

  • Customizing the System List

    I'm trying to figure out how to add an item in the System List. Up to now I figured out how to remove an unwanted item. I go into these 2 files (in the Tomcat folder) and delete the section related to the item in question. webapps\rtrlet\xsltransform