My script is as follows :
echo " enter the directory path of file "
cd $path

echo "">/tmp/df_mac
echo " enter the name of file"

count=`expr 1`
one=`expr 1`
while read line
IDX=`expr index $line \/`
echo $IDX
echo $line
count=`expr $count + $one`

My input file is :
trace (

The O/p I am getting is

expr: syntax error

/1 /advancemfg /backup /bin /boot /buildblr /buildsjc /corp /departments /dept /dev /etc /global /home /import /initrd /lib /lost+found /media /misc /mnt /net /opt /proc /proj /root /sbin /scm_adm_remedy /scm_adm_spre /scm_adm_sre /scm_com_cla /scm_dev_aadm /scm_dev_ams /scm_dev_asic_emulator /scm_dev_avalon /scm_dev_bsi /scm_dev_catapult /scm_dev_dbgtools /scm_dev_dmc /scm_dev_dmm /scm_dev_efcm /scm_dev_efcm_eccapi /scm_dev_efcm_eccapi_pvob /scm_dev_efcm_pvob /scm_dev_efcm_ut /scm_dev_efcm_ut_pvob /scm_dev_efcmapi_pvob /scm_dev_efcmelmgr /scm_dev_efcmelmgr_pvob /scm_dev_efcmlegacy /scm_dev_efcmlegacy_pvob /scm_dev_efcmmpi /scm_dev_efcmxproj_common /scm_dev_efcmxproj_common_pvob /scm_dev_ezswitchsetup /scm_dev_fchba /scm_dev_fchba_toolchains /scm_dev_fos_lgen /scm_dev_isnap_test /scm_dev_lsi /scm_dev_maps /scm_dev_mldds /scm_dev_pki /scm_dev_prism /scm_env_globaltools /scm_env_metadata /scm_env_testage /scm_fvt_dce /scm_fvt_dcfm /scm_fvt_fcip /scm_grp_els /scm_grp_interwoven /scm_grp_itcollaboration /scm_grp_itobiee /scm_grp_itobiee_pvob /scm_grp_itoracleerp /scm_grp_itprojects /scm_grp_itprojects_ucm /scm_grp_itprojects_ucm_pvob /scm_grp_itpvob /scm_grp_itsharepoint_ucm /scm_grp_itsharepoint_ucm_pvob /scm_grp_itslk /scm_grp_itslk_swportal_ucm /scm_grp_itslkucm /scm_grp_itslkucm_pvob /scm_grp_itsoa /scm_grp_itsoa_pvob /scm_grp_itv2soa /scm_grp_itv3soa /scm_grp_itweb /scm_grp_r12 /scm_grp_rafw /scm_grp_rafw_dep /scm_grp_rafw_pvob /scm_grp_support /scm_grp_test /scm_int_media /scm_int_tpsinstallers /scm_jnk_fan /scm_jnk_grpitslk /scm_jnk_mergetest /scm_jnk_metadata /scm_jnk_rcitest1 /scm_jnk_rcitest2 /scm_jnk_simple /scm_jnk_testcq /scm_jnk_testinm /scm_jnk_testinm_pvob /scm_jnk_tetest /scm_jnk_training /scm_jnk_trainingte05 /scm_jnk_ucm /scm_jnk_ucm_pvob /scm_jnk_winrci /scm_oss_efcm /scm_oss_javajars /scm_oss_testtools /scm_rel_docs /scm_sqa_api /scm_sqa_arm /scm_sqa_bfc /scm_sqa_bosman /scm_sqa_dca /scm_sqa_dcfm /scm_sqa_dmm /scm_sqa_embedded /scm_sqa_ff /scm_sqa_fm /scm_sqa_fos /scm_sqa_fosreg /scm_sqa_hba /scm_sqa_nos /scm_sqa_oem /scm_sqa_psi /scm_sqa_sas /scm_sqa_scalability /scm_sqa_smia /scm_sqa_sustain /scm_sqa_toolsdev /scm_sqa_v0fos /scm_sqa_v2dcfm /scm_sqa_v2embedded /scm_sqa_v2fm /scm_sqa_v2fos /scm_sqa_v2oem /scm_sqa_v2smia /scm_sqa_v2sustain /scm_sqa_v3oem /scm_sqa_v4oem /scm_sqa_xpath_2 /scm_sqa_xpath_webtools /scm_tps_efcm /scm_tps_efcm_pvob /scm_tps_fos_safenet /scratch /selinux /srv /stage /swapfile1 /swapfile2 /sys /tftpboot /tmp /users /usr /var /view /vobs /vws1
expr: syntax error

trace (

The problem is coming in reading '*'.
can somebody tell reason and way out?

Recommended Answers

All 5 Replies

Many lines in the above script are unnecessary for this issue. please ignore them

Hi suman.great!

Here's a line from the 'expr' man page that relates to the problem that you're having: Beware that many operators need to be escaped or quoted for shells. You can work around this simply by quoting your variables. I tested this with your sample data and this script:

cat test.txt | while read line; do 
    IDX=`expr index "$line" \/`
    echo "$IDX"
    echo "$line"

Putting quotes around "$line" in your expr command allows expr to read it correctly. Quoting "$line" again for your echo command allows it to echo the literal "*" instead of translating * to all of the files in the current directory.

I hope this helps!


Thanks .... it works .
can somebody explain What was shell interpreting and how this unexpected lines appearing ?

Sure Suman.great!

Your first error was here: expr: syntax error This was a result of the shell expanding "/*" into all of the file and directory names in the top level (root, or /) of the filesystem, instead of a literal "/*"

This would make your 'expr' command, with the variable expanded, look like this: expr index /1 /advancemfg /backup /bin /boot /buildblr /buildsjc /corp /departments /dept /dev /etc /global /home /import /initrd /lib /lost+found /media /misc /mnt /net /opt /proc /proj /root /sbin /scm_adm_remedy /scm_adm_spre /scm_adm_sre /scm_com_cla /scm_dev_aadm /scm_dev_ams /scm_dev_asic_emulator /scm_dev_avalon /scm_dev_bsi /scm_dev_catapult /scm_dev_dbgtools /scm_dev_dmc /scm_dev_dmm /scm_dev_efcm /scm_dev_efcm_eccapi /scm_dev_efcm_eccapi_pvob /scm_dev_efcm_pvob /scm_dev_efcm_ut /scm_dev_efcm_ut_pvob /scm_dev_efcmapi_pvob /scm_dev_efcmelmgr /scm_dev_efcmelmgr_pvob /scm_dev_efcmlegacy /scm_dev_efcmlegacy_pvob /scm_dev_efcmmpi /scm_dev_efcmxproj_common /scm_dev_efcmxproj_common_pvob /scm_dev_ezswitchsetup /scm_dev_fchba /scm_dev_fchba_toolchains /scm_dev_fos_lgen /scm_dev_isnap_test /scm_dev_lsi /scm_dev_maps /scm_dev_mldds /scm_dev_pki /scm_dev_prism /scm_env_globaltools /scm_env_metadata /scm_env_testage /scm_fvt_dce /scm_fvt_dcfm /scm_fvt_fcip /scm_grp_els /scm_grp_interwoven /scm_grp_itcollaboration /scm_grp_itobiee /scm_grp_itobiee_pvob /scm_grp_itoracleerp /scm_grp_itprojects /scm_grp_itprojects_ucm /scm_grp_itprojects_ucm_pvob /scm_grp_itpvob /scm_grp_itsharepoint_ucm /scm_grp_itsharepoint_ucm_pvob /scm_grp_itslk /scm_grp_itslk_swportal_ucm /scm_grp_itslkucm /scm_grp_itslkucm_pvob /scm_grp_itsoa /scm_grp_itsoa_pvob /scm_grp_itv2soa /scm_grp_itv3soa /scm_grp_itweb /scm_grp_r12 /scm_grp_rafw /scm_grp_rafw_dep /scm_grp_rafw_pvob /scm_grp_support /scm_grp_test /scm_int_media /scm_int_tpsinstallers /scm_jnk_fan /scm_jnk_grpitslk /scm_jnk_mergetest /scm_jnk_metadata /scm_jnk_rcitest1 /scm_jnk_rcitest2 /scm_jnk_simple /scm_jnk_testcq /scm_jnk_testinm /scm_jnk_testinm_pvob /scm_jnk_tetest /scm_jnk_training /scm_jnk_trainingte05 /scm_jnk_ucm /scm_jnk_ucm_pvob /scm_jnk_winrci /scm_oss_efcm /scm_oss_javajars /scm_oss_testtools /scm_rel_docs /scm_sqa_api /scm_sqa_arm /scm_sqa_bfc /scm_sqa_bosman /scm_sqa_dca /scm_sqa_dcfm /scm_sqa_dmm /scm_sqa_embedded /scm_sqa_ff /scm_sqa_fm /scm_sqa_fos /scm_sqa_fosreg /scm_sqa_hba /scm_sqa_nos /scm_sqa_oem /scm_sqa_psi /scm_sqa_sas /scm_sqa_scalability /scm_sqa_smia /scm_sqa_sustain /scm_sqa_toolsdev /scm_sqa_v0fos /scm_sqa_v2dcfm /scm_sqa_v2embedded /scm_sqa_v2fm /scm_sqa_v2fos /scm_sqa_v2oem /scm_sqa_v2smia /scm_sqa_v2sustain /scm_sqa_v3oem /scm_sqa_v4oem /scm_sqa_xpath_2 /scm_sqa_xpath_webtools /scm_tps_efcm /scm_tps_efcm_pvob /scm_tps_fos_safenet /scratch /selinux /srv /stage /swapfile1 /swapfile2 /sys /tftpboot /tmp /users /usr /var /view /vobs /vws1 \/ Expr just doesn't know what to do with this.

The output that you see AFTER that error is what "/*" expands to when not using quotes. An easy way to see this is with 'echo'. Try echo'ing that line with and without quotes:

# echo /*
/bin /boot /dev /etc /home /initrd /lib /lost+found /media /mnt /opt /proc /root /sbin /selinux /srv /sys /tmp /usr /var

# echo "/*"

I hope this helps!

ya .... sounds logical .....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.