tag:blogger.com,1999:blog-24271425265117957772024-02-08T12:43:09.490-08:00Uuden suomenkiinalaisen IT-elämä SuomessaTä jätkä harrastaa koko ajan laiskottelua, joten ei oo yhtä mitään kertomista vielä.Suomenkiinalainenhttp://www.blogger.com/profile/04373472212131154304noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-2427142526511795777.post-27641269146058779272015-04-12T08:44:00.002-07:002015-04-12T08:44:41.408-07:00Linux > Bash > set +ex (debugging Bash scripts)Suomenkiinalainenhttp://www.blogger.com/profile/04373472212131154304noreply@blogger.com0tag:blogger.com,1999:blog-2427142526511795777.post-75753737115374913552015-04-03T03:46:00.002-07:002015-04-03T03:47:09.349-07:00Linux > Bash > shopt (expand_aliases)<b>Problem:</b><br />
<div>
I am currently coding some Bash scripts to install Hadoop on Aalto University's Triton SLURM system through SBATCH. <b>Bash script running through SBATCH can't recognize my own aliases, whereas the same alias, which I created, can be recognized by running though Triton's front end or our school's kosh host</b>. </div>
<div>
<br /></div>
<div>
<b>Investigation:</b></div>
<div>
<b>My initial wrong guess</b> at the beginning: <b>the SLURM system just don't support aliases</b>. </div>
<div>
<b>Why my guess is wrong</b>: after Googling with keyword - <b>bash display all aliases</b>, I found the link:</div>
<div>
http://stackoverflow.com/questions/948008/linux-command-to-list-all-available-commands-and-aliases</div>
<div>
There is command: "<b>compgen -a</b>" will list all the aliases you could run. </div>
<div>
Then I run the following commands:</div>
<div>
<b>alias alias_test='echo "hello world"'</b></div>
<div>
<b>alias_test</b></div>
<div>
<b>compgen -a</b></div>
<div>
In the <b>stdout</b> log, "compgen -a" indeed listed the aliases, but in the <b>stderr</b> log I got error message like: /var/spool/slurmd/job17374903/slurm_script: line 25: <b>alias_test: command not found</b></div>
<div>
<b><br /></b></div>
<div>
<b>Second guess: There must be some settings on SLURM, which makes my aliases not recognizable.</b></div>
<div>
Research:<b> </b>I read page: <b>http://tldp.org/LDP/abs/html/aliases.html</b>. Surprisingly I found in their alias making example, there is an extra line: <b>shopt -s expand_aliases</b></div>
<div>
Then I read shopt's manual (man shopt): </div>
<div>
option <b>-s </b>means: <b>Enable (set) each optname</b>.</div>
<div>
<b>Reasoning of my second guess</b>: command <b>shopt -s expand_aliases </b>enables the expand_aliases, then I tried to add this line at the beginning of my previous code like:</div>
<div>
<b>shopt -s expand_aliases</b></div>
<div>
<div>
<b>alias alias_test='echo "hello world"'</b></div>
<div>
<b>alias_test</b></div>
<div>
<b>compgen -a</b></div>
</div>
<div>
Now my alias can be recognized!</div>
<div>
<br /></div>
<div>
Moreover, at beginning of shopt manual, there is text: At the beginning of manual:</div>
<div>
"Toggle the values of variables controlling optional shell behavior. <b>With no options, or with the -p option, a list of all settable options is displayed, with an indication of whether or not each is set. </b> The -p option causes output to be displayed in a form that may be reused as input. Other options have the following meanings:"</div>
<div>
Then I added <b>shopt -p</b> to previous code:</div>
<div>
<b><span style="color: red;">shopt</span></b></div>
<div>
<div>
<b>shopt -s expand_aliases</b></div>
<div>
<b>echo "after expand_aliases enabled"</b></div>
<div>
<b><span style="color: red;">shopt</span></b></div>
<div>
<div>
<b>alias alias_test='echo "hello world"'</b></div>
<div>
<b>alias_test</b></div>
<div>
<b>compgen -a</b></div>
</div>
</div>
<div>
Then ran it through sbatch:</div>
<div>
the log before text "after expand_aliases enabled" displayed: <b>expand_aliases off</b></div>
<div>
the log after text "after expand_aliases enabled" displayed: <b>expand_aliases on</b></div>
<div>
<b><br /></b></div>
<div>
<b>=> solution: add "</b><b>shopt -s expand_aliases</b><b>" at the beginning of the Bash script!</b></div>
<div>
<b><br /></b></div>
Suomenkiinalainenhttp://www.blogger.com/profile/04373472212131154304noreply@blogger.com0